Skip to content

Commit

Permalink
define rec_ary conditionally during type-checking to avoid inflating …
Browse files Browse the repository at this point in the history
…recursion depth
  • Loading branch information
majosm committed Jan 9, 2025
1 parent 119da92 commit 34af8ef
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions pytato/transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# }}}

Expand All @@ -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

# }}}

Expand Down

0 comments on commit 34af8ef

Please sign in to comment.