-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Closed
Copy link
Labels
bugSomething isn't workingSomething isn't workingfixesRelated to suggested fixes for violationsRelated to suggested fixes for violations
Description
Summary
The fix for post-init-default (RUF033) introduces an error when the parameter’s type uses a type variable scoped to __post_init__. It might not be possible to fix this automatically. Example:
$ cat >ruf033.py <<'# EOF'
from annotationlib import get_annotations
from dataclasses import dataclass
@dataclass
class C:
def __post_init__[T: (str, bytes)](self, a: T | None = None, b: T | None = None) -> None:
self.a = str(a)
self.b = str(b)
print(get_annotations(C))
# EOF
$ python3.14 ruf033.py
{}
$ ruff --isolated check ruf033.py --select RUF033 --unsafe-fixes --fix
Found 2 errors (2 fixed, 0 remaining).
$ cat ruf033.py
from annotationlib import get_annotations
from dataclasses import dataclass, InitVar
@dataclass
class C:
a: InitVar[T | None] = None
b: InitVar[T | None] = None
def __post_init__[T: (str, bytes)](self, a: T | None, b: T | None) -> None:
self.a = a
self.b = b
print(get_annotations(C))
$ python3.14 ruf033.py 2>&1 | tail -n 1
NameError: name 'T' is not definedVersion
ruff 0.12.7 (c5ac998 2025-07-29)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingfixesRelated to suggested fixes for violationsRelated to suggested fixes for violations