Skip to content

Commit ef43416

Browse files
authored
Fixes bool and Literal[True, False] subtyping, refs #11701 (#11709)
In some places in our code bool was compared with `Union[Literal[True], Literal[False]]`. It was later compared with each union item. And bool is not subtype of `Literal[True]` and `Literal[False]`. Closes #11701 Refs #11705
1 parent 6b6099b commit ef43416

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

mypy/subtypes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ def _is_subtype(left: Type, right: Type,
125125
# of a union of all enum items as literal types. Only do it if
126126
# the previous check didn't succeed, since recombining can be
127127
# expensive.
128-
if not is_subtype_of_item and isinstance(left, Instance) and left.type.is_enum:
128+
# `bool` is a special case, because `bool` is `Literal[True, False]`.
129+
if (not is_subtype_of_item
130+
and isinstance(left, Instance)
131+
and (left.type.is_enum or left.type.fullname == 'builtins.bool')):
129132
right = UnionType(mypy.typeops.try_contracting_literals_in_union(right.items))
130133
is_subtype_of_item = any(is_subtype(orig_left, item,
131134
ignore_type_params=ignore_type_params,

mypy/typeops.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,10 @@ def try_contracting_literals_in_union(types: Sequence[Type]) -> List[ProperType]
759759
Will replace the first instance of the literal with the sum type and
760760
remove all others.
761761
762-
if we call `try_contracting_union(Literal[Color.RED, Color.BLUE, Color.YELLOW])`,
762+
If we call `try_contracting_union(Literal[Color.RED, Color.BLUE, Color.YELLOW])`,
763763
this function will return Color.
764+
765+
We also treat `Literal[True, False]` as `bool`.
764766
"""
765767
proper_types = [get_proper_type(typ) for typ in types]
766768
sum_types: Dict[str, Tuple[Set[Any], List[int]]] = {}

test-data/unit/check-namedtuple.test

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,24 @@ def bad3() -> NamedTuple:
10601060

10611061
[builtins fixtures/tuple.pyi]
10621062
[typing fixtures/typing-namedtuple.pyi]
1063+
1064+
[case testBoolInTuplesRegression]
1065+
# https://github.com/python/mypy/issues/11701
1066+
from typing import NamedTuple, Literal, List, Tuple
1067+
1068+
C = NamedTuple("C", [("x", Literal[True, False])])
1069+
1070+
T = Tuple[Literal[True, False]]
1071+
1072+
# Was error here:
1073+
# Incompatible types in assignment (expression has type "List[C]", variable has type "List[C]")
1074+
x: List[C] = [C(True)]
1075+
1076+
t: T
1077+
1078+
# Was error here:
1079+
# Incompatible types in assignment (expression has type "List[Tuple[bool]]",
1080+
# variable has type "List[Tuple[Union[Literal[True], Literal[False]]]]")
1081+
y: List[T] = [t]
1082+
[builtins fixtures/tuple.pyi]
1083+
[typing fixtures/typing-namedtuple.pyi]

test-data/unit/fixtures/typing-namedtuple.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Generic = 0
33
Any = 0
44
overload = 0
55
Type = 0
6+
Literal = 0
67

78
T_co = TypeVar('T_co', covariant=True)
89
KT = TypeVar('KT')

0 commit comments

Comments
 (0)