Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dataclass_transform] support subclass/metaclass-based transforms #14657

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2830,6 +2830,7 @@ class is generic then it will be a type constructor of higher kind.
"type_var_tuple_prefix",
"type_var_tuple_suffix",
"self_type",
"dataclass_transform_spec",
)

_fullname: str # Fully qualified name
Expand Down Expand Up @@ -2977,6 +2978,9 @@ class is generic then it will be a type constructor of higher kind.
# Shared type variable for typing.Self in this class (if used, otherwise None).
self_type: mypy.types.TypeVarType | None

# Added if the corresponding class is directly decorated with `typing.dataclass_transform`
dataclass_transform_spec: DataclassTransformSpec | None

FLAGS: Final = [
"is_abstract",
"is_enum",
Expand Down Expand Up @@ -3032,6 +3036,7 @@ def __init__(self, names: SymbolTable, defn: ClassDef, module_name: str) -> None
self.is_intersection = False
self.metadata = {}
self.self_type = None
self.dataclass_transform_spec = None

def add_type_vars(self) -> None:
self.has_type_var_tuple_type = False
Expand Down Expand Up @@ -3251,6 +3256,11 @@ def serialize(self) -> JsonDict:
"slots": list(sorted(self.slots)) if self.slots is not None else None,
"deletable_attributes": self.deletable_attributes,
"self_type": self.self_type.serialize() if self.self_type is not None else None,
"dataclass_transform_spec": (
self.dataclass_transform_spec.serialize()
if self.dataclass_transform_spec is not None
else None
),
}
return data

Expand Down Expand Up @@ -3314,6 +3324,10 @@ def deserialize(cls, data: JsonDict) -> TypeInfo:
set_flags(ti, data["flags"])
st = data["self_type"]
ti.self_type = mypy.types.TypeVarType.deserialize(st) if st is not None else None
if data.get("dataclass_transform_spec") is not None:
ti.dataclass_transform_spec = DataclassTransformSpec.deserialize(
data["dataclass_transform_spec"]
)
return ti


Expand Down
Loading