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

RUF009 handles attrs dataclasses with auto_attribs = False incorrectly #14519

Open
InSyncWithFoo opened this issue Nov 22, 2024 · 0 comments · May be fixed by #14520
Open

RUF009 handles attrs dataclasses with auto_attribs = False incorrectly #14519

InSyncWithFoo opened this issue Nov 22, 2024 · 0 comments · May be fixed by #14520

Comments

@InSyncWithFoo
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant