@alloy-js/python 0.5.0-dev.1 → 0.5.0-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/dev/src/components/SourceFile.js +44 -32
- package/dist/dev/src/components/SourceFile.js.map +1 -1
- package/dist/dev/test/callsignatures.test.js +471 -297
- package/dist/dev/test/callsignatures.test.js.map +1 -1
- package/dist/dev/test/class-method-declaration.test.js +21 -10
- package/dist/dev/test/class-method-declaration.test.js.map +1 -1
- package/dist/dev/test/classdeclarations.test.js +459 -393
- package/dist/dev/test/classdeclarations.test.js.map +1 -1
- package/dist/dev/test/classinstantiations.test.js +201 -168
- package/dist/dev/test/classinstantiations.test.js.map +1 -1
- package/dist/dev/test/constructordeclaration.test.js +22 -11
- package/dist/dev/test/constructordeclaration.test.js.map +1 -1
- package/dist/dev/test/dataclassdeclarations.test.js +322 -368
- package/dist/dev/test/dataclassdeclarations.test.js.map +1 -1
- package/dist/dev/test/decoratorlist.test.js +96 -49
- package/dist/dev/test/decoratorlist.test.js.map +1 -1
- package/dist/dev/test/dundermethoddeclaration.test.js +22 -11
- package/dist/dev/test/dundermethoddeclaration.test.js.map +1 -1
- package/dist/dev/test/enums.test.js +218 -184
- package/dist/dev/test/enums.test.js.map +1 -1
- package/dist/dev/test/externals.test.js +57 -45
- package/dist/dev/test/externals.test.js.map +1 -1
- package/dist/dev/test/factories.test.js +124 -50
- package/dist/dev/test/factories.test.js.map +1 -1
- package/dist/dev/test/functioncallexpressions.test.js +199 -164
- package/dist/dev/test/functioncallexpressions.test.js.map +1 -1
- package/dist/dev/test/functiondeclaration.test.js +439 -272
- package/dist/dev/test/functiondeclaration.test.js.map +1 -1
- package/dist/dev/test/imports.test.js +273 -221
- package/dist/dev/test/imports.test.js.map +1 -1
- package/dist/dev/test/memberexpressions.test.js +1237 -972
- package/dist/dev/test/memberexpressions.test.js.map +1 -1
- package/dist/dev/test/methoddeclaration.test.js +142 -78
- package/dist/dev/test/methoddeclaration.test.js.map +1 -1
- package/dist/dev/test/namepolicies.test.js +130 -94
- package/dist/dev/test/namepolicies.test.js.map +1 -1
- package/dist/dev/test/propertydeclaration.test.js +88 -59
- package/dist/dev/test/propertydeclaration.test.js.map +1 -1
- package/dist/dev/test/pydanticclassdeclarations.test.js +299 -347
- package/dist/dev/test/pydanticclassdeclarations.test.js.map +1 -1
- package/dist/dev/test/pydocs.test.js +888 -715
- package/dist/dev/test/pydocs.test.js.map +1 -1
- package/dist/dev/test/references.test.js +42 -35
- package/dist/dev/test/references.test.js.map +1 -1
- package/dist/dev/test/sourcefiles.test.js +1109 -841
- package/dist/dev/test/sourcefiles.test.js.map +1 -1
- package/dist/dev/test/staticmethoddeclaration.test.js +21 -10
- package/dist/dev/test/staticmethoddeclaration.test.js.map +1 -1
- package/dist/dev/test/type-checking-imports.test.js +408 -359
- package/dist/dev/test/type-checking-imports.test.js.map +1 -1
- package/dist/dev/test/typereference.test.js +55 -40
- package/dist/dev/test/typereference.test.js.map +1 -1
- package/dist/dev/test/uniontypeexpression.test.js +222 -146
- package/dist/dev/test/uniontypeexpression.test.js.map +1 -1
- package/dist/dev/test/utils.js +39 -77
- package/dist/dev/test/utils.js.map +1 -1
- package/dist/dev/test/values.test.js +237 -101
- package/dist/dev/test/values.test.js.map +1 -1
- package/dist/dev/test/variables.test.js +321 -203
- package/dist/dev/test/variables.test.js.map +1 -1
- package/dist/dev/test/vitest.setup.js +2 -0
- package/dist/dev/test/vitest.setup.js.map +1 -0
- package/dist/src/components/SourceFile.d.ts +2 -2
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +12 -0
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/test/callsignatures.test.js +346 -272
- package/dist/test/callsignatures.test.js.map +1 -1
- package/dist/test/class-method-declaration.test.js +7 -4
- package/dist/test/class-method-declaration.test.js.map +1 -1
- package/dist/test/classdeclarations.test.js +302 -288
- package/dist/test/classdeclarations.test.js.map +1 -1
- package/dist/test/classinstantiations.test.js +112 -103
- package/dist/test/classinstantiations.test.js.map +1 -1
- package/dist/test/constructordeclaration.test.js +7 -4
- package/dist/test/constructordeclaration.test.js.map +1 -1
- package/dist/test/dataclassdeclarations.test.js +134 -184
- package/dist/test/dataclassdeclarations.test.js.map +1 -1
- package/dist/test/decoratorlist.test.js +59 -36
- package/dist/test/decoratorlist.test.js.map +1 -1
- package/dist/test/dundermethoddeclaration.test.js +7 -4
- package/dist/test/dundermethoddeclaration.test.js.map +1 -1
- package/dist/test/enums.test.js +161 -159
- package/dist/test/enums.test.js.map +1 -1
- package/dist/test/externals.test.js +24 -24
- package/dist/test/externals.test.js.map +1 -1
- package/dist/test/factories.test.js +75 -33
- package/dist/test/factories.test.js.map +1 -1
- package/dist/test/functioncallexpressions.test.js +117 -106
- package/dist/test/functioncallexpressions.test.js.map +1 -1
- package/dist/test/functiondeclaration.test.js +247 -180
- package/dist/test/functiondeclaration.test.js.map +1 -1
- package/dist/test/imports.test.js +171 -143
- package/dist/test/imports.test.js.map +1 -1
- package/dist/test/memberexpressions.test.js +582 -453
- package/dist/test/memberexpressions.test.js.map +1 -1
- package/dist/test/methoddeclaration.test.js +66 -46
- package/dist/test/methoddeclaration.test.js.map +1 -1
- package/dist/test/namepolicies.test.js +90 -78
- package/dist/test/namepolicies.test.js.map +1 -1
- package/dist/test/propertydeclaration.test.js +25 -20
- package/dist/test/propertydeclaration.test.js.map +1 -1
- package/dist/test/pydanticclassdeclarations.test.js +134 -190
- package/dist/test/pydanticclassdeclarations.test.js.map +1 -1
- package/dist/test/pydocs.test.js +573 -532
- package/dist/test/pydocs.test.js.map +1 -1
- package/dist/test/references.test.js +31 -28
- package/dist/test/references.test.js.map +1 -1
- package/dist/test/sourcefiles.test.js +700 -580
- package/dist/test/sourcefiles.test.js.map +1 -1
- package/dist/test/staticmethoddeclaration.test.js +7 -4
- package/dist/test/staticmethoddeclaration.test.js.map +1 -1
- package/dist/test/type-checking-imports.test.js +297 -284
- package/dist/test/type-checking-imports.test.js.map +1 -1
- package/dist/test/typereference.test.js +29 -22
- package/dist/test/typereference.test.js.map +1 -1
- package/dist/test/uniontypeexpression.test.js +124 -88
- package/dist/test/uniontypeexpression.test.js.map +1 -1
- package/dist/test/utils.d.ts +10 -17
- package/dist/test/utils.d.ts.map +1 -1
- package/dist/test/utils.js +32 -74
- package/dist/test/utils.js.map +1 -1
- package/dist/test/values.test.js +135 -67
- package/dist/test/values.test.js.map +1 -1
- package/dist/test/variables.test.js +201 -151
- package/dist/test/variables.test.js.map +1 -1
- package/dist/test/vitest.setup.d.ts +2 -0
- package/dist/test/vitest.setup.d.ts.map +1 -0
- package/dist/test/vitest.setup.js +2 -0
- package/dist/test/vitest.setup.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/docs/api/components/SourceFile.md +20 -8
- package/package.json +2 -2
- package/src/components/SourceFile.tsx +6 -1
- package/temp/api.json +16 -2
- package/test/callsignatures.test.tsx +309 -283
- package/test/class-method-declaration.test.tsx +3 -4
- package/test/classdeclarations.test.tsx +263 -248
- package/test/classinstantiations.test.tsx +115 -109
- package/test/constructordeclaration.test.tsx +9 -6
- package/test/dataclassdeclarations.test.tsx +243 -361
- package/test/decoratorlist.test.tsx +78 -59
- package/test/dundermethoddeclaration.test.tsx +3 -4
- package/test/enums.test.tsx +65 -81
- package/test/externals.test.tsx +25 -25
- package/test/factories.test.tsx +64 -22
- package/test/functioncallexpressions.test.tsx +123 -109
- package/test/functiondeclaration.test.tsx +209 -148
- package/test/imports.test.tsx +119 -91
- package/test/memberexpressions.test.tsx +265 -207
- package/test/methoddeclaration.test.tsx +84 -63
- package/test/namepolicies.test.tsx +69 -69
- package/test/propertydeclaration.test.tsx +7 -8
- package/test/pydanticclassdeclarations.test.tsx +355 -487
- package/test/pydocs.test.tsx +531 -579
- package/test/references.test.tsx +24 -23
- package/test/sourcefiles.test.tsx +527 -492
- package/test/staticmethoddeclaration.test.tsx +3 -4
- package/test/type-checking-imports.test.tsx +206 -218
- package/test/typereference.test.tsx +15 -12
- package/test/uniontypeexpression.test.tsx +74 -61
- package/test/utils.tsx +26 -110
- package/test/values.test.tsx +82 -32
- package/test/variables.test.tsx +162 -142
- package/test/vitest.setup.ts +1 -0
- package/vitest.config.ts +3 -0
|
@@ -1,27 +1,17 @@
|
|
|
1
|
-
import { Prose, namekey, refkey } from "@alloy-js/core";
|
|
2
|
-
import { d } from "@alloy-js/core/testing";
|
|
1
|
+
import { Prose, namekey, refkey, render } from "@alloy-js/core";
|
|
3
2
|
import { describe, expect, it } from "vitest";
|
|
4
3
|
import { dataclassesModule } from "../src/builtins/python.js";
|
|
5
4
|
import * as py from "../src/index.js";
|
|
6
|
-
import {
|
|
7
|
-
assertFileContents,
|
|
8
|
-
toSourceText,
|
|
9
|
-
toSourceTextMultiple,
|
|
10
|
-
} from "./utils.jsx";
|
|
5
|
+
import { TestOutput, TestOutputDirectory } from "./utils.js";
|
|
11
6
|
|
|
12
7
|
describe("DataclassDeclaration", () => {
|
|
13
8
|
it("stacks user decorators above @dataclass", () => {
|
|
14
|
-
|
|
15
|
-
[
|
|
16
|
-
<py.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
{ externals: [dataclassesModule] },
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
expect(res).toRenderTo(
|
|
24
|
-
d`
|
|
9
|
+
expect(
|
|
10
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
11
|
+
<py.DataclassDeclaration name="User" frozen decorators={["@final"]} />
|
|
12
|
+
</TestOutput>,
|
|
13
|
+
).toRenderTo(
|
|
14
|
+
`
|
|
25
15
|
from dataclasses import dataclass
|
|
26
16
|
|
|
27
17
|
|
|
@@ -30,7 +20,6 @@ describe("DataclassDeclaration", () => {
|
|
|
30
20
|
class User:
|
|
31
21
|
pass
|
|
32
22
|
|
|
33
|
-
|
|
34
23
|
`,
|
|
35
24
|
);
|
|
36
25
|
});
|
|
@@ -39,17 +28,12 @@ describe("DataclassDeclaration", () => {
|
|
|
39
28
|
const doc = (
|
|
40
29
|
<py.ClassDoc description={[<Prose>Represents a user.</Prose>]} />
|
|
41
30
|
);
|
|
42
|
-
|
|
43
|
-
[
|
|
44
|
-
<py.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
{ externals: [dataclassesModule] },
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
expect(res).toRenderTo(
|
|
52
|
-
d`
|
|
31
|
+
expect(
|
|
32
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
33
|
+
<py.DataclassDeclaration name="User" doc={doc} />
|
|
34
|
+
</TestOutput>,
|
|
35
|
+
).toRenderTo(
|
|
36
|
+
`
|
|
53
37
|
from dataclasses import dataclass
|
|
54
38
|
|
|
55
39
|
|
|
@@ -61,42 +45,36 @@ describe("DataclassDeclaration", () => {
|
|
|
61
45
|
|
|
62
46
|
pass
|
|
63
47
|
|
|
64
|
-
|
|
65
48
|
`,
|
|
66
49
|
);
|
|
67
50
|
});
|
|
68
51
|
|
|
69
52
|
it("Creates a dataclass with fields and defaults", () => {
|
|
70
|
-
|
|
71
|
-
[
|
|
72
|
-
<py.
|
|
73
|
-
<py.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
{ externals: [dataclassesModule] },
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
expect(res).toRenderTo(
|
|
99
|
-
d`
|
|
53
|
+
expect(
|
|
54
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
55
|
+
<py.DataclassDeclaration name="User">
|
|
56
|
+
<py.VariableDeclaration
|
|
57
|
+
instanceVariable
|
|
58
|
+
omitNone
|
|
59
|
+
name="id"
|
|
60
|
+
type="int"
|
|
61
|
+
/>
|
|
62
|
+
<py.VariableDeclaration
|
|
63
|
+
instanceVariable
|
|
64
|
+
name={namekey("_", { ignoreNamePolicy: true })}
|
|
65
|
+
type={dataclassesModule["."].KW_ONLY}
|
|
66
|
+
omitNone
|
|
67
|
+
/>
|
|
68
|
+
<py.VariableDeclaration
|
|
69
|
+
instanceVariable
|
|
70
|
+
name="name"
|
|
71
|
+
type="str"
|
|
72
|
+
initializer={"Anonymous"}
|
|
73
|
+
/>
|
|
74
|
+
</py.DataclassDeclaration>
|
|
75
|
+
</TestOutput>,
|
|
76
|
+
).toRenderTo(
|
|
77
|
+
`
|
|
100
78
|
from dataclasses import dataclass
|
|
101
79
|
from typing import TYPE_CHECKING
|
|
102
80
|
|
|
@@ -110,30 +88,24 @@ describe("DataclassDeclaration", () => {
|
|
|
110
88
|
_: KW_ONLY
|
|
111
89
|
name: str = "Anonymous"
|
|
112
90
|
|
|
113
|
-
|
|
114
91
|
`,
|
|
115
92
|
);
|
|
116
93
|
});
|
|
117
94
|
|
|
118
95
|
it("Creates a dataclass with keyword arguments", () => {
|
|
119
|
-
|
|
120
|
-
[
|
|
121
|
-
<py.
|
|
122
|
-
<py.
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
{ externals: [dataclassesModule] },
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
expect(res).toRenderTo(
|
|
136
|
-
d`
|
|
96
|
+
expect(
|
|
97
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
98
|
+
<py.DataclassDeclaration name="User" frozen slots kwOnly>
|
|
99
|
+
<py.VariableDeclaration
|
|
100
|
+
instanceVariable
|
|
101
|
+
omitNone
|
|
102
|
+
name="id"
|
|
103
|
+
type="int"
|
|
104
|
+
/>
|
|
105
|
+
</py.DataclassDeclaration>
|
|
106
|
+
</TestOutput>,
|
|
107
|
+
).toRenderTo(
|
|
108
|
+
`
|
|
137
109
|
from dataclasses import dataclass
|
|
138
110
|
|
|
139
111
|
|
|
@@ -141,35 +113,29 @@ describe("DataclassDeclaration", () => {
|
|
|
141
113
|
class User:
|
|
142
114
|
id: int
|
|
143
115
|
|
|
144
|
-
|
|
145
116
|
`,
|
|
146
117
|
);
|
|
147
118
|
});
|
|
148
119
|
|
|
149
120
|
it("Creates a dataclass with all keyword arguments", () => {
|
|
150
|
-
|
|
151
|
-
[
|
|
152
|
-
<py.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
{ externals: [dataclassesModule] },
|
|
169
|
-
);
|
|
170
|
-
|
|
171
|
-
expect(res).toRenderTo(
|
|
172
|
-
d`
|
|
121
|
+
expect(
|
|
122
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
123
|
+
<py.DataclassDeclaration
|
|
124
|
+
name="User"
|
|
125
|
+
init
|
|
126
|
+
repr={false}
|
|
127
|
+
eq
|
|
128
|
+
order={false}
|
|
129
|
+
unsafeHash
|
|
130
|
+
frozen
|
|
131
|
+
matchArgs={false}
|
|
132
|
+
kwOnly
|
|
133
|
+
slots
|
|
134
|
+
weakrefSlot={false}
|
|
135
|
+
/>
|
|
136
|
+
</TestOutput>,
|
|
137
|
+
).toRenderTo(
|
|
138
|
+
`
|
|
173
139
|
from dataclasses import dataclass
|
|
174
140
|
|
|
175
141
|
|
|
@@ -177,20 +143,16 @@ describe("DataclassDeclaration", () => {
|
|
|
177
143
|
class User:
|
|
178
144
|
pass
|
|
179
145
|
|
|
180
|
-
|
|
181
146
|
`,
|
|
182
147
|
);
|
|
183
148
|
});
|
|
184
149
|
|
|
185
150
|
it("Throws error when weakref_slot=True without slots=True", () => {
|
|
186
151
|
expect(() =>
|
|
187
|
-
|
|
188
|
-
[
|
|
189
|
-
<py.
|
|
190
|
-
|
|
191
|
-
</py.SourceFile>,
|
|
192
|
-
],
|
|
193
|
-
{ externals: [dataclassesModule] },
|
|
152
|
+
render(
|
|
153
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
154
|
+
<py.DataclassDeclaration name="User" weakrefSlot />
|
|
155
|
+
</TestOutput>,
|
|
194
156
|
),
|
|
195
157
|
).toThrowError(
|
|
196
158
|
/weakref_slot=True requires slots=True in @dataclass decorator/,
|
|
@@ -198,16 +160,12 @@ describe("DataclassDeclaration", () => {
|
|
|
198
160
|
});
|
|
199
161
|
|
|
200
162
|
it("Allows weakref_slot=True when slots=True", () => {
|
|
201
|
-
|
|
202
|
-
[
|
|
203
|
-
<py.
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
{ externals: [dataclassesModule] },
|
|
208
|
-
);
|
|
209
|
-
expect(res).toRenderTo(
|
|
210
|
-
d`
|
|
163
|
+
expect(
|
|
164
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
165
|
+
<py.DataclassDeclaration name="User" slots weakrefSlot />
|
|
166
|
+
</TestOutput>,
|
|
167
|
+
).toRenderTo(
|
|
168
|
+
`
|
|
211
169
|
from dataclasses import dataclass
|
|
212
170
|
|
|
213
171
|
|
|
@@ -215,35 +173,27 @@ describe("DataclassDeclaration", () => {
|
|
|
215
173
|
class User:
|
|
216
174
|
pass
|
|
217
175
|
|
|
218
|
-
|
|
219
176
|
`,
|
|
220
177
|
);
|
|
221
178
|
});
|
|
222
179
|
|
|
223
180
|
it("Throws error when order=True and eq=False", () => {
|
|
224
181
|
expect(() =>
|
|
225
|
-
|
|
226
|
-
[
|
|
227
|
-
<py.
|
|
228
|
-
|
|
229
|
-
</py.SourceFile>,
|
|
230
|
-
],
|
|
231
|
-
{ externals: [dataclassesModule] },
|
|
182
|
+
render(
|
|
183
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
184
|
+
<py.DataclassDeclaration name="User" order eq={false} />
|
|
185
|
+
</TestOutput>,
|
|
232
186
|
),
|
|
233
187
|
).toThrowError(/order=True requires eq=True/);
|
|
234
188
|
});
|
|
235
189
|
|
|
236
190
|
it("Creates a dataclass with order=True and no conflicting methods", () => {
|
|
237
|
-
|
|
238
|
-
[
|
|
239
|
-
<py.
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
{ externals: [dataclassesModule] },
|
|
244
|
-
);
|
|
245
|
-
expect(res).toRenderTo(
|
|
246
|
-
d`
|
|
191
|
+
expect(
|
|
192
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
193
|
+
<py.DataclassDeclaration name="User" order />
|
|
194
|
+
</TestOutput>,
|
|
195
|
+
).toRenderTo(
|
|
196
|
+
`
|
|
247
197
|
from dataclasses import dataclass
|
|
248
198
|
|
|
249
199
|
|
|
@@ -251,22 +201,18 @@ describe("DataclassDeclaration", () => {
|
|
|
251
201
|
class User:
|
|
252
202
|
pass
|
|
253
203
|
|
|
254
|
-
|
|
255
204
|
`,
|
|
256
205
|
);
|
|
257
206
|
});
|
|
258
207
|
|
|
259
208
|
it("Throws error when order=True and class defines __lt__", () => {
|
|
260
209
|
expect(() =>
|
|
261
|
-
|
|
262
|
-
[
|
|
263
|
-
<py.
|
|
264
|
-
<py.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
</py.SourceFile>,
|
|
268
|
-
],
|
|
269
|
-
{ externals: [dataclassesModule] },
|
|
210
|
+
render(
|
|
211
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
212
|
+
<py.DataclassDeclaration name="User" order>
|
|
213
|
+
<py.DunderMethodDeclaration name="__lt__" />
|
|
214
|
+
</py.DataclassDeclaration>
|
|
215
|
+
</TestOutput>,
|
|
270
216
|
),
|
|
271
217
|
).toThrowError(
|
|
272
218
|
/Cannot specify order=True when the class already defines __lt__\(\)/,
|
|
@@ -275,15 +221,12 @@ describe("DataclassDeclaration", () => {
|
|
|
275
221
|
|
|
276
222
|
it("Throws error when order=True and class defines __le__", () => {
|
|
277
223
|
expect(() =>
|
|
278
|
-
|
|
279
|
-
[
|
|
280
|
-
<py.
|
|
281
|
-
<py.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
</py.SourceFile>,
|
|
285
|
-
],
|
|
286
|
-
{ externals: [dataclassesModule] },
|
|
224
|
+
render(
|
|
225
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
226
|
+
<py.DataclassDeclaration name="User" order>
|
|
227
|
+
<py.DunderMethodDeclaration name="__le__" />
|
|
228
|
+
</py.DataclassDeclaration>
|
|
229
|
+
</TestOutput>,
|
|
287
230
|
),
|
|
288
231
|
).toThrowError(
|
|
289
232
|
/Cannot specify order=True when the class already defines __le__\(\)/,
|
|
@@ -292,15 +235,12 @@ describe("DataclassDeclaration", () => {
|
|
|
292
235
|
|
|
293
236
|
it("Throws error when order=True and class defines __gt__", () => {
|
|
294
237
|
expect(() =>
|
|
295
|
-
|
|
296
|
-
[
|
|
297
|
-
<py.
|
|
298
|
-
<py.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
</py.SourceFile>,
|
|
302
|
-
],
|
|
303
|
-
{ externals: [dataclassesModule] },
|
|
238
|
+
render(
|
|
239
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
240
|
+
<py.DataclassDeclaration name="User" order>
|
|
241
|
+
<py.DunderMethodDeclaration name="__gt__" />
|
|
242
|
+
</py.DataclassDeclaration>
|
|
243
|
+
</TestOutput>,
|
|
304
244
|
),
|
|
305
245
|
).toThrowError(
|
|
306
246
|
/Cannot specify order=True when the class already defines __gt__\(\)/,
|
|
@@ -309,15 +249,12 @@ describe("DataclassDeclaration", () => {
|
|
|
309
249
|
|
|
310
250
|
it("Throws error when order=True and class defines __ge__", () => {
|
|
311
251
|
expect(() =>
|
|
312
|
-
|
|
313
|
-
[
|
|
314
|
-
<py.
|
|
315
|
-
<py.
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
</py.SourceFile>,
|
|
319
|
-
],
|
|
320
|
-
{ externals: [dataclassesModule] },
|
|
252
|
+
render(
|
|
253
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
254
|
+
<py.DataclassDeclaration name="User" order>
|
|
255
|
+
<py.DunderMethodDeclaration name="__ge__" />
|
|
256
|
+
</py.DataclassDeclaration>
|
|
257
|
+
</TestOutput>,
|
|
321
258
|
),
|
|
322
259
|
).toThrowError(
|
|
323
260
|
/Cannot specify order=True when the class already defines __ge__\(\)/,
|
|
@@ -329,15 +266,12 @@ describe("DataclassDeclaration", () => {
|
|
|
329
266
|
return <py.DunderMethodDeclaration name="__lt__" />;
|
|
330
267
|
}
|
|
331
268
|
expect(() =>
|
|
332
|
-
|
|
333
|
-
[
|
|
334
|
-
<py.
|
|
335
|
-
<
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
</py.SourceFile>,
|
|
339
|
-
],
|
|
340
|
-
{ externals: [dataclassesModule] },
|
|
269
|
+
render(
|
|
270
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
271
|
+
<py.DataclassDeclaration name="User" order>
|
|
272
|
+
<Wrapper />
|
|
273
|
+
</py.DataclassDeclaration>
|
|
274
|
+
</TestOutput>,
|
|
341
275
|
),
|
|
342
276
|
).toThrowError(
|
|
343
277
|
/Cannot specify order=True when the class already defines __lt__\(\)/,
|
|
@@ -346,15 +280,12 @@ describe("DataclassDeclaration", () => {
|
|
|
346
280
|
|
|
347
281
|
it("Throws error when unsafe_hash=True and class defines __hash__", () => {
|
|
348
282
|
expect(() =>
|
|
349
|
-
|
|
350
|
-
[
|
|
351
|
-
<py.
|
|
352
|
-
<py.
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
</py.SourceFile>,
|
|
356
|
-
],
|
|
357
|
-
{ externals: [dataclassesModule] },
|
|
283
|
+
render(
|
|
284
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
285
|
+
<py.DataclassDeclaration name="User" unsafeHash>
|
|
286
|
+
<py.DunderMethodDeclaration name="__hash__" />
|
|
287
|
+
</py.DataclassDeclaration>
|
|
288
|
+
</TestOutput>,
|
|
358
289
|
),
|
|
359
290
|
).toThrowError(
|
|
360
291
|
/Cannot specify unsafe_hash=True when the class already defines __hash__\(\)/,
|
|
@@ -363,15 +294,12 @@ describe("DataclassDeclaration", () => {
|
|
|
363
294
|
|
|
364
295
|
it("Throws error when frozen=True and class defines __setattr__", () => {
|
|
365
296
|
expect(() =>
|
|
366
|
-
|
|
367
|
-
[
|
|
368
|
-
<py.
|
|
369
|
-
<py.
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
</py.SourceFile>,
|
|
373
|
-
],
|
|
374
|
-
{ externals: [dataclassesModule] },
|
|
297
|
+
render(
|
|
298
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
299
|
+
<py.DataclassDeclaration name="User" frozen>
|
|
300
|
+
<py.DunderMethodDeclaration name="__setattr__" />
|
|
301
|
+
</py.DataclassDeclaration>
|
|
302
|
+
</TestOutput>,
|
|
375
303
|
),
|
|
376
304
|
).toThrowError(
|
|
377
305
|
/Cannot specify frozen=True when the class already defines __setattr__\(\)/,
|
|
@@ -380,15 +308,12 @@ describe("DataclassDeclaration", () => {
|
|
|
380
308
|
|
|
381
309
|
it("Throws errorwhen frozen=True and class defines __delattr__", () => {
|
|
382
310
|
expect(() =>
|
|
383
|
-
|
|
384
|
-
[
|
|
385
|
-
<py.
|
|
386
|
-
<py.
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
</py.SourceFile>,
|
|
390
|
-
],
|
|
391
|
-
{ externals: [dataclassesModule] },
|
|
311
|
+
render(
|
|
312
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
313
|
+
<py.DataclassDeclaration name="User" frozen>
|
|
314
|
+
<py.DunderMethodDeclaration name="__delattr__" />
|
|
315
|
+
</py.DataclassDeclaration>
|
|
316
|
+
</TestOutput>,
|
|
392
317
|
),
|
|
393
318
|
).toThrowError(
|
|
394
319
|
/Cannot specify frozen=True when the class already defines __delattr__\(\)/,
|
|
@@ -397,15 +322,12 @@ describe("DataclassDeclaration", () => {
|
|
|
397
322
|
|
|
398
323
|
it("Throws error when slots=True and class defines __slots__", () => {
|
|
399
324
|
expect(() =>
|
|
400
|
-
|
|
401
|
-
[
|
|
402
|
-
<py.
|
|
403
|
-
<py.
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
</py.SourceFile>,
|
|
407
|
-
],
|
|
408
|
-
{ externals: [dataclassesModule] },
|
|
325
|
+
render(
|
|
326
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
327
|
+
<py.DataclassDeclaration name="User" slots>
|
|
328
|
+
<py.DunderMethodDeclaration name="__slots__" />
|
|
329
|
+
</py.DataclassDeclaration>
|
|
330
|
+
</TestOutput>,
|
|
409
331
|
),
|
|
410
332
|
).toThrowError(
|
|
411
333
|
/Cannot specify slots=True when the class already defines __slots__\(\)/,
|
|
@@ -413,23 +335,19 @@ describe("DataclassDeclaration", () => {
|
|
|
413
335
|
});
|
|
414
336
|
|
|
415
337
|
it("Creates a dataclass with kw_only=True on decorator (sentinel not used)", () => {
|
|
416
|
-
|
|
417
|
-
[
|
|
418
|
-
<py.
|
|
419
|
-
<py.
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
{ externals: [dataclassesModule] },
|
|
430
|
-
);
|
|
431
|
-
expect(res).toRenderTo(
|
|
432
|
-
d`
|
|
338
|
+
expect(
|
|
339
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
340
|
+
<py.DataclassDeclaration name="User" kwOnly>
|
|
341
|
+
<py.VariableDeclaration
|
|
342
|
+
instanceVariable
|
|
343
|
+
omitNone
|
|
344
|
+
name="id"
|
|
345
|
+
type="int"
|
|
346
|
+
/>
|
|
347
|
+
</py.DataclassDeclaration>
|
|
348
|
+
</TestOutput>,
|
|
349
|
+
).toRenderTo(
|
|
350
|
+
`
|
|
433
351
|
from dataclasses import dataclass
|
|
434
352
|
|
|
435
353
|
|
|
@@ -437,23 +355,17 @@ describe("DataclassDeclaration", () => {
|
|
|
437
355
|
class User:
|
|
438
356
|
id: int
|
|
439
357
|
|
|
440
|
-
|
|
441
358
|
`,
|
|
442
359
|
);
|
|
443
360
|
});
|
|
444
361
|
|
|
445
362
|
it("Creates a dataclass with base classes", () => {
|
|
446
|
-
|
|
447
|
-
[
|
|
448
|
-
<py.
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
{ externals: [dataclassesModule] },
|
|
453
|
-
);
|
|
454
|
-
|
|
455
|
-
expect(res).toRenderTo(
|
|
456
|
-
d`
|
|
363
|
+
expect(
|
|
364
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
365
|
+
<py.DataclassDeclaration name="User" bases={["Base"]} />
|
|
366
|
+
</TestOutput>,
|
|
367
|
+
).toRenderTo(
|
|
368
|
+
`
|
|
457
369
|
from dataclasses import dataclass
|
|
458
370
|
|
|
459
371
|
|
|
@@ -461,82 +373,68 @@ describe("DataclassDeclaration", () => {
|
|
|
461
373
|
class User(Base):
|
|
462
374
|
pass
|
|
463
375
|
|
|
464
|
-
|
|
465
376
|
`,
|
|
466
377
|
);
|
|
467
378
|
});
|
|
468
379
|
|
|
469
380
|
it("Throws error when more than one KW_ONLY sentinel is present", () => {
|
|
470
381
|
expect(() =>
|
|
471
|
-
|
|
472
|
-
[
|
|
473
|
-
<py.
|
|
474
|
-
<py.
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
</py.SourceFile>,
|
|
489
|
-
],
|
|
490
|
-
{ externals: [dataclassesModule] },
|
|
382
|
+
render(
|
|
383
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
384
|
+
<py.DataclassDeclaration name="User">
|
|
385
|
+
<py.VariableDeclaration
|
|
386
|
+
instanceVariable
|
|
387
|
+
name={namekey("_", { ignoreNamePolicy: true })}
|
|
388
|
+
type={dataclassesModule["."].KW_ONLY}
|
|
389
|
+
omitNone
|
|
390
|
+
/>
|
|
391
|
+
<py.VariableDeclaration
|
|
392
|
+
instanceVariable
|
|
393
|
+
name={namekey("_", { ignoreNamePolicy: true })}
|
|
394
|
+
type={dataclassesModule["."].KW_ONLY}
|
|
395
|
+
omitNone
|
|
396
|
+
/>
|
|
397
|
+
</py.DataclassDeclaration>
|
|
398
|
+
</TestOutput>,
|
|
491
399
|
),
|
|
492
400
|
).toThrowError(/Only one KW_ONLY sentinel is allowed per dataclass body/);
|
|
493
401
|
});
|
|
494
402
|
|
|
495
403
|
it("Will raise arg validation errors first over member conflicts", () => {
|
|
496
404
|
expect(() =>
|
|
497
|
-
|
|
498
|
-
[
|
|
499
|
-
<py.
|
|
500
|
-
<py.
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
</py.SourceFile>,
|
|
504
|
-
],
|
|
505
|
-
{ externals: [dataclassesModule] },
|
|
405
|
+
render(
|
|
406
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
407
|
+
<py.DataclassDeclaration name="User" order eq={false}>
|
|
408
|
+
<py.DunderMethodDeclaration name="__lt__" />
|
|
409
|
+
</py.DataclassDeclaration>
|
|
410
|
+
</TestOutput>,
|
|
506
411
|
),
|
|
507
412
|
).toThrowError(/order=True requires eq=True/);
|
|
508
413
|
});
|
|
509
414
|
|
|
510
415
|
it("Does not raise errors for member conflict checks without the equivalent kwargs", () => {
|
|
511
416
|
expect(() =>
|
|
512
|
-
|
|
513
|
-
[
|
|
514
|
-
<py.
|
|
515
|
-
<py.
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
</py.SourceFile>,
|
|
523
|
-
],
|
|
524
|
-
{ externals: [dataclassesModule] },
|
|
417
|
+
render(
|
|
418
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
419
|
+
<py.DataclassDeclaration name="User">
|
|
420
|
+
<py.DunderMethodDeclaration name="__lt__" />
|
|
421
|
+
<py.DunderMethodDeclaration name="__slots__" />
|
|
422
|
+
<py.DunderMethodDeclaration name="__hash__" />
|
|
423
|
+
<py.DunderMethodDeclaration name="__setattr__" />
|
|
424
|
+
<py.DunderMethodDeclaration name="__delattr__" />
|
|
425
|
+
</py.DataclassDeclaration>
|
|
426
|
+
</TestOutput>,
|
|
525
427
|
),
|
|
526
428
|
).not.toThrow();
|
|
527
429
|
});
|
|
528
430
|
|
|
529
431
|
it("Allows unsafe_hash=True when no __hash__ is defined", () => {
|
|
530
|
-
|
|
531
|
-
[
|
|
532
|
-
<py.
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
{ externals: [dataclassesModule] },
|
|
537
|
-
);
|
|
538
|
-
expect(res).toRenderTo(
|
|
539
|
-
d`
|
|
432
|
+
expect(
|
|
433
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
434
|
+
<py.DataclassDeclaration name="User" unsafeHash />
|
|
435
|
+
</TestOutput>,
|
|
436
|
+
).toRenderTo(
|
|
437
|
+
`
|
|
540
438
|
from dataclasses import dataclass
|
|
541
439
|
|
|
542
440
|
|
|
@@ -544,7 +442,6 @@ describe("DataclassDeclaration", () => {
|
|
|
544
442
|
class User:
|
|
545
443
|
pass
|
|
546
444
|
|
|
547
|
-
|
|
548
445
|
`,
|
|
549
446
|
);
|
|
550
447
|
});
|
|
@@ -561,36 +458,29 @@ describe("DataclassDeclaration", () => {
|
|
|
561
458
|
);
|
|
562
459
|
}
|
|
563
460
|
expect(() =>
|
|
564
|
-
|
|
565
|
-
[
|
|
566
|
-
<py.
|
|
567
|
-
<py.
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
</py.SourceFile>,
|
|
577
|
-
],
|
|
578
|
-
{ externals: [dataclassesModule] },
|
|
461
|
+
render(
|
|
462
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
463
|
+
<py.DataclassDeclaration name="User">
|
|
464
|
+
<py.VariableDeclaration
|
|
465
|
+
instanceVariable
|
|
466
|
+
name={namekey("_", { ignoreNamePolicy: true })}
|
|
467
|
+
type={dataclassesModule["."].KW_ONLY}
|
|
468
|
+
omitNone
|
|
469
|
+
/>
|
|
470
|
+
<Wrapper />
|
|
471
|
+
</py.DataclassDeclaration>
|
|
472
|
+
</TestOutput>,
|
|
579
473
|
),
|
|
580
474
|
).toThrowError(/Only one KW_ONLY sentinel is allowed per dataclass body/);
|
|
581
475
|
});
|
|
582
476
|
|
|
583
477
|
it("Allows frozen=True when no conflicting dunders exist", () => {
|
|
584
|
-
|
|
585
|
-
[
|
|
586
|
-
<py.
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
{ externals: [dataclassesModule] },
|
|
591
|
-
);
|
|
592
|
-
expect(res).toRenderTo(
|
|
593
|
-
d`
|
|
478
|
+
expect(
|
|
479
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
480
|
+
<py.DataclassDeclaration name="User" frozen />
|
|
481
|
+
</TestOutput>,
|
|
482
|
+
).toRenderTo(
|
|
483
|
+
`
|
|
594
484
|
from dataclasses import dataclass
|
|
595
485
|
|
|
596
486
|
|
|
@@ -598,22 +488,17 @@ describe("DataclassDeclaration", () => {
|
|
|
598
488
|
class User:
|
|
599
489
|
pass
|
|
600
490
|
|
|
601
|
-
|
|
602
491
|
`,
|
|
603
492
|
);
|
|
604
493
|
});
|
|
605
494
|
|
|
606
495
|
it("Allows slots=True when no __slots__ is defined", () => {
|
|
607
|
-
|
|
608
|
-
[
|
|
609
|
-
<py.
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
{ externals: [dataclassesModule] },
|
|
614
|
-
);
|
|
615
|
-
expect(res).toRenderTo(
|
|
616
|
-
d`
|
|
496
|
+
expect(
|
|
497
|
+
<TestOutput path="user.py" externals={[dataclassesModule]}>
|
|
498
|
+
<py.DataclassDeclaration name="User" slots />
|
|
499
|
+
</TestOutput>,
|
|
500
|
+
).toRenderTo(
|
|
501
|
+
`
|
|
617
502
|
from dataclasses import dataclass
|
|
618
503
|
|
|
619
504
|
|
|
@@ -621,15 +506,14 @@ describe("DataclassDeclaration", () => {
|
|
|
621
506
|
class User:
|
|
622
507
|
pass
|
|
623
508
|
|
|
624
|
-
|
|
625
509
|
`,
|
|
626
510
|
);
|
|
627
511
|
});
|
|
628
512
|
|
|
629
513
|
it("Forwards refkey prop for symbol resolution in type references", () => {
|
|
630
514
|
const userRefkey = refkey();
|
|
631
|
-
|
|
632
|
-
[
|
|
515
|
+
expect(
|
|
516
|
+
<TestOutputDirectory externals={[dataclassesModule]}>
|
|
633
517
|
<py.SourceFile path="models.py">
|
|
634
518
|
<py.DataclassDeclaration name="User" refkey={userRefkey}>
|
|
635
519
|
<py.VariableDeclaration
|
|
@@ -645,7 +529,7 @@ describe("DataclassDeclaration", () => {
|
|
|
645
529
|
type="str"
|
|
646
530
|
/>
|
|
647
531
|
</py.DataclassDeclaration>
|
|
648
|
-
</py.SourceFile
|
|
532
|
+
</py.SourceFile>
|
|
649
533
|
<py.SourceFile path="services.py">
|
|
650
534
|
<py.FunctionDeclaration name="get_user" returnType={userRefkey}>
|
|
651
535
|
<py.VariableDeclaration
|
|
@@ -661,30 +545,28 @@ describe("DataclassDeclaration", () => {
|
|
|
661
545
|
<hbr />
|
|
662
546
|
{"return user"}
|
|
663
547
|
</py.FunctionDeclaration>
|
|
664
|
-
</py.SourceFile
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
);
|
|
668
|
-
assertFileContents(res, {
|
|
548
|
+
</py.SourceFile>
|
|
549
|
+
</TestOutputDirectory>,
|
|
550
|
+
).toRenderTo({
|
|
669
551
|
"models.py": `
|
|
670
|
-
|
|
552
|
+
from dataclasses import dataclass
|
|
671
553
|
|
|
672
554
|
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
555
|
+
@dataclass
|
|
556
|
+
class User:
|
|
557
|
+
id: int
|
|
558
|
+
name: str
|
|
677
559
|
|
|
678
|
-
|
|
560
|
+
`,
|
|
679
561
|
"services.py": `
|
|
680
|
-
|
|
562
|
+
from models import User
|
|
681
563
|
|
|
682
564
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
565
|
+
def get_user() -> User:
|
|
566
|
+
user: User = User(1, "Alice")
|
|
567
|
+
return user
|
|
686
568
|
|
|
687
|
-
|
|
569
|
+
`,
|
|
688
570
|
});
|
|
689
571
|
});
|
|
690
572
|
});
|