forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
ruff
] Also report problems for attrs
dataclasses in preview mode…
… (`RUF008`, `RUF009`) (astral-sh#14327) Co-authored-by: Alex Waygood <alex.waygood@gmail.com>
- Loading branch information
1 parent
9a3001b
commit d8b1afb
Showing
12 changed files
with
344 additions
and
51 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
crates/ruff_linter/resources/test/fixtures/ruff/RUF008_attrs.py
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import typing | ||
from typing import ClassVar, Sequence | ||
|
||
import attr | ||
from attr import s | ||
from attrs import define, frozen | ||
|
||
KNOWINGLY_MUTABLE_DEFAULT = [] | ||
|
||
|
||
@define | ||
class A: | ||
mutable_default: list[int] = [] | ||
immutable_annotation: typing.Sequence[int] = [] | ||
without_annotation = [] | ||
correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
perfectly_fine: list[int] = field(default_factory=list) | ||
class_variable: typing.ClassVar[list[int]] = [] | ||
|
||
|
||
@frozen | ||
class B: | ||
mutable_default: list[int] = [] | ||
immutable_annotation: Sequence[int] = [] | ||
without_annotation = [] | ||
correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
perfectly_fine: list[int] = field(default_factory=list) | ||
class_variable: ClassVar[list[int]] = [] | ||
|
||
|
||
@attr.s | ||
class C: | ||
mutable_default: list[int] = [] | ||
immutable_annotation: Sequence[int] = [] | ||
without_annotation = [] | ||
correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
perfectly_fine: list[int] = field(default_factory=list) | ||
class_variable: ClassVar[list[int]] = [] | ||
|
||
@s | ||
class D: | ||
mutable_default: list[int] = [] | ||
immutable_annotation: Sequence[int] = [] | ||
without_annotation = [] | ||
correct_code: list[int] = KNOWINGLY_MUTABLE_DEFAULT | ||
perfectly_fine: list[int] = field(default_factory=list) | ||
class_variable: ClassVar[list[int]] = [] |
75 changes: 75 additions & 0 deletions
75
crates/ruff_linter/resources/test/fixtures/ruff/RUF009_attrs.py
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import datetime | ||
import re | ||
import typing | ||
from collections import OrderedDict | ||
from fractions import Fraction | ||
from pathlib import Path | ||
from typing import ClassVar, NamedTuple | ||
|
||
import attr | ||
from attrs import Factory, define, field, frozen | ||
|
||
|
||
def default_function() -> list[int]: | ||
return [] | ||
|
||
|
||
class ImmutableType(NamedTuple): | ||
something: int = 8 | ||
|
||
|
||
@attr.s | ||
class A: | ||
hidden_mutable_default: list[int] = default_function() | ||
class_variable: typing.ClassVar[list[int]] = default_function() | ||
another_class_var: ClassVar[list[int]] = default_function() | ||
|
||
fine_path: Path = Path() | ||
fine_date: datetime.date = datetime.date(2042, 1, 1) | ||
fine_timedelta: datetime.timedelta = datetime.timedelta(hours=7) | ||
fine_tuple: tuple[int] = tuple([1]) | ||
fine_regex: re.Pattern = re.compile(r".*") | ||
fine_float: float = float("-inf") | ||
fine_int: int = int(12) | ||
fine_complex: complex = complex(1, 2) | ||
fine_str: str = str("foo") | ||
fine_bool: bool = bool("foo") | ||
fine_fraction: Fraction = Fraction(1, 2) | ||
|
||
|
||
DEFAULT_IMMUTABLETYPE_FOR_ALL_DATACLASSES = ImmutableType(40) | ||
DEFAULT_A_FOR_ALL_DATACLASSES = A([1, 2, 3]) | ||
|
||
|
||
@define | ||
class B: | ||
hidden_mutable_default: list[int] = default_function() | ||
another_dataclass: A = A() | ||
not_optimal: ImmutableType = ImmutableType(20) | ||
good_variant: ImmutableType = DEFAULT_IMMUTABLETYPE_FOR_ALL_DATACLASSES | ||
okay_variant: A = DEFAULT_A_FOR_ALL_DATACLASSES | ||
|
||
fine_dataclass_function: list[int] = field(default_factory=list) | ||
attrs_factory: dict[str, str] = Factory(OrderedDict) | ||
|
||
|
||
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)) | ||
|
||
|
||
@frozen | ||
class InventoryItem: | ||
quantity_on_hand: IntConversionDescriptor = IntConversionDescriptor(default=100) |
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
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
Oops, something went wrong.