RecursionError: maximum recursion depth exceeded for large models
Closed this issue · 4 comments
Hi :)
I ran into the following error. When I try to create a Formula object with more than 246 model terms I get a RecursionError. One can fix this by simply setting the recursion limit to a higher value but I wanted to report this because I think it is potentially unwanted behaviour. This is a minimal example to reproduce the error:
from formulaic import Formula
expr = ""
for i in range(247):
expr += f"x{i} + "
expr = expr[:-3]
f = Formula(expr)
Thanks for reporting this @Osburg ! The threshold seems to be much higher for me (737), but I can reproduce this. The issue is that the AST generated for the formula is evaluated implicitly via recursion. Fixing this will require switching to a topological evaluation of the formula, and slightly less clean code. Nevertheless, you're right... this is something that really ought not to break, so I'll see if I can resolve it in 0.4.0.
The above PR should fix things. My plan is to get it in along with a few other fixes and call it 0.4.0 :).
@Osburg @DavidWalz Sorry for the delay! 0.4.0 has now been released :).
Awesome, works like a charm!