Skip to content

Commit

Permalink
Be more liberal in when we broaden a type back to Any
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Sep 17, 2018
1 parent 2d67fc3 commit 42e28ac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
9 changes: 4 additions & 5 deletions mypy/binder.py
Expand Up @@ -271,11 +271,10 @@ def assign_type(self, expr: Expression,
and not restrict_any):
# If x is Any and y is int, after x = y we do not infer that x is int.
# This could be changed.
if not isinstance(type, AnyType):
# We narrowed type from Any in a recent frame (probably an
# isinstance check), but now it is reassigned, so broaden back
# to Any (which is the most recent enclosing type)
self.put(expr, enclosing_type)
# Instead, since we narrowed type from Any in a recent frame (probably an
# isinstance check), but now it is reassigned, we broaden back
# to Any (which is the most recent enclosing type)
self.put(expr, enclosing_type)
elif (isinstance(type, AnyType)
and not (isinstance(declared_type, UnionType)
and any(isinstance(item, AnyType) for item in declared_type.items))):
Expand Down
10 changes: 10 additions & 0 deletions test-data/unit/check-unions.test
Expand Up @@ -965,4 +965,14 @@ def union_test2(x):
return x
else:
return x[0]

def f(): return 0

def union_test3():
# type: () -> int
x = f()
assert x is None
x = f()
return x + 1

[builtins fixtures/isinstancelist.pyi]

0 comments on commit 42e28ac

Please sign in to comment.