"maximum recursion depth exceeded while calling a Python object" while calling long chain of methods
When multiple methods are chained on the object together (~140), Bugbear will cause maximum recursion depth exceeded while calling a Python object
Steps to reproduce:
- Windows, Python 3.8, Bugbear 22.9.23
- Download and rename chain_method.txt to chain_method.py
pip install flake8 flake8-bugbear
flake8 .
Result:
Traceback (most recent call last):
File "c:\program files\python38\lib\runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "c:\program files\python38\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Program Files\Python38\Scripts\flake8.exe\__main__.py", line 7, in <module>
File "c:\program files\python38\lib\site-packages\flake8\main\cli.py", line 22, in main
app.run(argv)
File "c:\program files\python38\lib\site-packages\flake8\main\application.py", line 375, in run
self._run(argv)
File "c:\program files\python38\lib\site-packages\flake8\main\application.py", line 364, in _run
self.run_checks()
File "c:\program files\python38\lib\site-packages\flake8\main\application.py", line 271, in run_checks
self.file_checker_manager.run()
File "c:\program files\python38\lib\site-packages\flake8\checker.py", line 311, in run
self.run_serial()
File "c:\program files\python38\lib\site-packages\flake8\checker.py", line 295, in run_serial
checker.run_checks()
File "c:\program files\python38\lib\site-packages\flake8\checker.py", line 597, in run_checks
self.run_ast_checks()
File "c:\program files\python38\lib\site-packages\flake8\checker.py", line 500, in run_ast_checks
for (line_number, offset, text, _) in runner:
File "c:\program files\python38\lib\site-packages\bugbear.py", line 60, in run
visitor.visit(self.tree)
File "c:\program files\python38\lib\site-packages\bugbear.py", line 268, in visit
super().visit(node)
File "c:\program files\python38\lib\ast.py", line 371, in visit
return visitor(node)
File "c:\program files\python38\lib\ast.py", line 379, in generic_visit
self.visit(item)
File "c:\program files\python38\lib\site-packages\bugbear.py", line 268, in visit
super().visit(node)
File "c:\program files\python38\lib\ast.py", line 371, in visit
return visitor(node)
File "c:\program files\python38\lib\ast.py", line 381, in generic_visit
self.visit(value)
File "c:\program files\python38\lib\site-packages\bugbear.py", line 268, in visit
super().visit(node)
File "c:\program files\python38\lib\ast.py", line 371, in visit
return visitor(node)
File "c:\program files\python38\lib\site-packages\bugbear.py", line 357, in visit_Call
self.generic_visit(node)
File "c:\program files\python38\lib\ast.py", line 381, in generic_visit
self.visit(value)
File "c:\program files\python38\lib\site-packages\bugbear.py", line 268, in visit
This continues until error is reached:
RecursionError: maximum recursion depth exceeded while calling a Python object
Use case: We use such long chains to describe internal schema of DB
So what do you actually want here to fix it? I guess we could add a config parameter to set sys.setrecursionlimit: https://docs.python.org/3/library/sys.html#sys.setrecursionlimit
You could then set this for your deep recursive code ...
Thank you, I was not aware of this setting. Not sure then if bugbear would be the best place. Maybe flake8 repo would be a place to ask for command line parameter, to increase it - would be more efficient , as in theory would apply to any plugin (?)
Agree. I only suggested config as we (bugbear plug-in) have no way to change the CLI but I think we can get at the config easier (I’ve never tried).
Yes, this could occur on any plug-in depending how it works. We are AST based so your large files cause a lot of recursion with our parsing. You could also, be hilighting a bug, but I don’t think so due to you claiming very large files.
It’s definitely a better solution to ask flake8 itself here how they want to handle it.
Thank you. Well maybe someone will also encounter this in the future. For now we divided those long blocks of chained methods. Closing then.