Skip to content

Wrap visitor recursion errors in PluginExecutionFailed#560

Merged
cooperlees merged 3 commits into
PyCQA:mainfrom
m-aciek:copilot/wrap-recursionerror-in-flake8-exception
Jun 18, 2026
Merged

Wrap visitor recursion errors in PluginExecutionFailed#560
cooperlees merged 3 commits into
PyCQA:mainfrom
m-aciek:copilot/wrap-recursionerror-in-flake8-exception

Conversation

@m-aciek

@m-aciek m-aciek commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Refs. #295 (comment)

New UX:

$ uvx --with git+https://github.com/m-aciek/flake8-bugbear.git@copilot%2Fwrap-recursionerror-in-flake8-exception flake8
sympy/polys/numberfields/resolvent_lookup.py: "flake8-bugbear" failed during execution due to RecursionError('maximum recursion depth exceeded')
Run flake8 with greater verbosity to see more details

Previous UX:

$ uvx --with flake8-bugbear flake8
[… long stacktrace]
    return visitor(node)                                                                                                                                                                                         
  File "~/.local/share/uv/python/cpython-3.14.0-macos-x86_64-none/lib/python3.14/ast.py", line 516, in generic_visit                                                                            
    self.visit(value)                                                                                                                                                                                            
    ~~~~~~~~~~^^^^^^^                                                                                                                                                                                            
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/bugbear.py", line 455, in visit                                                                                    
    super().visit(node)                                                                                                                                                                                          
    ~~~~~~~~~~~~~^^^^^^                                                                                                                                                                                          
  File "~/.local/share/uv/python/cpython-3.14.0-macos-x86_64-none/lib/python3.14/ast.py", line 506, in visit                                                                                    
    return visitor(node)                                                                                                                                                                                         
  File "~/.local/share/uv/python/cpython-3.14.0-macos-x86_64-none/lib/python3.14/ast.py", line 516, in generic_visit                                                                            
    self.visit(value)                                                                                                                                                                                            
    ~~~~~~~~~~^^^^^^^                                                                                                                                                                                            
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/bugbear.py", line 455, in visit                                                                                    
    super().visit(node)                                                                                                                                                                                          
    ~~~~~~~~~~~~~^^^^^^                                                                                                                                                                                          
  File "~/.local/share/uv/python/cpython-3.14.0-macos-x86_64-none/lib/python3.14/ast.py", line 506, in visit                                                                                    
    return visitor(node)                                                                                                                                                                                         
  File "~/.local/share/uv/python/cpython-3.14.0-macos-x86_64-none/lib/python3.14/ast.py", line 510, in generic_visit                                                                            
    for field, value in iter_fields(node):                                                                                                                                                                       
                        ~~~~~~~~~~~^^^^^^                                                                                                                                                                        
RecursionError: maximum recursion depth exceeded                                                                                                                                                                 
"""                                                                                                                                                                                                              

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/bin/flake8", line 12, in <module>
    sys.exit(main())
             ~~~~^^
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/flake8/main/cli.py", line 23, in main
    app.run(argv)
    ~~~~~~~^^^^^^
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/flake8/main/application.py", line 198, in run
    self._run(argv)
    ~~~~~~~~~^^^^^^
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/flake8/main/application.py", line 187, in _run
    self.run_checks()
    ~~~~~~~~~~~~~~~^^
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/flake8/main/application.py", line 103, in run_checks
    self.file_checker_manager.run()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/flake8/checker.py", line 235, in run
    self.run_parallel()
    ~~~~~~~~~~~~~~~~~^^
  File "~/.cache/uv/archive-v0/DcgALyZzrPwYWWKQ/lib/python3.14/site-packages/flake8/checker.py", line 204, in run_parallel
    self.results = list(pool.imap_unordered(_mp_run, self.filenames))
                   ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/.local/share/uv/python/cpython-3.14.0-macos-x86_64-none/lib/python3.14/multiprocessing/pool.py", line 873, in next
    raise value
RecursionError: maximum recursion depth exceeded

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves flake8-bugbear’s failure UX by converting AST visitor recursion overflows into a flake8.exceptions.PluginExecutionFailed, so Flake8 can present a short “plugin failed” message instead of dumping a long stack trace.

Changes:

  • Wrap exceptions raised during BugBearVisitor.visit() in PluginExecutionFailed to improve Flake8’s user-facing error output.
  • Add a unit test asserting that a RecursionError is wrapped correctly and preserves filename/plugin metadata.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
bugbear.py Wraps visitor execution failures in PluginExecutionFailed so Flake8 can report a concise plugin error.
tests/test_bugbear.py Adds coverage ensuring RecursionError is wrapped with correct metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bugbear.py Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@cooperlees cooperlees left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for this! A clearer error now.

@cooperlees cooperlees merged commit 4ab9681 into PyCQA:main Jun 18, 2026
6 checks passed
@m-aciek m-aciek deleted the copilot/wrap-recursionerror-in-flake8-exception branch June 18, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants