Skip to content

RUF009 handles attrs dataclasses with auto_attribs = False incorrectly #14519

@InSyncWithFoo

Description

@InSyncWithFoo

From #14327:

Note that RUF009 can be a bit misleading when auto_attribs=False (or with older attr.s codebases):

import attrs

@attrs.define(auto_attribs=False)
class X:
    a_field: int = attrs.field(default=42)
    not_a_field: list = (lambda: [])()
#                       ^^^^^^^^^^^^^^ RUF009 Do not perform function call in dataclass defaults

Without auto_attribs, not_a_field can't possibly be a dataclass field. In this example, it's an incorrectly typed class variable. Fixing the typing error also fixes the RUF009 warning:

@attrs.define(auto_attribs=False)
class X:
    a_field: int = attrs.field(default=42)
    not_a_field: typing.ClassVar[list] = (lambda: [])()

This is an issue though when using attrs.field wrappers:

def my_field():
    return attrs.field(default=42, metadata={"my_metadata": 42})


@attrs.define(auto_attribs=False)
class X:
    a_field: int = my_field()
#                  ^^^^^^^^^^ RUF009 Do not perform function call `my_field` in dataclass defaults

@pwuertz

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions