Skip to content

Commit 610d413

Browse files
committed
remove map_foreign from Mapper
1 parent ca4f336 commit 610d413

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
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-9
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")
@@ -199,13 +203,6 @@ def handle_unsupported_array(self, expr: MappedT,
199203
raise UnsupportedArrayError(
200204
f"{type(self).__name__} cannot handle expressions of type {type(expr)}")
201205

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-
209206
def rec(self, expr: ArrayOrNames, *args: P.args, **kwargs: P.kwargs) -> ResultT:
210207
"""Call the mapper method of *expr* and return the result."""
211208
method: Callable[..., Any] | None
@@ -223,7 +220,9 @@ def rec(self, expr: ArrayOrNames, *args: P.args, **kwargs: P.kwargs) -> ResultT:
223220
else:
224221
return self.handle_unsupported_array(expr, *args, **kwargs)
225222
else:
226-
return cast("ResultT", self.map_foreign(expr, *args, **kwargs))
223+
raise ForeignObjectError(
224+
f"{type(self).__name__} encountered invalid foreign "
225+
f"object: {expr!r}") from None
227226

228227
assert method is not None
229228
return cast("ResultT", method(expr, *args, **kwargs))
@@ -237,7 +236,8 @@ def rec_function_definition(
237236
try:
238237
method = self.map_function_definition # type: ignore[attr-defined]
239238
except AttributeError:
240-
return cast("FunctionResultT", self.map_foreign(expr, *args, **kwargs))
239+
raise ValueError(
240+
f"{type(self).__name__} lacks a mapper method for functions.") from None
241241

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

0 commit comments

Comments
 (0)