Transform ID:Documentation Index
Fetch the complete documentation index at: https://docs.refactron.dev/llms.txt
Use this file to discover all available pages before exploring further.
class_to_dataclass
Language: Python
What it does
Detects classes whose__init__ method only does self.<name> = <name> assignments and rewrites them as @dataclass. Each former init parameter becomes a typed field (Any when the original parameter has no annotation).
Detector pattern
The detector atsrc/analyze/detectors/python/class-init-only.ts walks ClassDef nodes, accepts only those with a single __init__ method, and verifies every body statement is an Assign(targets=[Attribute(self, name)], value=Name(name)) shape.
Preconditions
- The class has exactly one method, and it is
__init__. - The
__init__body is purelyself.<name> = <name>lines (no defaults, no transformations likeself.x = x * 2). - The class has no
__slots__declaration. - The class does not inherit from a parent with a custom
__init__. - Init parameter names match the assignment target names (no
self.public = _privateredirection).
Before / after
Edge cases handled
- Adds
from dataclasses import dataclassandfrom typing import Anyif missing. - Preserves class docstrings and decorators that precede the class.
- Multiple convertible classes per file are all converted in one pass.
Edge cases NOT handled (skip via precondition)
- Class has any method besides
__init__(the transform refuses to promote — a dataclass with custom methods is fine in principle, but inferringeq/hash/reprinteractions safely is out of scope for v0.2). - Init body contains expressions (
self.id = id * 2,self.full_name = first + " " + last). - Init has default arguments (
def __init__(self, x=0)) — supported by dataclass viafield(default=…)but not auto-inferred here. - Class uses
__slots__.