From 42e28aca0d3fdb3bcb8826eadc62c80b44c05aad Mon Sep 17 00:00:00 2001 From: "Michael J. Sullivan" Date: Mon, 17 Sep 2018 15:22:44 -0700 Subject: [PATCH] Be more liberal in when we broaden a type back to Any --- mypy/binder.py | 9 ++++----- test-data/unit/check-unions.test | 10 ++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/mypy/binder.py b/mypy/binder.py index f943621aef2d..e987d49f922d 100644 --- a/mypy/binder.py +++ b/mypy/binder.py @@ -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))): diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index 557f278bad9a..0a34df3dcacc 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -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]