-
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
[ruff
] Also report problems for attrs
dataclasses in preview mode (RUF008
, RUF009
)
#14327
Conversation
|
code | total | + violation | - violation | + fix | - fix |
---|---|---|---|---|---|
RUF008 | 6 | 6 | 0 | 0 | 0 |
RUF012 | 6 | 0 | 6 | 0 | 0 |
RUF009 | 2 | 2 | 0 | 0 | 0 |
CodSpeed Performance ReportMerging #14327 will not alter performanceComparing Summary
|
@AlexWaygood would you mind taking a look at this rule. I don't have the necessary context about what it changes to review it. I do think that we should make this a preview-only change because it increases the rule's scope. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @InSyncWithFoo, this is great! I pushed some changes:
- Optimised it a bit by adding some tracking to the semantic model of whether
attrs
has been imported or not - Consolidated the logic in the
rules::ruff::helpers
module - Made the change preview-only for now, since it increases the scope of the rule.
ruff
] Also report problems for attrs
dataclasses (RUF008
, RUF009
)ruff
] Also report problems for attrs
dataclasses in preview mode (RUF008
, RUF009
)
Note that 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 @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 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 I'll work on a fix. Could you open a new issue with all the details, please? |
Summary
Resolves #9757.
Test Plan
cargo nextest run
andcargo insta test
.