mypy icon indicating copy to clipboard operation
mypy copied to clipboard

False negative on `setter` field of undefined variable

Open sterliakov opened this issue 7 months ago • 0 comments

Bug Report

Property setter detection is now a bit too greedy. No matter what, a decorator of the shape @something.setter is treated as a setter of the preceding @property - irrespective of what something is.

To Reproduce

class Demo:
    @property
    def foo(self) -> int:
        return 0
    
    @certainly_not_a_foo_what_is_this_at_all.setter
    def foo(self, _: int) -> None:
        ...


d = Demo()
reveal_type(d.foo)  # N: Revealed type is "builtins.int"
d.foo = 0
d.foo = ''  # E: Incompatible types in assignment (expression has type "str", variable has type "int")  [assignment]

https://mypy-play.net/?mypy=master&python=3.12&flags=strict&gist=92b58f4d15da129a921ab39d8c0548cf

Expected Behavior

Say that the long name is not defined.

Actual Behavior

See inline comments - that thing is still treated as foo setter.

Your Environment

  • Mypy version used: master
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

sterliakov avatar Jun 18 '25 13:06 sterliakov