Skip to content

RUF033 fix introduces an error for a type variable scoped to __post_init__ #19628

@dscorbett

Description

@dscorbett

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 defined

Version

ruff 0.12.7 (c5ac998 2025-07-29)

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingfixesRelated to suggested fixes for violations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions