properties appear to lose their specialness inside context managers at class scope
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the reported Python snippet and confirming both diagnostics involving the property setter inside the class-scope context manager. Trace mypy's property and class-body handling for this pattern; done means the setter is recognized and the assignment produces neither reported error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 40/100