PyCQA / flake8-bugbear

A plugin for Flake8 finding likely bugs and design problems in your program. Contains warnings that don't belong in pyflakes and pycodestyle.
MIT License
1.05k stars 103 forks source link

Avoid another source of deprecation warnings on py312 #394

Closed AlexWaygood closed 1 year ago

AlexWaygood commented 1 year ago

Running flake8 (with flake8-bugbear installed) using Python 3.12 on a file containing this...

class Foo:
    def visit_NameConstant(self):
        ...

...will cause this DeprecationWarning to be emitted:

flake8-bugbear\bugbear.py:1185: DeprecationWarning: ast.NameConstant is deprecated and will be removed in Python 3.14; use ast.Constant instead
  class_type = getattr(ast, class_name, None)

This is because of this line here -- doing getattr(ast, "NameConstant", None) causes a DeprecationWarning to be emitted on Python 3.12:

https://github.com/PyCQA/flake8-bugbear/blob/2763a13e6d415d5a2cbda51d126f601ae8215480/bugbear.py#L1185

I think the simplest thing here is just to catch and silence any deprecation warnings that might arise as a result of this getattr() call. (An alternative strategy could be to hardcode a list of AST node names that we know are deprecated, and avoid passing them to the getattr() call. But we'd have to make sure we kept that up to date -- more AST nodes might be deprecated in future Python versions.)