Skip to content

Commit cd2de7b

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 cd2de7b

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

tests/test_type_eval.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typemap.typing import (
2020
Attrs,
2121
FromUnion,
22+
GetAnnotations,
2223
GetArg,
2324
GetArgs,
2425
GetAttr,
@@ -732,6 +733,10 @@ def test_eval_literal_idempotent_01():
732733
t = nt
733734

734735

736+
def test_is_literal_true_vs_one():
737+
assert eval_typing(Is[Literal[True], Literal[1]]) is False
738+
739+
735740
def test_callable_to_signature():
736741
from typemap.type_eval._eval_operators import _callable_type_to_signature
737742
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)