Skip to content

Checks Instance and Literal subtypes correctly, refs #11232 #11236

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 3 commits into from
Oct 1, 2021
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
2 changes: 2 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ def visit_instance(self, left: Instance) -> bool:
return True
if isinstance(item, Instance):
return is_named_instance(item, 'builtins.object')
if isinstance(right, LiteralType) and left.last_known_value is not None:
return self._is_subtype(left.last_known_value, right)
if isinstance(right, CallableType):
# Special case: Instance can be a subtype of Callable.
call = find_member('__call__', left, left, is_operator=True)
Expand Down
52 changes: 47 additions & 5 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2687,11 +2687,8 @@ def force2(x: Tuple[Literal[1], Literal[2]]) -> None: pass
reveal_type(a) # N: Revealed type is "Literal[1]?"
reveal_type(b) # N: Revealed type is "Tuple[Literal[1]?, Literal[2]?]"

# TODO: This test seems somewhat broken and might need a rewrite (and a fix somewhere in mypy).
# See https://github.com/python/mypy/issues/7399#issuecomment-554188073 for more context.
force1(reveal_type(a)) # N: Revealed type is "Literal[1]"
force2(reveal_type(b)) # E: Argument 1 to "force2" has incompatible type "Tuple[int, int]"; expected "Tuple[Literal[1], Literal[2]]" \
# N: Revealed type is "Tuple[Literal[1]?, Literal[2]?]"
force1(a) # ok
force2(b) # ok
[builtins fixtures/tuple.pyi]
[out]

Expand Down Expand Up @@ -3304,3 +3301,48 @@ else:
reveal_type(w) # E: Statement is unreachable

[builtins fixtures/bool.pyi]

[case testLiteralAndInstanceSubtyping]
# https://github.com/python/mypy/issues/7399
# https://github.com/python/mypy/issues/11232
from typing import Tuple, Union
from typing_extensions import Literal, Final

x: bool

def f() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]:
if x:
return (True, 5)
else:
return (False, 'oops')

reveal_type(f()) # N: Revealed type is "Union[Tuple[Literal[True], builtins.int], Tuple[Literal[False], builtins.str]]"

def does_work() -> Tuple[Literal[1]]:
x: Final = (1,)
return x

def also_works() -> Tuple[Literal[1]]:
x: Tuple[Literal[1]] = (1,)
return x

def invalid_literal_value() -> Tuple[Literal[1]]:
x: Final = (2,)
return x # E: Incompatible return value type (got "Tuple[int]", expected "Tuple[Literal[1]]")

def invalid_literal_type() -> Tuple[Literal[1]]:
x: Final = (True,)
return x # E: Incompatible return value type (got "Tuple[bool]", expected "Tuple[Literal[1]]")

def incorrect_return1() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]:
if x:
return (False, 5) # E: Incompatible return value type (got "Tuple[bool, int]", expected "Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]")
else:
return (True, 'oops') # E: Incompatible return value type (got "Tuple[bool, str]", expected "Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]")

def incorrect_return2() -> Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]:
if x:
return (bool(), 5) # E: Incompatible return value type (got "Tuple[bool, int]", expected "Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]")
else:
return (bool(), 'oops') # E: Incompatible return value type (got "Tuple[bool, str]", expected "Union[Tuple[Literal[True], int], Tuple[Literal[False], str]]")
[builtins fixtures/bool.pyi]