diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 48f1f6e0ce62..1524a6528fee 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -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) diff --git a/test-data/unit/check-optional.test b/test-data/unit/check-optional.test index 010ab8f9eef5..e1400620960b 100644 --- a/test-data/unit/check-optional.test +++ b/test-data/unit/check-optional.test @@ -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]