ITADN

Support dataclass transform

#2049Pull RequestMorikko 创建于 2025-02-15
M
Morikkocommented
# Goal Support dataclass transform as specified: https://typing.readthedocs.io/en/latest/spec/dataclasses.html#dataclass-transform # Motivation To have signature completions when using libraries like `Pydantic` or `SQLalchemy`. # Features - [X] Final attributes (already supported, just tested) - [X] Support ClassVar (excluded from init) - [X] dataclass_transform base class - [X] dataclass_transform decorator - [X] dataclass_transform metaclass - [x] init=False. The dataclass_transform decorator, metaclass or base class are always assumed to default to init=True. However if it is setting `init=False`, the parameters are by default ignored. If the defined dataclass-semantics is overriding the `init`, it takes precedence. Not covered: - `(dataclasses.Field, dataclasses.field) init=False` / `dataclass_transform field_specifiers init=False`. Those fields are always present. It may also show the wrong type/default value but as before this MR. - Base Model with custom default is not feasible as `__init_subclass__` signature is hardcoded in the jedi third_party typeshed. So `init` can not be introspected. # Technical details 1. Python Support ```python # 3.11+ from typing import dataclass_transform # <3.11 from typing_extensions import dataclass_transform ``` 2. The decorator usage is very similar to the `dataclasses.dataclass` 3. With a difference though: > If dataclass_transform is applied to a class, dataclass-like semantics will be assumed for any class that directly or indirectly derives from the decorated class or uses the decorated class as a metaclass. Attributes on the decorated class and its base classes are not considered to be fields.
合并状态:未合并 关闭于 2025-09-03 22 条评论