Skip to content
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

False RUF012 on empty mutable collections as default values in msgspec.Structs #7785

Closed
floxay opened this issue Oct 3, 2023 · 1 comment · Fixed by #7786
Closed

False RUF012 on empty mutable collections as default values in msgspec.Structs #7785

floxay opened this issue Oct 3, 2023 · 1 comment · Fixed by #7786
Labels
bug Something isn't working

Comments

@floxay
Copy link

floxay commented Oct 3, 2023

msgspec.Structs allow empty mutable collections as default values, however Ruff currently warns about these.
Docs:

Builtin empty mutable collections ([], {}, set(), and bytearray()) may be used as default values (as in c above). Since defaults of these types are so common, these are “syntactic sugar” for specifying the corresponding default_factory (to avoid accidental sharing of mutable values). A default of [] is identical to a default of field(default_factory=list), with a new list instance used each time.

Ruff version: 0.0.292

MRE:

import msgspec

class Example(msgspec.Struct):
    c: list[int] = []  # Mutable class attributes should be annotated with `typing.ClassVar` Ruff(RUF012)
@charliermarsh
Copy link
Member

Thanks, will fix.

@charliermarsh charliermarsh added the bug Something isn't working label Oct 3, 2023
charliermarsh added a commit that referenced this issue Oct 3, 2023
## Summary

The carve-out we have in `RUF012` for Pydantic classes also applies to
`msgspec.Struct`.

Closes #7785.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants