diff options
author | Devin J. Pohly <djpohly@gmail.com> | 2022-07-04 19:45:53 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-04 19:45:53 -0500 |
commit | d8af08511bbfd9e441ac6910c96afd3a3ebf2a34 (patch) | |
tree | 8caa4172112d78ab186dd971babe4d33616dacac | |
parent | 79d21db42c1385b94bb5b686d1315077fc822f22 (diff) | |
download | pyparsing-git-d8af08511bbfd9e441ac6910c96afd3a3ebf2a34.tar.gz |
Return NotImplemented for unsupported operations (#425)
Fixes #424.
-rw-r--r-- | pyparsing/core.py | 48 |
1 files changed, 12 insertions, 36 deletions
diff --git a/pyparsing/core.py b/pyparsing/core.py index 3a332e9..fd5889a 100644 --- a/pyparsing/core.py +++ b/pyparsing/core.py @@ -1378,9 +1378,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return And([self, other]) def __radd__(self, other) -> "ParserElement": @@ -1393,9 +1391,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return other + self def __sub__(self, other) -> "ParserElement": @@ -1405,9 +1401,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return self + And._ErrorStop() + other def __rsub__(self, other) -> "ParserElement": @@ -1417,9 +1411,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return other - self def __mul__(self, other) -> "ParserElement": @@ -1466,13 +1458,9 @@ class ParserElement(ABC): minElements, optElements = other optElements -= minElements else: - raise TypeError( - f"cannot multiply ParserElement and ({','.join(type(item).__name__ for item in other)}) objects" - ) + return NotImplemented else: - raise TypeError( - f"cannot multiply ParserElement and {type(other).__name__} objects" - ) + return NotImplemented if minElements < 0: raise ValueError("cannot multiply ParserElement by negative value") @@ -1518,9 +1506,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return MatchFirst([self, other]) def __ror__(self, other) -> "ParserElement": @@ -1530,9 +1516,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return other | self def __xor__(self, other) -> "ParserElement": @@ -1542,9 +1526,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return Or([self, other]) def __rxor__(self, other) -> "ParserElement": @@ -1554,9 +1536,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return other ^ self def __and__(self, other) -> "ParserElement": @@ -1566,9 +1546,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return Each([self, other]) def __rand__(self, other) -> "ParserElement": @@ -1578,9 +1556,7 @@ class ParserElement(ABC): if isinstance(other, str_type): other = self._literalStringClass(other) if not isinstance(other, ParserElement): - raise TypeError( - f"Cannot combine element of type {type(other).__name__} with ParserElement" - ) + return NotImplemented return other & self def __invert__(self) -> "ParserElement": |