Skip to content

Commit addcb7a

Browse files
authored
Fix _BoolLiteral not being treated as equivalent to Literal. (#72)
1 parent 84ef843 commit addcb7a

3 files changed

Lines changed: 43 additions & 23 deletions

File tree

tests/test_type_eval.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
GetSpecialAttr,
3232
GetType,
3333
GetAnnotations,
34+
IsSubtype,
3435
IsSub,
3536
Iter,
3637
Length,
@@ -1334,7 +1335,7 @@ def test_eval_bool_05():
13341335
assert d == Literal[False]
13351336

13361337

1337-
def test_eval_literal_generic_01():
1338+
def test_eval_bool_literal_01():
13381339
d = eval_typing(_BoolLiteral[True])
13391340
assert d == _BoolLiteral[True]
13401341
d = eval_typing(_BoolLiteral[False])
@@ -1349,7 +1350,7 @@ def test_eval_literal_generic_01():
13491350
assert d == _BoolLiteral[False]
13501351

13511352

1352-
def test_eval_literal_generic_02():
1353+
def test_eval_bool_literal_02():
13531354
d = eval_typing(not _BoolLiteral[True])
13541355
assert d == _BoolLiteral[False]
13551356

@@ -1359,7 +1360,7 @@ def test_eval_literal_generic_02():
13591360
assert d == _BoolLiteral[True]
13601361

13611362

1362-
def test_eval_literal_generic_03():
1363+
def test_eval_bool_literal_03():
13631364
d = eval_typing(AndLiteralGeneric[_BoolLiteral[True], _BoolLiteral[True]])
13641365
assert d == _BoolLiteral[True]
13651366
d = eval_typing(AndLiteralGeneric[_BoolLiteral[True], _BoolLiteral[False]])
@@ -1370,7 +1371,7 @@ def test_eval_literal_generic_03():
13701371
assert d == _BoolLiteral[False]
13711372

13721373

1373-
def test_eval_literal_generic_04():
1374+
def test_eval_bool_literal_04():
13741375
d = eval_typing(OrLiteralGeneric[_BoolLiteral[True], _BoolLiteral[True]])
13751376
assert d == _BoolLiteral[True]
13761377
d = eval_typing(OrLiteralGeneric[_BoolLiteral[True], _BoolLiteral[False]])
@@ -1381,21 +1382,48 @@ def test_eval_literal_generic_04():
13811382
assert d == _BoolLiteral[False]
13821383

13831384

1384-
def test_eval_literal_generic_05():
1385+
def test_eval_bool_literal_05():
13851386
d = eval_typing(LiteralGenericToLiteral[_BoolLiteral[True]])
13861387
assert d == Literal[True]
13871388
d = eval_typing(LiteralGenericToLiteral[_BoolLiteral[False]])
13881389
assert d == Literal[False]
13891390

13901391

1391-
def test_eval_literal_generic_06():
1392+
def test_eval_bool_literal_06():
13921393
d = eval_typing(NotLiteralGenericToLiteral[_BoolLiteral[True]])
13931394
assert d == Literal[False]
13941395
d = eval_typing(NotLiteralGenericToLiteral[_BoolLiteral[False]])
13951396
assert d == Literal[True]
13961397

13971398

1398-
def test_eval_literal_generic_error_01():
1399+
def test_eval_bool_literal_07():
1400+
d = eval_typing(IsSub[_BoolLiteral[True], Literal[True]])
1401+
assert d == _BoolLiteral[True]
1402+
d = eval_typing(IsSub[_BoolLiteral[False], Literal[False]])
1403+
assert d == _BoolLiteral[True]
1404+
1405+
d = eval_typing(IsSub[Literal[True], _BoolLiteral[True]])
1406+
assert d == _BoolLiteral[True]
1407+
d = eval_typing(IsSub[Literal[False], _BoolLiteral[False]])
1408+
assert d == _BoolLiteral[True]
1409+
1410+
d = eval_typing(IsSubtype[_BoolLiteral[True], Literal[True]])
1411+
assert d == _BoolLiteral[True]
1412+
d = eval_typing(IsSubtype[_BoolLiteral[False], Literal[False]])
1413+
assert d == _BoolLiteral[True]
1414+
1415+
d = eval_typing(IsSubtype[Literal[True], _BoolLiteral[True]])
1416+
assert d == _BoolLiteral[True]
1417+
d = eval_typing(IsSubtype[Literal[False], _BoolLiteral[False]])
1418+
assert d == _BoolLiteral[True]
1419+
1420+
d = eval_typing(Matches[_BoolLiteral[True], Literal[True]])
1421+
assert d == _BoolLiteral[True]
1422+
d = eval_typing(Matches[_BoolLiteral[False], Literal[False]])
1423+
assert d == _BoolLiteral[True]
1424+
1425+
1426+
def test_eval_bool_literal_error_01():
13991427
with pytest.raises(TypeError, match="Expected literal type, got 'int'"):
14001428
eval_typing(_BoolLiteral[int])
14011429

typemap/type_eval/_eval_operators.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,6 @@ def _eval_Bool(tp, *, ctx):
275275
return _eval_bool_tp(tp, ctx)
276276

277277

278-
@type_eval.register_evaluator(_BoolLiteral)
279-
@_lift_evaluated
280-
def _eval_BoolLiteral(tp, *, ctx):
281-
from typemap.typing import _BoolLiteralGenericAlias
282-
283-
if isinstance(tp, type):
284-
raise TypeError(f"Expected literal type, got '{tp.__name__}'")
285-
286-
# If already wrapped, just return it
287-
if isinstance(tp, _BoolLiteralGenericAlias):
288-
return tp
289-
290-
return _BoolLiteral[tp]
291-
292-
293278
##################################################################
294279

295280

typemap/typing.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,11 @@ def __bool__(self):
307307

308308
@_SpecialForm
309309
def _BoolLiteral(self, tp):
310-
return _BoolLiteralGenericAlias(self, tp)
310+
if isinstance(tp, type):
311+
raise TypeError(f"Expected literal type, got '{tp.__name__}'")
312+
313+
# If already wrapped, just return it
314+
if isinstance(tp, _BoolLiteralGenericAlias):
315+
return tp
316+
317+
return _BoolLiteralGenericAlias(Literal, tp)

0 commit comments

Comments
 (0)