Repository: PyCQA/flake8-bugbear

Issue:
crash on tuple expansion in try/except block
Running flake8 against the following file produces no report as an exception is raised in bugbear
```python
# dummy.py
grey_list = (ValueError,)
black_list = (TypeError,)

try:
    int("1e3")
except (*grey_list, *black_list):
    print("error caught")
```

crashes with
```
Traceback (most recent call last):
...
  File "/Users/clm/.pyenv/versions/3.8.6/envs/yt_dev/lib/python3.8/site-packages/bugbear.py", line 126, in _to_name_str
    assert isinstance(node, ast.Attribute)
AssertionError
```

For information
```
$ flake8 --version
3.8.4 (flake8-bugbear: 20.11.1, mccabe: 0.6.1, pycodestyle: 2.6.0, pyflakes: 2.2.0) CPython 3.8.6 on Darwin
```

Discussion:

Patch:
diff --git a/bugbear.py b/bugbear.py
index 873fc10..974e5cb 100644
--- a/bugbear.py
+++ b/bugbear.py
@@ -124,8 +124,10 @@ def _to_name_str(node):
     # "pkg.mod.error", handling any depth of attribute accesses.
     if isinstance(node, ast.Name):
         return node.id
-    assert isinstance(node, ast.Attribute)
-    return _to_name_str(node.value) + "." + node.attr
+    try:
+        return _to_name_str(node.value) + "." + node.attr
+    except AttributeError:
+        return _to_name_str(node.value)
 
 
 def _typesafe_issubclass(cls, class_or_tuple):


Test patch:
diff --git a/tests/test_bugbear.py b/tests/test_bugbear.py
index ccce9da..c755fb9 100644
--- a/tests/test_bugbear.py
+++ b/tests/test_bugbear.py
@@ -340,6 +340,20 @@ class TestFuzz(unittest.TestCase):
                     if f.endswith(".py"):
                         BugBearChecker(filename=str(Path(dirname) / f))
 
+    def test_does_not_crash_on_tuple_expansion_in_except_statement(self):
+        # akin to test_does_not_crash_on_any_valid_code
+        # but targets a rare case that's not covered by hypothesmith.from_grammar
+        # see https://github.com/PyCQA/flake8-bugbear/issues/153
+        syntax_tree = ast.parse(
+            "grey_list = (ValueError,)\n"
+            "black_list = (TypeError,)\n"
+            "try:\n"
+            "    int('1e3')\n"
+            "except (*grey_list, *black_list):\n"
+            "     print('error caught')"
+        )
+        BugBearVisitor(filename="<string>", lines=[]).visit(syntax_tree)
+
 
 if __name__ == "__main__":
     unittest.main()