From 9959f196cb7363fab2141ad8cec3d7baf714fbe8 Mon Sep 17 00:00:00 2001 From: David Fisher Date: Fri, 5 Aug 2016 15:18:04 -0700 Subject: [PATCH] Fix PartialType-related crash in --strict-optional --- mypy/checker.py | 3 ++- mypy/checkexpr.py | 6 ++++-- test-data/unit/check-optional.test | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/mypy/checker.py b/mypy/checker.py index c8db99f596d3..00cc7b198357 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -2247,7 +2247,8 @@ def leave_partial_types(self) -> None: partial_types = self.partial_types.pop() if not self.current_node_deferred: for var, context in partial_types.items(): - if experiments.STRICT_OPTIONAL and cast(PartialType, var.type).type is None: + if (experiments.STRICT_OPTIONAL and + isinstance(var.type, PartialType) and var.type.type is None): # None partial type: assume variable is intended to have type None var.type = NoneTyp() else: diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 62835dda6a88..f412d95283a6 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -157,8 +157,10 @@ def try_infer_partial_type(self, e: CallExpr) -> None: var = cast(Var, e.callee.expr.node) partial_types = self.chk.find_partial_types(var) if partial_types is not None and not self.chk.current_node_deferred: - partial_type = cast(PartialType, var.type) - if partial_type is None or partial_type.type is None: + partial_type = var.type + if (partial_type is None or + not isinstance(partial_type, PartialType) or + partial_type.type is None): # A partial None type -> can't infer anything. return typename = partial_type.type.fullname() diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 75f1f1220291..2d643fea4648 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -315,3 +315,18 @@ x = f # type: Callable[[], None] [case testOptionalCallable] from typing import Callable, Optional T = Optional[Callable[..., None]] + +[case testAnyTypeInPartialTypeList] +# options: check_untyped_defs +def f(): ... + +def lookup_field(name, obj): + try: + pass + except: + attr = f() + else: + attr = None +[out] +main: note: In function "lookup_field": +main:10: error: Need type annotation for variable