Redefinition without reassignment doesn't check existing value
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
With --allow-redefinition, mypy allows a variable to be redefined, without checking that the existing value is the correct type.
a: str = "foo"
print(a) # value needs to be used before redefinition
a: int # but it is really still str
if a > 1:
...
Expected Behaviour
In this example, mypy should detect that a is still a str, and not allow this redefinition.
It could treat the redefinition as though it is a new variable, being assigned the value of the existing one. That would result in an error like:
error: Incompatible types in assignment (expression has type "str", variable has type "int")
Or it could be a new error, like:
error: Redefinition incompatible with existing type (existing type "str", new type "int")
Actual Behaviour
mypy:
Success: no issues found in 1 source file
python:
TypeError: '>' not supported between instances of 'str' and 'int'
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 reproducing the example in the linked mypy-play.net run with --allow-redefinition, then trace mypy's handling of annotated redefinitions. The work is done when the existing str value is checked against the later int annotation and mypy reports an appropriate error instead of success.
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
- 35/100