Skip to content

Complain about inferring UninhabitedType for a variable #4112

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
Oct 13, 2017
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
12 changes: 6 additions & 6 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 11 additions & 7 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,15 @@ def id(x: T) -> T:
return x
[out]

[case testUnderspecifiedInferenceResult-skip]
[case testUnderspecifiedInferenceResult]
from typing import TypeVar
T = TypeVar('T')
class A: pass
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down