Skip to content

Commit f6a2074

Browse files
committed
chore: split up and/or and add cython casts
1 parent 8acdc32 commit f6a2074

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

koerce/patterns.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,8 @@ def __or__(self, other: Pattern) -> AnyOf:
279279
-------
280280
New pattern that matches if either of the patterns match.
281281
"""
282-
if isinstance(self, AnyOf) and isinstance(other, AnyOf):
283-
return AnyOf(*self.inners, *other.inners)
284-
elif isinstance(self, AnyOf):
285-
return AnyOf(*self.inners, other)
286-
elif isinstance(other, AnyOf):
287-
return AnyOf(self, *other.inners)
282+
if isinstance(other, AnyOf):
283+
return AnyOf(self, *cython.cast(AnyOf, other).inners)
288284
else:
289285
return AnyOf(self, other)
290286

@@ -300,12 +296,8 @@ def __and__(self, other: Pattern) -> AllOf:
300296
-------
301297
New pattern that matches if both of the patterns match.
302298
"""
303-
if isinstance(self, AllOf) and isinstance(other, AllOf):
304-
return AllOf(*self.inners, *other.inners)
305-
elif isinstance(self, AllOf):
306-
return AllOf(*self.inners, other)
307-
elif isinstance(other, AllOf):
308-
return AllOf(self, *other.inners)
299+
if isinstance(other, AllOf):
300+
return AllOf(self, *cython.cast(AllOf, other).inners)
309301
else:
310302
return AllOf(self, other)
311303

@@ -883,6 +875,23 @@ def match(self, value, ctx: Context):
883875
pass
884876
raise NoMatchError()
885877

878+
def __or__(self, other: Pattern) -> AnyOf:
879+
"""Syntax sugar for matching either of the patterns.
880+
881+
Parameters
882+
----------
883+
other
884+
The other pattern to match against.
885+
886+
Returns
887+
-------
888+
New pattern that matches if either of the patterns match.
889+
"""
890+
if isinstance(other, AnyOf):
891+
return AnyOf(*self.inners, *cython.cast(AnyOf, other).inners)
892+
else:
893+
return AnyOf(*self.inners, other)
894+
886895

887896
@cython.final
888897
@cython.cclass
@@ -905,6 +914,23 @@ def match(self, value, ctx: Context):
905914
value = inner.match(value, ctx)
906915
return value
907916

917+
def __and__(self, other: Pattern) -> AllOf:
918+
"""Syntax sugar for matching both of the patterns.
919+
920+
Parameters
921+
----------
922+
other
923+
The other pattern to match against.
924+
925+
Returns
926+
-------
927+
New pattern that matches if both of the patterns match.
928+
"""
929+
if isinstance(other, AllOf):
930+
return AllOf(*self.inners, *cython.cast(AllOf, other).inners)
931+
else:
932+
return AllOf(*self.inners, other)
933+
908934

909935
def NoneOf(*args) -> Pattern:
910936
"""Match none of the passed patterns."""

0 commit comments

Comments
 (0)