python / python/mypy

Name already defined error when using `property.setter` on a property declared with function-based syntax

Open
#11,008 0 comments 12 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-descriptors
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

  1. 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
  1. Run mypy on it
  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.