You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Bug Report
In MyPy version 1.11.0, the type inference for loop variables seems to persist across different loops when the same variable name is used. This behavior differs from MyPy version 1.10.1, where the code works without issues.
Expected Behavior
In previous versions, this did not raise an issue.
Actual Behavior
test.py:9: error: Incompatible types in assignment (expression has type "Literal['__dict__']", variable has type "Literal['__module__', '__name__', '__qualname__', '__doc__', '__annotations__']") [assignment]
Found 1 error in 1 file (checked 1 source file)
This is seemingly fixed by changing the loop variables
for attr_1 in WRAPPER_ASSIGNMENTS:
object.__setattr__(self, attr_1, getattr(self.stage_fn, attr_1))
for attr_2 in WRAPPER_UPDATES:
getattr(self, attr_2).update(getattr(self.stage_fn, attr_2, {}))
This workaround should not be necessary, as loop variables should be scoped independently.
I think this isn't a bug. Loop variables in Python are function-scoped and do continue existing beyond the end of the loop. Examples like
a: list[str]
b: list[int]
for x in a:
pass
for x in b:
pass
have failed since at least mypy 1.0. I think the difference is that mypy 1.11 now assigns the type Literal['__module__', '__name__', '__qualname__', '__doc__', '__annotations__', '__type_params__'] to attr on the first loop instead of the type str. If you want this to succeed without needing to rename the variable, maybe try declaring attr: str before the first loop?
Bug Report
In MyPy version 1.11.0, the type inference for loop variables seems to persist across different loops when the same variable name is used. This behavior differs from MyPy version 1.10.1, where the code works without issues.
To Reproduce
Expected Behavior
In previous versions, this did not raise an issue.
Actual Behavior
This is seemingly fixed by changing the loop variables
This workaround should not be necessary, as loop variables should be scoped independently.
Your Environment
pyproject.toml:
The text was updated successfully, but these errors were encountered: