-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow descriptor instantiations in dataclass fields (#5537)
## Summary Per the Python documentation, dataclasses are allowed to instantiate descriptors, like so: ```python class IntConversionDescriptor: def __init__(self, *, default): self._default = default def __set_name__(self, owner, name): self._name = "_" + name def __get__(self, obj, type): if obj is None: return self._default return getattr(obj, self._name, self._default) def __set__(self, obj, value): setattr(obj, self._name, int(value)) @DataClass class InventoryItem: quantity_on_hand: IntConversionDescriptor = IntConversionDescriptor(default=100) ``` Closes #4451.
- Loading branch information
1 parent
9e1039f
commit c5bfd1e
Showing
4 changed files
with
72 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 24 additions & 24 deletions
48
crates/ruff/src/rules/ruff/snapshots/ruff__rules__ruff__tests__RUF009_RUF009.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/ruff/mod.rs | ||
--- | ||
RUF009.py:19:41: RUF009 Do not perform function call `default_function` in dataclass defaults | ||
RUF009.py:20:41: RUF009 Do not perform function call `default_function` in dataclass defaults | ||
| | ||
17 | @dataclass() | ||
18 | class A: | ||
19 | hidden_mutable_default: list[int] = default_function() | ||
18 | @dataclass() | ||
19 | class A: | ||
20 | hidden_mutable_default: list[int] = default_function() | ||
| ^^^^^^^^^^^^^^^^^^ RUF009 | ||
20 | class_variable: typing.ClassVar[list[int]] = default_function() | ||
21 | another_class_var: ClassVar[list[int]] = default_function() | ||
21 | class_variable: typing.ClassVar[list[int]] = default_function() | ||
22 | another_class_var: ClassVar[list[int]] = default_function() | ||
| | ||
|
||
RUF009.py:41:41: RUF009 Do not perform function call `default_function` in dataclass defaults | ||
RUF009.py:43:41: RUF009 Do not perform function call `default_function` in dataclass defaults | ||
| | ||
39 | @dataclass | ||
40 | class B: | ||
41 | hidden_mutable_default: list[int] = default_function() | ||
41 | @dataclass | ||
42 | class B: | ||
43 | hidden_mutable_default: list[int] = default_function() | ||
| ^^^^^^^^^^^^^^^^^^ RUF009 | ||
42 | another_dataclass: A = A() | ||
43 | not_optimal: ImmutableType = ImmutableType(20) | ||
44 | another_dataclass: A = A() | ||
45 | not_optimal: ImmutableType = ImmutableType(20) | ||
| | ||
|
||
RUF009.py:42:28: RUF009 Do not perform function call `A` in dataclass defaults | ||
RUF009.py:44:28: RUF009 Do not perform function call `A` in dataclass defaults | ||
| | ||
40 | class B: | ||
41 | hidden_mutable_default: list[int] = default_function() | ||
42 | another_dataclass: A = A() | ||
42 | class B: | ||
43 | hidden_mutable_default: list[int] = default_function() | ||
44 | another_dataclass: A = A() | ||
| ^^^ RUF009 | ||
43 | not_optimal: ImmutableType = ImmutableType(20) | ||
44 | good_variant: ImmutableType = DEFAULT_IMMUTABLETYPE_FOR_ALL_DATACLASSES | ||
45 | not_optimal: ImmutableType = ImmutableType(20) | ||
46 | good_variant: ImmutableType = DEFAULT_IMMUTABLETYPE_FOR_ALL_DATACLASSES | ||
| | ||
|
||
RUF009.py:43:34: RUF009 Do not perform function call `ImmutableType` in dataclass defaults | ||
RUF009.py:45:34: RUF009 Do not perform function call `ImmutableType` in dataclass defaults | ||
| | ||
41 | hidden_mutable_default: list[int] = default_function() | ||
42 | another_dataclass: A = A() | ||
43 | not_optimal: ImmutableType = ImmutableType(20) | ||
43 | hidden_mutable_default: list[int] = default_function() | ||
44 | another_dataclass: A = A() | ||
45 | not_optimal: ImmutableType = ImmutableType(20) | ||
| ^^^^^^^^^^^^^^^^^ RUF009 | ||
44 | good_variant: ImmutableType = DEFAULT_IMMUTABLETYPE_FOR_ALL_DATACLASSES | ||
45 | okay_variant: A = DEFAULT_A_FOR_ALL_DATACLASSES | ||
46 | good_variant: ImmutableType = DEFAULT_IMMUTABLETYPE_FOR_ALL_DATACLASSES | ||
47 | okay_variant: A = DEFAULT_A_FOR_ALL_DATACLASSES | ||
| | ||
|
||
|