From 0d1498fa7add96fb5a9f537af28de7fb9089718f Mon Sep 17 00:00:00 2001 From: Matthew Smith Date: Thu, 9 Jan 2025 10:48:42 -0600 Subject: [PATCH] define rec_ary conditionally during type-checking to avoid inflating recursion depth --- pytato/transform/__init__.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/pytato/transform/__init__.py b/pytato/transform/__init__.py index 4a0c9beef..6022748a6 100644 --- a/pytato/transform/__init__.py +++ b/pytato/transform/__init__.py @@ -321,10 +321,16 @@ class TransformMapper(CachedMapper[ArrayOrNames, FunctionDefinition, []]): implement default mapper methods; for that, see :class:`CopyMapper`. """ - def rec_ary(self, expr: Array) -> Array: - res = self.rec(expr) - assert isinstance(res, Array) - return res + # Define conditionally to avoid inflating recursion depth + if TYPE_CHECKING: + def rec_ary(self, expr: Array) -> Array: + res = self.rec(expr) + assert isinstance(res, Array) + return res + else: + @property + def rec_ary(self): + return self.rec # }}} @@ -341,10 +347,16 @@ class TransformMapperWithExtraArgs( The logic in :class:`TransformMapper` purposely does not take the extra arguments to keep the cost of its each call frame low. """ - def rec_ary(self, expr: Array, *args: P.args, **kwargs: P.kwargs) -> Array: - res = self.rec(expr, *args, **kwargs) - assert isinstance(res, Array) - return res + # Define conditionally to avoid inflating recursion depth + if TYPE_CHECKING: + def rec_ary(self, expr: Array, *args: P.args, **kwargs: P.kwargs) -> Array: + res = self.rec(expr, *args, **kwargs) + assert isinstance(res, Array) + return res + else: + @property + def rec_ary(self): + return self.rec # }}}