diff --git a/mypy/checker.py b/mypy/checker.py index 35d357f8cb6f..8c9b8590b6fc 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1890,7 +1890,7 @@ def infer_variable_type(self, name: Var, lvalue: Lvalue, self.set_inferred_type(name, lvalue, init_type) def infer_partial_type(self, name: Var, lvalue: Lvalue, init_type: Type) -> bool: - if isinstance(init_type, (NoneTyp, UninhabitedType)): + if isinstance(init_type, NoneTyp): partial_type = PartialType(None, name, [init_type]) elif isinstance(init_type, Instance): fullname = init_type.type.fullname() @@ -3344,11 +3344,11 @@ def is_valid_inferred_type(typ: Type) -> bool: invalid. When doing strict Optional checking, only None and types that are incompletely defined (i.e. contain UninhabitedType) are invalid. """ - if is_same_type(typ, NoneTyp()): - # With strict Optional checking, we *may* eventually infer NoneTyp, but - # we only do that if we can't infer a specific Optional type. This - # resolution happens in leave_partial_types when we pop a partial types - # scope. + if isinstance(typ, (NoneTyp, UninhabitedType)): + # With strict Optional checking, we *may* eventually infer NoneTyp when + # the initializer is None, but we only do that if we can't infer a + # specific Optional type. This resolution happens in + # leave_partial_types when we pop a partial types scope. return False return is_valid_inferred_type_component(typ) diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 18814539db6c..8eb9f1856d1e 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -413,7 +413,7 @@ def id(x: T) -> T: return x [out] -[case testUnderspecifiedInferenceResult-skip] +[case testUnderspecifiedInferenceResult] from typing import TypeVar T = TypeVar('T') class A: pass @@ -421,6 +421,7 @@ a = None # type: A def ff() -> None: x = f() # E: Need type annotation for variable + reveal_type(x) g(None) # Ok f() # Ok because not used to infer local variable type @@ -874,9 +875,10 @@ for x in [A()]: b = x # E: Incompatible types in assignment (expression has type "A", variable has type "B") a = x -for y in []: - a = y - reveal_type(y) # E: Revealed type is 'builtins.None' +for y in []: # E: Need type annotation for variable + a = y # E: Cannot determine type of 'y' + reveal_type(y) # E: Revealed type is 'Any' \ + # E: Cannot determine type of 'y' class A: pass class B: pass @@ -920,9 +922,11 @@ for x, y in [[A()]]: a = x a = y -for e, f in [[]]: - reveal_type(e) # E: Revealed type is 'builtins.None' - reveal_type(f) # E: Revealed type is 'builtins.None' +for e, f in [[]]: # E: Need type annotation for variable + reveal_type(e) # E: Revealed type is 'Any' \ + # E: Cannot determine type of 'e' + reveal_type(f) # E: Revealed type is 'Any' \ + # E: Cannot determine type of 'f' class A: pass class B: pass diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 387eabca4764..ab3cd6ea1a6b 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -633,7 +633,7 @@ for x in t: [case testForLoopOverEmptyTuple] import typing t = () -for x in t: pass +for x in t: pass # E: Need type annotation for variable [builtins fixtures/for.pyi] [case testForLoopOverNoneValuedTuple]