Skip to content
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

Fixes bool and Literal[True, False] subtyping, refs #11701 #11709

Merged
merged 2 commits into from
Dec 14, 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
5 changes: 4 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ def _is_subtype(left: Type, right: Type,
# of a union of all enum items as literal types. Only do it if
# the previous check didn't succeed, since recombining can be
# expensive.
if not is_subtype_of_item and isinstance(left, Instance) and left.type.is_enum:
# `bool` is a special case, because `bool` is `Literal[True, False]`.
if (not is_subtype_of_item
and isinstance(left, Instance)
and (left.type.is_enum or left.type.fullname == 'builtins.bool')):
right = UnionType(mypy.typeops.try_contracting_literals_in_union(right.items))
is_subtype_of_item = any(is_subtype(orig_left, item,
ignore_type_params=ignore_type_params,
Expand Down
4 changes: 3 additions & 1 deletion mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,10 @@ def try_contracting_literals_in_union(types: Sequence[Type]) -> List[ProperType]
Will replace the first instance of the literal with the sum type and
remove all others.

if we call `try_contracting_union(Literal[Color.RED, Color.BLUE, Color.YELLOW])`,
If we call `try_contracting_union(Literal[Color.RED, Color.BLUE, Color.YELLOW])`,
this function will return Color.

We also treat `Literal[True, False]` as `bool`.
"""
proper_types = [get_proper_type(typ) for typ in types]
sum_types: Dict[str, Tuple[Set[Any], List[int]]] = {}
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,24 @@ def bad3() -> NamedTuple:

[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]

[case testBoolInTuplesRegression]
# https://github.com/python/mypy/issues/11701
from typing import NamedTuple, Literal, List, Tuple

C = NamedTuple("C", [("x", Literal[True, False])])

T = Tuple[Literal[True, False]]

# Was error here:
# Incompatible types in assignment (expression has type "List[C]", variable has type "List[C]")
x: List[C] = [C(True)]

t: T

# Was error here:
# Incompatible types in assignment (expression has type "List[Tuple[bool]]",
# variable has type "List[Tuple[Union[Literal[True], Literal[False]]]]")
y: List[T] = [t]
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-namedtuple.pyi]
1 change: 1 addition & 0 deletions test-data/unit/fixtures/typing-namedtuple.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Generic = 0
Any = 0
overload = 0
Type = 0
Literal = 0

T_co = TypeVar('T_co', covariant=True)
KT = TypeVar('KT')
Expand Down