Skip to content

Commit 34ab294

Browse files
committed
Add Matches.
1 parent fd58e78 commit 34ab294

3 files changed

Lines changed: 108 additions & 0 deletions

File tree

tests/test_type_eval.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
IsSub,
3535
Iter,
3636
Length,
37+
Matches,
3738
Member,
3839
Members,
3940
NewProtocol,
@@ -1000,6 +1001,99 @@ def test_never_is():
10001001
assert d == _LiteralGeneric[True]
10011002

10021003

1004+
def test_matches_01():
1005+
d = eval_typing(Matches[int, int])
1006+
assert d == _LiteralGeneric[True]
1007+
1008+
d = eval_typing(Matches[int, str])
1009+
assert d == _LiteralGeneric[False]
1010+
1011+
d = eval_typing(Matches[str, int])
1012+
assert d == _LiteralGeneric[False]
1013+
1014+
1015+
def test_matches_02():
1016+
class A:
1017+
pass
1018+
1019+
class B(A):
1020+
pass
1021+
1022+
class C(B):
1023+
pass
1024+
1025+
class D(A):
1026+
pass
1027+
1028+
class X:
1029+
pass
1030+
1031+
d = eval_typing(Matches[A, A])
1032+
assert d == _LiteralGeneric[True]
1033+
1034+
d = eval_typing(Matches[A, B])
1035+
assert d == _LiteralGeneric[False]
1036+
d = eval_typing(Matches[B, A])
1037+
assert d == _LiteralGeneric[False]
1038+
d = eval_typing(Matches[B, C])
1039+
assert d == _LiteralGeneric[False]
1040+
d = eval_typing(Matches[C, B])
1041+
assert d == _LiteralGeneric[False]
1042+
d = eval_typing(Matches[C, D])
1043+
assert d == _LiteralGeneric[False]
1044+
d = eval_typing(Matches[D, C])
1045+
assert d == _LiteralGeneric[False]
1046+
1047+
d = eval_typing(Matches[A, X])
1048+
assert d == _LiteralGeneric[False]
1049+
1050+
1051+
def test_matches_03():
1052+
class A[T]:
1053+
pass
1054+
1055+
class B[T](A[T]):
1056+
pass
1057+
1058+
class C(B[int]):
1059+
pass
1060+
1061+
class D(A[str]):
1062+
pass
1063+
1064+
class X:
1065+
pass
1066+
1067+
d = eval_typing(Matches[A[int], A[int]])
1068+
assert d == _LiteralGeneric[True]
1069+
d = eval_typing(Matches[A[int], A[str]])
1070+
assert d == _LiteralGeneric[True]
1071+
1072+
d = eval_typing(Matches[A[int], B[int]])
1073+
assert d == _LiteralGeneric[False]
1074+
d = eval_typing(Matches[B[int], A[int]])
1075+
assert d == _LiteralGeneric[False]
1076+
d = eval_typing(Matches[A[int], B[str]])
1077+
assert d == _LiteralGeneric[False]
1078+
d = eval_typing(Matches[B[str], A[int]])
1079+
assert d == _LiteralGeneric[False]
1080+
d = eval_typing(Matches[B[int], C])
1081+
assert d == _LiteralGeneric[False]
1082+
d = eval_typing(Matches[C, B[int]])
1083+
assert d == _LiteralGeneric[False]
1084+
d = eval_typing(Matches[B[str], C])
1085+
assert d == _LiteralGeneric[False]
1086+
d = eval_typing(Matches[C, B[str]])
1087+
assert d == _LiteralGeneric[False]
1088+
d = eval_typing(Matches[C, D])
1089+
assert d == _LiteralGeneric[False]
1090+
d = eval_typing(Matches[D, C])
1091+
assert d == _LiteralGeneric[False]
1092+
1093+
d = eval_typing(Matches[A[int], X])
1094+
assert d == _LiteralGeneric[False]
1095+
1096+
10031097
def test_eval_iter_01():
10041098
d = eval_typing(Iter[tuple[int, str]])
10051099
assert tuple(d) == (int, str)

typemap/type_eval/_eval_operators.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
Iter,
3636
Length,
3737
Lowercase,
38+
Matches,
3839
Member,
3940
Members,
4041
NewProtocol,
@@ -246,6 +247,14 @@ def _eval_IsSubSimilar(lhs, rhs, *, ctx):
246247
return _LiteralGeneric[type_eval.issubsimilar(lhs, rhs)]
247248

248249

250+
@type_eval.register_evaluator(Matches)
251+
@_lift_evaluated
252+
def _eval_Matches(lhs, rhs, *, ctx):
253+
return _LiteralGeneric[
254+
type_eval.issubsimilar(lhs, rhs) and type_eval.issubsimilar(rhs, lhs)
255+
]
256+
257+
249258
def _eval_bool_tp(tp):
250259
if _typing_inspect.is_generic_alias(tp):
251260
if tp.__origin__ is typing.Literal:

typemap/typing.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def IsSubSimilar(self, tps):
196196
return _BoolGenericAlias(self, tps)
197197

198198

199+
@_SpecialForm
200+
def Matches(self, tps):
201+
return _BoolGenericAlias(self, tps)
202+
203+
199204
IsSub = IsSubSimilar
200205

201206

0 commit comments

Comments
 (0)