Name already defined error when using `property.setter` on a property declared with function-based syntax
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When using function-based syntax to declare property (i.e. foo = property(get_foo)), mypy reports a "Name already defined" error when trying to reassign to it using property.setter (see below)
To Reproduce
- Save this code to a python file:
class X:
def __init__(self):
self._foo = 123
def get_foo(self) -> int:
return self._foo
foo = property(get_foo)
@foo.setter
def foo(self, value: int) -> None:
self._foo = value
- Run mypy on it
- See output.
Expected Behavior
No errors. I think this should apply not just to properties but also to any other custom descriptors.
Actual Behavior
I got an error
main.py:10: error: Name "foo" already defined on line 8
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: both 0.910 and latest
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.9
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 with the reported Python reproducer and run mypy to confirm the "Name already defined" diagnostic. Trace how function-based properties and the @foo.setter reassignment are analyzed, including whether custom descriptors follow the same path; done means the reproducer and equivalent descriptor cases type-check without the duplicate-name error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100