Skip to content

Fix PartialType-related crash in --strict-optional #1992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -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