Debian Codegraph @

test-data/unit/fine-grained-dataclass-transform.test

raw
1 [case updateDataclassTransformParameterViaDecorator]
2 # flags: --python-version 3.11
3 from m import my_dataclass
4
5 @my_dataclass
6 class Foo:
7 x: int
8
9 foo = Foo(1)
10 foo.x = 2
11
12 [file m.py]
13 from typing import dataclass_transform
14
15 @dataclass_transform(frozen_default=False)
16 def my_dataclass(cls): return cls
17
18 [file m.py.2]
19 from typing import dataclass_transform
20
21 @dataclass_transform(frozen_default=True)
22 def my_dataclass(cls): return cls
23
24 [typing fixtures/typing-full.pyi]
25 [builtins fixtures/dataclasses.pyi]
26
27 [out]
28 ==
29 main:9: error: Property "x" defined in "Foo" is read-only
30
31 [case updateDataclassTransformParameterViaParentClass]
32 # flags: --python-version 3.11
33 from m import Dataclass
34
35 class Foo(Dataclass):
36 x: int
37
38 foo = Foo(1)
39 foo.x = 2
40
41 [file m.py]
42 from typing import dataclass_transform
43
44 @dataclass_transform(frozen_default=False)
45 class Dataclass: ...
46
47 [file m.py.2]
48 from typing import dataclass_transform
49
50 @dataclass_transform(frozen_default=True)
51 class Dataclass: ...
52
53 [typing fixtures/typing-full.pyi]
54 [builtins fixtures/dataclasses.pyi]
55
56 [out]
57 ==
58 main:8: error: Property "x" defined in "Foo" is read-only
59
60 [case updateBaseClassToUseDataclassTransform]
61 # flags: --python-version 3.11
62 from m import A
63
64 class B(A):
65 y: int
66
67 B(x=1, y=2)
68
69 [file m.py]
70 class Dataclass: ...
71
72 class A(Dataclass):
73 x: int
74
75 [file m.py.2]
76 from typing import dataclass_transform
77
78 @dataclass_transform()
79 class Dataclass: ...
80
81 class A(Dataclass):
82 x: int
83
84 [typing fixtures/typing-full.pyi]
85 [builtins fixtures/dataclasses.pyi]
86
87 [out]
88 main:7: error: Unexpected keyword argument "x" for "B"
89 main:7: error: Unexpected keyword argument "y" for "B"
90 ==
91
92 [case frozenInheritanceViaDefault]
93 # flags: --python-version 3.11
94 from foo import Foo
95
96 foo = Foo(base=0, foo=1)
97
98 [file transform.py]
99 from typing import dataclass_transform, Type
100
101 @dataclass_transform(frozen_default=True)
102 def dataclass(cls: Type) -> Type: return cls
103
104 [file base.py]
105 from transform import dataclass
106
107 @dataclass
108 class Base:
109 base: int
110
111 [file foo.py]
112 from base import Base
113 from transform import dataclass
114
115 @dataclass
116 class Foo(Base):
117 foo: int
118
119 [file foo.py.2]
120 from base import Base
121 from transform import dataclass
122
123 @dataclass
124 class Foo(Base):
125 foo: int
126 bar: int = 0
127
128 [typing fixtures/typing-full.pyi]
129 [builtins fixtures/dataclasses.pyi]
130
131 # If the frozen parameter is being maintained correctly, we *don't* expect to see issues; if it's
132 # broken in incremental mode, then we'll see an error about inheriting a non-frozen class from a
133 # frozen one.
134 #
135 # Ideally we'd also add a `foo.foo = 2` to confirm that frozen semantics are actually being
136 # enforced, but incremental tests currently can't start with an error, which makes it tricky to
137 # write such a test case.
138 [out]
139 ==