Skip to content

Commit

Permalink
Allow None to be inferred from context in strict Optional mode
Browse files Browse the repository at this point in the history
Fixes first part of #2230.
  • Loading branch information
ddfisher committed Oct 7, 2016
1 parent 08561f9 commit 12165d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,13 @@ def infer_function_type_arguments_using_context(
# See also github issues #462 and #360.
ret_type = NoneTyp()
args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
# Only substitute non-None and non-erased types.
# Only substitute non-Uninhabited and non-erased types.
new_args = [] # type: List[Type]
for arg in args:
if isinstance(arg, (NoneTyp, UninhabitedType)) or has_erased_component(arg):
if isinstance(arg, UninhabitedType) or has_erased_component(arg):
new_args.append(None)
elif not experiments.STRICT_OPTIONAL and isinstance(arg, NoneTyp):
# Don't substitute None types in non-strict-Optional mode.
new_args.append(None)
else:
new_args.append(arg)
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,11 @@ x + 1
[out]
main:2: note: In module imported here:
tmp/a.py:3: error: Unsupported left operand type for + (some union)

[case testNoneContextInference]
from typing import Dict, List
def f() -> List[None]:
return []
def g() -> Dict[None, None]:
return {}
[builtins fixtures/dict.pyi]

0 comments on commit 12165d9

Please sign in to comment.