[ruff] Also report problems for attrs dataclasses in preview mode (RUF008, RUF009)#14327
[ruff] Also report problems for attrs dataclasses in preview mode (RUF008, RUF009)#14327AlexWaygood merged 5 commits intoastral-sh:mainfrom
ruff] Also report problems for attrs dataclasses in preview mode (RUF008, RUF009)#14327Conversation
|
| 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. |
AlexWaygood
left a comment
There was a problem hiding this comment.
Thanks @InSyncWithFoo, this is great! I pushed some changes:
- Optimised it a bit by adding some tracking to the semantic model of whether
attrshas been imported or not - Consolidated the logic in the
rules::ruff::helpersmodule - 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 defaultsWithout @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? |
|
I can't get this working, even on 0.9.0 which was when this was merged: |
Please open a new issue. It makes it easier to help and allows other users to find it too |
Summary
Resolves #9757.
Test Plan
cargo nextest runandcargo insta test.