Skip to content

Commit 0d651cd

Browse files
ddfisherJukkaL
authored andcommitted
Allow None to be inferred from context in strict Optional mode (#2233)
Fixes first part of #2230.
1 parent 571f425 commit 0d651cd

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

mypy/checkexpr.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,13 @@ def infer_function_type_arguments_using_context(
460460
# See also github issues #462 and #360.
461461
ret_type = NoneTyp()
462462
args = infer_type_arguments(callable.type_var_ids(), ret_type, erased_ctx)
463-
# Only substitute non-None and non-erased types.
463+
# Only substitute non-Uninhabited and non-erased types.
464464
new_args = [] # type: List[Type]
465465
for arg in args:
466-
if isinstance(arg, (NoneTyp, UninhabitedType)) or has_erased_component(arg):
466+
if isinstance(arg, UninhabitedType) or has_erased_component(arg):
467+
new_args.append(None)
468+
elif not experiments.STRICT_OPTIONAL and isinstance(arg, NoneTyp):
469+
# Don't substitute None types in non-strict-Optional mode.
467470
new_args.append(None)
468471
else:
469472
new_args.append(arg)

test-data/unit/check-optional.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,11 @@ x + 1
428428
[out]
429429
main:2: note: In module imported here:
430430
tmp/a.py:3: error: Unsupported left operand type for + (some union)
431+
432+
[case testNoneContextInference]
433+
from typing import Dict, List
434+
def f() -> List[None]:
435+
return []
436+
def g() -> Dict[None, None]:
437+
return {}
438+
[builtins fixtures/dict.pyi]

0 commit comments

Comments
 (0)