Skip to content

Commit 742ae35

Browse files
committed
Make Is checks for Literals check type also
Literal[True] and Literal[1] are unrelated. Same for enums.
1 parent 585191c commit 742ae35

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

tests/test_type_eval.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ def test_eval_literal_idempotent_01():
732732
t = nt
733733

734734

735+
def test_is_literal_true_vs_one():
736+
assert eval_typing(Is[Literal[True], Literal[1]]) is False
737+
738+
735739
def test_callable_to_signature():
736740
from typemap.type_eval._eval_operators import _callable_type_to_signature
737741
from typemap.typing import Param

typemap/type_eval/_subsim.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import typing
22

3-
43
from . import _typing_inspect
54

6-
75
__all__ = ("issubsimilar",)
86

97

@@ -38,11 +36,11 @@ def issubsimilar(lhs: typing.Any, rhs: typing.Any) -> bool:
3836
return issubclass(lhs, rhs)
3937

4038
# literal <:? literal
41-
elif bool(
42-
_typing_inspect.is_literal(lhs) and _typing_inspect.is_literal(rhs)
43-
):
44-
rhs_args = set(typing.get_args(rhs))
45-
return all(lv in rhs_args for lv in typing.get_args(lhs))
39+
elif _typing_inspect.is_literal(lhs) and _typing_inspect.is_literal(rhs):
40+
# We need to check both value and type, since True == 1 but
41+
# Literal[True] should not be a subtype of Literal[1]
42+
rhs_args = {(t, type(t)) for t in typing.get_args(rhs)}
43+
return all((lv, type(lv)) in rhs_args for lv in typing.get_args(lhs))
4644

4745
# XXX: This case is kind of a hack, to support NoLiterals.
4846
elif rhs is typing.Literal:

0 commit comments

Comments
 (0)