mypy icon indicating copy to clipboard operation
mypy copied to clipboard

properties appear to lose their specialness inside context managers at class scope

Open glyph opened this issue 2 years ago • 0 comments

Bug Report

Although reveal_type claims that @property objects are simply callables of the type of function they decorate, mypy does know that they have setter and getter attributes. However, if they are used inside a contextmanager, they lose this special property (although it obviously remains so at runtime):

To Reproduce

from contextlib import contextmanager
from typing import Iterator

@contextmanager
def noop() -> Iterator[None]:
    yield

class X:
    @property
    def a(self) -> int:
        return 7

    with noop():
        @a.setter
        def a(self, newValue: int) -> None:
            print("was set", newValue)


X().a = 9

https://mypy-play.net/?mypy=latest&python=3.11&gist=31b6919b60f665c999edc543233c3484

Expected Behavior

I'd expect neither of the errors here.

Actual Behavior

main.py:14: error: "Callable[[X], int]" has no attribute "setter"  [attr-defined]
main.py:19: error: Property "a" defined in "X" is read-only  [misc]
Found 2 errors in 1 file (checked 1 source file)

glyph avatar Jun 23 '23 23:06 glyph