python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.49k stars 2.83k forks source link

Using `x = property(get_x)` in a class definition misbehaves #16827

Open finite-state-machine opened 9 months ago

finite-state-machine commented 9 months ago

[It's difficult to believe this hasn't been reported before, but, with apologies, I haven't been able to find an open issue describing this.]

Bug Report

Mypy doesn't support using @property as a function in a class definition. This makes it challenging to work around #6700.

We might expect these declarations to be similar (a11y version follows):

# with '@property' used as a function:            │  # with '@property' used as a decorator:
                                                  │
class SomeClass:                                  │  class SomeClass:
                                                  │
    def _get_foo(self) -> int:                    │      def _get_foo(self) -> int:
        return 42                                 │          return 42
                                                  │
    foo = property(get_foo)                       │      @property
                                                  │      def foo(self) -> int:
                                                  │          return _get_foo()
Accessible version of the above ```python3 # with '@property' used as a function: class SomeClass: def _get_foo(self) -> int: return 42 foo = property(get_foo) # with '@property' used as a decorator: class SomeClass: def _get_foo(self) -> int: return 42 @property def foo(self) -> int: return _get_foo() ```

But in mypy, the left form results in foo having type Any, whether accessed on the class itself or an instance.

To Reproduce

mypy-play.net: gist

from typing_extensions import (
    assert_type,
    )

class SomeClass:
    @property
    def controlcase(self) -> int:
        return 42

    def get_testcase(self) -> int:
        return 42

    testcase = property(get_testcase)

inst = SomeClass()

# 'testcase' should behave the same way as 'controlcase':
reveal_type(SomeClass.controlcase)  # ... "def (self: SomeClass) -> int"
reveal_type(inst.controlcase)       # ... "int"

# but it does not:
reveal_type(SomeClass.testcase)     # ... "Any"
reveal_type(inst.testcase)          # ... "Any"

Expected Behavior

controlcase and testcase should be indistinguishable

Actual Behavior

Mypy doesn't understand @property when used as a function.

Your Environment

finite-state-machine commented 9 months ago

Accessible version of the first code block in this issue's description:

# with '@property' used as a function:

class SomeClass:

    def _get_foo(self) -> int:
        return 42

    foo = property(get_foo)

# with '@property' used as a decorator:

class SomeClass:

    def _get_foo(self) -> int:
        return 42

    @property
    def foo(self) -> int:
        return _get_foo()
erictraut commented 9 months ago

The property decorator requires significant special-casing within a static type checker, so mypy's behavior here doesn't surprise me. You're using property in a very unusual manner here. FWIW, pyright's behavior is the same as mypy in this case.

finite-state-machine commented 9 months ago

You're using property in a very unusual manner here.

Agreed. I was hoping to work around #6700 as follows:

class SomeClass:
    ...
    some_prop = property(getter, setter)
    some_alias = property(getter, setter)

I guess I'll have to "spell it out" until #6700 is definitively addressed.