Skip to content

Commit 42e28ac

Browse files
committed
Be more liberal in when we broaden a type back to Any
1 parent 2d67fc3 commit 42e28ac

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

mypy/binder.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,10 @@ def assign_type(self, expr: Expression,
271271
and not restrict_any):
272272
# If x is Any and y is int, after x = y we do not infer that x is int.
273273
# This could be changed.
274-
if not isinstance(type, AnyType):
275-
# We narrowed type from Any in a recent frame (probably an
276-
# isinstance check), but now it is reassigned, so broaden back
277-
# to Any (which is the most recent enclosing type)
278-
self.put(expr, enclosing_type)
274+
# Instead, since we narrowed type from Any in a recent frame (probably an
275+
# isinstance check), but now it is reassigned, we broaden back
276+
# to Any (which is the most recent enclosing type)
277+
self.put(expr, enclosing_type)
279278
elif (isinstance(type, AnyType)
280279
and not (isinstance(declared_type, UnionType)
281280
and any(isinstance(item, AnyType) for item in declared_type.items))):

test-data/unit/check-unions.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,4 +965,14 @@ def union_test2(x):
965965
return x
966966
else:
967967
return x[0]
968+
969+
def f(): return 0
970+
971+
def union_test3():
972+
# type: () -> int
973+
x = f()
974+
assert x is None
975+
x = f()
976+
return x + 1
977+
968978
[builtins fixtures/isinstancelist.pyi]

0 commit comments

Comments
 (0)