2Tests the dataclass_transform mechanism when it is applied to a decorator function.
5# Specification: https://typing.readthedocs.io/en/latest/spec/dataclasses.html#the-dataclass-transform-decorator
7from typing import Any, Callable, TypeVar, dataclass_transform, overload
13@dataclass_transform(kw_only_default=True, order_default=True)
14def create_model(cls: T) -> T:
28def create_model(*args: Any, **kwargs: Any) -> Any:
32@create_model(kw_only=False, order=False)
38@create_model(frozen=True)
44@create_model(frozen=True)
45class Customer2Subclass(Customer2):
49c1_1 = Customer1(id=3, name="Sue")
52c1_2 = Customer1(3, "Sue")
55# This should generate an error because of a type mismatch.
[invalid-assignment] Object of type `Literal[3]` is not assignable to attribute `name` of type `str`
58# This should generate an error because comparison methods are
[unsupported-operator] Operator `<` is not supported between two objects of type `Customer1`
62# This should generate an error because salary is not
64c1_3 = Customer1(id=3, name="Sue", salary=40000) # E
[unknown-argument] Argument `salary` does not match any known parameter
66c2_1 = Customer2(id=0, name="John")
68# This should generate an error because Customer2 supports
69# keyword-only parameters for its constructor.
70c2_2 = Customer2(0, "John") # E
[missing-argument] No arguments provided for required parameters `id`, `name`
[too-many-positional-arguments] Too many positional arguments: expected 0, got 2
75@dataclass_transform(kw_only_default=True, order_default=True, frozen_default=True)
76def create_model_frozen(cls: T) -> T:
77 raise NotImplementedError
86# This should generate an error because a non-frozen class
87# cannot inherit from a frozen class.
88@create_model # E[Customer3Subclass]
89class Customer3Subclass(Customer3): # E[Customer3Subclass]
Tag 'Customer3Subclass'
[invalid-frozen-dataclass-subclass] Non-frozen dataclass `Customer3Subclass` cannot inherit from frozen dataclass `Customer3`
93c3_1 = Customer3(id=2, name="hi")
95# This should generate an error because Customer3 is frozen.
[invalid-assignment] Property `id` defined in `Customer3` is read-only