RUF009 should behave similar to B008 and ignore attributes with immutable types (#16048)

This PR resolved #15772

Before PR:
```
def _(
    this_is_fine: int = f(),           # No error
    this_is_not: list[int] = f()       # B008: Do not perform function call `f` in argument defaults
): ...


@dataclass
class _:
    this_is_not_fine: list[int] = f()  # RUF009: Do not perform function call `f` in dataclass defaults
    this_is_also_not: int = f()        # RUF009: Do not perform function call `f` in dataclass defaults
```

After PR:
```
def _(
    this_is_fine: int = f(),           # No error
    this_is_not: list[int] = f()       # B008: Do not perform function call `f` in argument defaults
): ...


@dataclass
class _:
    this_is_not_fine: list[int] = f()  # RUF009: Do not perform function call `f` in dataclass defaults
    this_is_fine: int = f()
```
This commit is contained in:
ABDULRAHMAN ALRAHMA 2025-02-10 08:46:23 +00:00 committed by GitHub
parent 07cf8852a3
commit d2f661f795
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 29 additions and 107 deletions

View file

@ -97,3 +97,16 @@ class DataclassWithNewTypeFields:
# No errors
e: SpecialString = SpecialString("Lorem ipsum")
f: NegativeInteger = NegativeInteger(-110)
# Test for:
# https://github.com/astral-sh/ruff/issues/15772
def f() -> int:
return 0
@dataclass
class ShouldMatchB008RuleOfImmutableTypeAnnotationIgnored:
this_is_not_fine: list[int] = default_function()
# ignored
this_is_fine: int = f()