Skip to content

Commit 1e0ee99

Browse files
authored
Fix regression on redefinition in deferred loop (#20879)
Trivial fix for a regression introduced in #20865, a partial type is usually the same as no type at all.
1 parent 39156e0 commit 1e0ee99

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

mypy/checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,6 +4409,7 @@ def check_lvalue(
44094409
and isinstance(lvalue.node, Var)
44104410
and lvalue.node.is_inferred
44114411
and lvalue.node.type is not None
4412+
and not isinstance(lvalue.node.type, PartialType)
44124413
# Indexes in for loops require special handling, we need to reset them to
44134414
# a literal value on each loop, but binder doesn't work well with literals.
44144415
and not lvalue.node.is_index_var

test-data/unit/check-redefine2.test

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,23 @@ def f() -> None:
11411141
reveal_type(x) # N: Revealed type is "Any"
11421142
[builtins fixtures/tuple.pyi]
11431143

1144+
[case testNewRedefinitionPartialTypeInLoopDeferred]
1145+
# flags: --allow-redefinition-new --local-partial-types
1146+
1147+
def f(self) -> None:
1148+
x = []
1149+
for i in [1, 2, 3]:
1150+
C().x
1151+
x.append(1)
1152+
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
1153+
1154+
class C:
1155+
def __init__(self) -> None:
1156+
self.x = defer()
1157+
1158+
def defer() -> int: ...
1159+
[builtins fixtures/list.pyi]
1160+
11441161
[case testNewRedefinePartialTypeForUnderscore]
11451162
# flags: --allow-redefinition-new --local-partial-types
11461163

0 commit comments

Comments
 (0)