private (name mangled) attributes within inherited classes can not have different types
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
mypy does not recognise that a dunder class attribute does not clash with the inherited one
To Reproduce
Consider the following example:
class BaseSpecialValue:
def __init__(self, special_value:int):
self.__special_value = special_value
@property
def base_special_value(self) -> int:
return self.__special_value
class SpecialValue(BaseSpecialValue):
def __init__(self, base_special_value:int, special_value:str):
super().__init__(special_value=base_special_value)
self.__special_value = special_value
@property
def special_value(self) -> str:
return self.__special_value
if __name__ == '__main__':
foo = SpecialValue(special_value='one', base_special_value=1)
print(f'special_value={foo.special_value}, base_special_value={foo.base_special_value}')
In this case both SpecialValue and BaseSpecialValue define __special_value, when run it is clear that python treats them separately as the result is:
special_value=one, base_special_value=1
Expected Behavior
No mypy error reported
Actual Behavior
However, when mypy is run the following result is returned by mypy:
mypy --config-file=default_mypy.ini mypy_dunder_test.py
mypy_dunder_test.py:15: error: Incompatible types in assignment (expression has type "str", variable has type "int") [assignment]
mypy_dunder_test.py:19: error: Incompatible return value type (got "int", expected "str") [return-value]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.17.1
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): See below - Python version used: 3.12.0
default configuration used
# Global options:
[mypy]
warn_return_any = True
warn_unused_configs = True
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 provided inheritance example with mypy 1.17.1 and confirming the two reported errors. Trace how mypy handles double-underscore attribute names across inherited classes, then verify that the example produces no errors while preserving the distinct inferred types and property return types.
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
- 45/100