Skip to content

B042 has naive definition of exception #538

@GPHemsley-RELX

Description

@GPHemsley-RELX

B042 (introduced in #421 / #512) has a naive way of determining whether a class is an exception:

flake8-bugbear/bugbear.py

Lines 1798 to 1802 in 915edea

def is_exception(s: str):
for ending in "Exception", "Error", "Warning", "ExceptionGroup":
if s.endswith(ending):
return True
return False

It is based solely on the name of the class, which means it returns True even if the class is not an exception.

For example, if I have a context manager class called FailOnException, it will trigger, even though that class is not an exception at all:

from typing import (ContextManager, TypeVar)
T = TypeVar("T")
class FailOnException(ContextManager[T]): pass

print(FailOnException.__mro__)
(<class '__main__.FailOnException'>, <class 'contextlib.AbstractContextManager'>, <class 'abc.ABC'>, <class 'typing.Generic'>, <class 'object'>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions