Python Glossary Русский

Python 3.11+typingfunction

typing.dataclass_transform

decorators and markers

A marker decorator telling the type checker something produces dataclass-like objects (3.11+).

Syntax

@typing.dataclass_transform()

The decorator has no runtime effect — it only attaches a marker the static type checker reads, and never actually generates __init__ or any other method. It exists for library authors (attrs, pydantic, SQLAlchemy) so a checker treats their custom decorators and base classes as dataclass-like; application code rarely touches it.

Examples

from typing import dataclass_transform
print(callable(dataclass_transform))   # → True
from typing import dataclass_transform
def model(cls): return cls   # «фабрика» в стиле dataclass
print(dataclass_transform()(model) is model)   # → True
model = dataclass_transform(order_default=True)(model); print(model.__dataclass_transform__['order_default'])   # → True
print(dataclass_transform(True))   # → TypeError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy