Skip to content

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

@floxay

Description

@floxay

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions