Open
Description
Maybe we code like this shouldn't generate an error when using --allow-redefinition
:
x: List[int] = []
# do something with x
x = [] # Error: need annotation
The reason for the error is that the second assignment creates a completely independent variable, so the type of the original definition has no effect.
There are still some open issues:
- Using the context can generate false positives, in case the correct type would be something different for the second definition. I don't know how often this would be an issue.
- Propagating the type context is somewhat tricky since the name of the previous definition is not trivial to calculate. The rules for renaming depend on the scope (local/class/global).
- If/when we support more general redefinitions (not just within a single block), it may be unclear when we should propagate context. It may be best to first to make redefinitions more general and then reconsider this issue.
Follow-up to #6197.