Skip to content

Commit 5d4b0e2

Browse files
committed
remove map_foreign from Mapper
1 parent ca4f336 commit 5d4b0e2

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

pytato/stringifier.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
IndexLambda,
4040
ReductionDescriptor,
4141
)
42-
from pytato.transform import Mapper
42+
from pytato.transform import ForeignObjectError, Mapper
4343

4444

4545
if TYPE_CHECKING:
@@ -77,7 +77,10 @@ def rec(self, expr: Any, depth: int) -> str:
7777
try:
7878
return self._cache[cache_key]
7979
except KeyError:
80-
result = super().rec(expr, depth)
80+
try:
81+
result = super().rec(expr, depth)
82+
except ForeignObjectError:
83+
result = self.map_foreign(expr, depth)
8184
self._cache[cache_key] = result
8285
return result
8386

pytato/transform/__init__.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ class UnsupportedArrayError(ValueError):
161161
pass
162162

163163

164+
class ForeignObjectError(ValueError):
165+
pass
166+
167+
164168
# {{{ mapper base class
165169

166170
ResultT = TypeVar("ResultT")
@@ -185,7 +189,6 @@ class Mapper(Generic[ResultT, FunctionResultT, P]):
185189
if this is not desired.
186190
187191
.. automethod:: handle_unsupported_array
188-
.. automethod:: map_foreign
189192
.. automethod:: rec
190193
.. automethod:: __call__
191194
"""
@@ -199,13 +202,6 @@ def handle_unsupported_array(self, expr: MappedT,
199202
raise UnsupportedArrayError(
200203
f"{type(self).__name__} cannot handle expressions of type {type(expr)}")
201204

202-
def map_foreign(self, expr: Any, *args: P.args, **kwargs: P.kwargs) -> Any:
203-
"""Mapper method that is invoked for an object of class for which a
204-
mapper method does not exist in this mapper.
205-
"""
206-
raise ValueError(
207-
f"{type(self).__name__} encountered invalid foreign object: {expr!r}")
208-
209205
def rec(self, expr: ArrayOrNames, *args: P.args, **kwargs: P.kwargs) -> ResultT:
210206
"""Call the mapper method of *expr* and return the result."""
211207
method: Callable[..., Any] | None
@@ -223,7 +219,9 @@ def rec(self, expr: ArrayOrNames, *args: P.args, **kwargs: P.kwargs) -> ResultT:
223219
else:
224220
return self.handle_unsupported_array(expr, *args, **kwargs)
225221
else:
226-
return cast("ResultT", self.map_foreign(expr, *args, **kwargs))
222+
raise ForeignObjectError(
223+
f"{type(self).__name__} encountered invalid foreign "
224+
f"object: {expr!r}") from None
227225

228226
assert method is not None
229227
return cast("ResultT", method(expr, *args, **kwargs))
@@ -237,7 +235,8 @@ def rec_function_definition(
237235
try:
238236
method = self.map_function_definition # type: ignore[attr-defined]
239237
except AttributeError:
240-
return cast("FunctionResultT", self.map_foreign(expr, *args, **kwargs))
238+
raise ValueError(
239+
f"{type(self).__name__} lacks a mapper method for functions.") from None
241240

242241
assert method is not None
243242
return cast("FunctionResultT", method(expr, *args, **kwargs))

0 commit comments

Comments
 (0)