flake8-bugbear
flake8-bugbear copied to clipboard
B903 reword message when a subclass is involved
The current B903 message reads:
B903 Data class should either be immutable or use slots to save memory. Use collections.namedtuple to generate an immutable class, or enumerate the attributes in a slot declaration in the class to leave attributes mutable.
However, this lint message is not totally correct if the data class in question inherits from another class; e.g., Exception; in this case, use of namedtuple is inappropriate (actually, I'm not sure if there's any other way to do it.)
We ran into the very same issue.
Yes, this is idiomatic python, which means we had to disable this check every time we wrote
class MyError(ValueError):
"""MyError is raised when bla bla bla..."""
So this check was added before Data Classes existed. What's a proposed solution here? Want to check for this inheritance and disable the error if that's found? We would need to clearly explain it in the documentation.
I think that's the most sensible option. It's not perfect, but you'd still catch simple dataclasses and leave more advanced types alone.
Since you can't inspect the MRO in the syntax tree without a lot of effort, it's not practical to do anything more advanced
Want to check for this inheritance and disable the error if that's found? We would need to clearly explain it in the documentation.
Checking inheritance would make sense. In my case I came across a
class MockStripeObj(dict):
def __init__(self, param):
self.id = param
self.type = param
which triggered this opinionated warning and, like above, I’m silencing it as a false positive. If there were no base class(es) (i.e. implicit object
) then the warning would be valid.
We would need to clearly explain it in the documentation.
Yup. Probably would make sense to mention something like, “Is this meant to be a dataclass?” or some such?
I'm also hitting this on Exception classes
super in the init shuts it up...