Skip to content

Crash on nested tuple in except handler #345

@JelleZijlstra

Description

@JelleZijlstra

Run flake8-bugbear on this file:

try: x
except (a, (b, c)): pass

And you will get a stack trace that ends with

  File "/Users/jelle/py/venvs/py311/lib/python3.11/site-packages/bugbear.py", line 283, in visit_ExceptHandler
    names = [_to_name_str(e) for e in node.type.elts]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jelle/py/venvs/py311/lib/python3.11/site-packages/bugbear.py", line 283, in <listcomp>
    names = [_to_name_str(e) for e in node.type.elts]
             ^^^^^^^^^^^^^^^
  File "/Users/jelle/py/venvs/py311/lib/python3.11/site-packages/bugbear.py", line 202, in _to_name_str
    return _to_name_str(node.value)
                        ^^^^^^^^^^
AttributeError: 'Tuple' object has no attribute 'value'

Why would you use nested tuples in except handlers, you may ask. I'm not sure, but it's legal syntax so I am testing it in the pyanalyze test suite.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions