8000 Check code after inferred UninhabitedType by ilevkivskyi · Pull Request #4059 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Check code after inferred UninhabitedType #4059

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 2 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Check code after inferred UninhabitedType
  • Loading branch information
ilevkivskyi committed Oct 4, 2017
commit 6982ea6b425c96dd9cda7cde81d9fdceca895602
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def visit_call_expr(self, e: CallExpr, allow_none_return: bool = False) -> Type:
self.check_runtime_protocol_test(e)
if e.callee.fullname == 'builtins.issubclass':
self.check_protocol_issubclass(e)
if isinstance(ret_type, UninhabitedType):
if isinstance(ret_type, UninhabitedType) and not ret_type.ambiguous:
self.chk.binder.unreachable()
if not allow_none_return and isinstance(ret_type, NoneTyp):
self.chk.msg.does_not_return_value(callee_type, e)
Expand Down
1 change: 1 addition & 0 deletions mypy/solve.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def solve_constraints(vars: List[TypeVarId], constraints: List[Constraint],
# No constraints for type variable -- 'UninhabitedType' is the most specific type.
if strict:
candidate = UninhabitedType()
candidate.ambiguous = True
else:
candidate = AnyType(TypeOfAny.special_form)
elif top is None:
Expand Down
1 change: 1 addition & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class UninhabitedType(Type):
can_be_true = False
can_be_false = False
is_noreturn = False # Does this come from a NoReturn? Purely for error messages.
ambiguous = False # Is this a result of inference for a variable without constraints?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mention why we care about this and where this makes a difference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mention why we care about this and where this makes a difference.

I added a short discussion.


def __init__(self, is_noreturn: bool = False, line: int = -1, column: int = -1) -> None:
super().__init__(line, column)
Expand Down
49 changes: 49 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -1942,3 +1942,52 @@ def main() -> None:
reveal_type(b) # E: Revealed type is 'builtins.int'
[builtins fixtures/tuple.pyi]
[out]

[case testDontMarkUnreachableAfterInferenceUninhabited]
from typing import TypeVar
T = TypeVar('T')
def f() -> T: pass

class C:
x = f()
def m(self) -> str:
return 42 # E: Incompatible return value type (got "int", expected "str")

if bool():
f()
1 + '' # E: Unsupported left operand type for + ("int")
[builtins fixtures/list.pyi]
[out]

[case testDontMarkUnreachableAfterInferenceUninhabited2]
# flags: --strict-optional
from typing import TypeVar, Optional
T = TypeVar('T')
def f(x: Optional[T] = None) -> T: pass

class C:
x = f()
def m(self) -> str:
return 42 # E: Incompatible return value type (got "int", expected "str")

if bool():
f()
1 + '' # E: Unsupported left operand type for + ("int")
[builtins fixtures/list.pyi]
[out]

[case testDontMarkUnreachableAfterInferenceUninhabited3]
from typing import TypeVar, List
T = TypeVar('T')
def f(x: List[T]) -> T: pass

class C:
x = f([])
def m(self) -> str:
return 42 # E: Incompatible return value type (got "int", expected "str")

if bool():
f([])
1 + '' # E: Unsupported left operand type for + ("int")
[builtins fixtures/list.pyi]
[out]
0