-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trigger violation on mutable values and function calls for non-dataclass attribute defaults #4053
Comments
Interesting! I initially assumed that the suggestion here was to flag usages of |
I'll note some libraries do re-use/support dataclasses field for there own decorator/class. field just serves as a convenient way to hold some annotations and there are number of dataclass like libraries. I do use internal library that does, from dataclasses import field
@configurable
class Foo:
x = field(default_factory=list) where default_factory list can be safe to use. It's own separate rule to disallow field in non dataclass sounds fine though as if you aren't using alternative dataclass like libraries it makes sense. |
If it's easy, it would be great to allow a configurable list of decorators that allow data class.field, or maybe even any dataclass_transform should be exempted? |
Yes, exactly. |
Though I would settle for the more targeted warning on just dataclass.field if that's preferable! |
Closing in favor of #4390. |
This is a feature request. I see in this PR that there is some recent work in flagging mutable and function call defaults for dataclass attributes. Is there a plan to enable this check for non-dataclass attributes as well? Right now, no Python tooling I know of catches this bug:
The problem is that
dataclass.field
lies about its type.It would be great if we could check for mutable and function call defaults in class attributes because they are just problematic for non-dataclasses as they are for dataclasses. The only difference from the existing implementation would need to be that
dataclasses.field
should only be exempted when used inside a dataclass.I can look into this if the work is not already planned since I think it's a simple change from what's already been done.
Thanks for the great tool!
The text was updated successfully, but these errors were encountered: