@@ -161,6 +161,10 @@ class UnsupportedArrayError(ValueError):
161
161
pass
162
162
163
163
164
+ class ForeignObjectError (ValueError ):
165
+ pass
166
+
167
+
164
168
# {{{ mapper base class
165
169
166
170
ResultT = TypeVar ("ResultT" )
@@ -185,7 +189,6 @@ class Mapper(Generic[ResultT, FunctionResultT, P]):
185
189
if this is not desired.
186
190
187
191
.. automethod:: handle_unsupported_array
188
- .. automethod:: map_foreign
189
192
.. automethod:: rec
190
193
.. automethod:: __call__
191
194
"""
@@ -199,13 +202,6 @@ def handle_unsupported_array(self, expr: MappedT,
199
202
raise UnsupportedArrayError (
200
203
f"{ type (self ).__name__ } cannot handle expressions of type { type (expr )} " )
201
204
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
-
209
205
def rec (self , expr : ArrayOrNames , * args : P .args , ** kwargs : P .kwargs ) -> ResultT :
210
206
"""Call the mapper method of *expr* and return the result."""
211
207
method : Callable [..., Any ] | None
@@ -223,7 +219,9 @@ def rec(self, expr: ArrayOrNames, *args: P.args, **kwargs: P.kwargs) -> ResultT:
223
219
else :
224
220
return self .handle_unsupported_array (expr , * args , ** kwargs )
225
221
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
227
225
228
226
assert method is not None
229
227
return cast ("ResultT" , method (expr , * args , ** kwargs ))
@@ -237,7 +235,8 @@ def rec_function_definition(
237
235
try :
238
236
method = self .map_function_definition # type: ignore[attr-defined]
239
237
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
241
240
242
241
assert method is not None
243
242
return cast ("FunctionResultT" , method (expr , * args , ** kwargs ))
0 commit comments