class documentation

class DataclassTransformer: (source)

View In Hierarchy

Implement the behavior of @dataclass. Note that this may be executed multiple times on the same class, so everything here must be idempotent. This runs after the main semantic analysis pass, so you can assume that there are no placeholders.

Method __init__ Undocumented
Method add_slots Undocumented
Method collect_attributes Collect all attributes declared in the dataclass and its parents.
Method reset_init_only_vars Remove init-only vars from the class and reset init var declarations.
Method transform Apply all the necessary transformations to the underlying dataclass so as to ensure it is fully type checked according to the rules in PEP 557.
Method _add_dataclass_fields_magic_attribute Undocumented
Method _collect_field_args Returns a tuple where the first value represents whether or not the expression is a call to dataclass.field and the second is a dictionary of the keyword arguments that field() was called with.
Method _freeze Converts all attributes to @property methods in order to emulate frozen classes.
Method _get_bool_arg Undocumented
Method _is_kw_only_type Checks if the type of the node is the KW_ONLY sentinel value.
Method _propertize_callables Converts all attributes with callable types to @property methods.
Instance Variable _api Undocumented
Instance Variable _cls Undocumented
Instance Variable _reason Undocumented
Instance Variable _spec Undocumented
def add_slots(self, info: TypeInfo, attributes: list[DataclassAttribute], *, correct_version: bool): (source)

Undocumented

def collect_attributes(self) -> list[DataclassAttribute]|None: (source)

Collect all attributes declared in the dataclass and its parents. All assignments of the form a: SomeType b: SomeOtherType = ... are collected. Return None if some dataclass base class hasn't been processed yet and thus we'll need to ask for another pass.

def reset_init_only_vars(self, info: TypeInfo, attributes: list[DataclassAttribute]): (source)

Remove init-only vars from the class and reset init var declarations.

def transform(self) -> bool: (source)

Apply all the necessary transformations to the underlying dataclass so as to ensure it is fully type checked according to the rules in PEP 557.

def _add_dataclass_fields_magic_attribute(self): (source)

Undocumented

def _collect_field_args(self, expr: Expression) -> tuple[bool, dict[str, Expression]]: (source)

Returns a tuple where the first value represents whether or not the expression is a call to dataclass.field and the second is a dictionary of the keyword arguments that field() was called with.

def _freeze(self, attributes: list[DataclassAttribute]): (source)

Converts all attributes to @property methods in order to emulate frozen classes.

def _get_bool_arg(self, name: str, default: bool) -> bool: (source)

Undocumented

def _is_kw_only_type(self, node: Type|None) -> bool: (source)

Checks if the type of the node is the KW_ONLY sentinel value.

def _propertize_callables(self, attributes: list[DataclassAttribute], settable: bool = True): (source)

Converts all attributes with callable types to @property methods. This avoids the typechecker getting confused and thinking that `my_dataclass_instance.callable_attr(foo)` is going to receive a `self` argument (it is not).

Undocumented

Undocumented

Undocumented

Undocumented