@alloy-js/python 0.2.0-dev.3 → 0.2.0-dev.5

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.
Files changed (213) hide show
  1. package/dist/src/builtins/python.d.ts +3 -0
  2. package/dist/src/builtins/python.d.ts.map +1 -1
  3. package/dist/src/builtins/python.js +6 -0
  4. package/dist/src/builtins/python.js.map +1 -1
  5. package/dist/src/components/Atom.d.ts +1 -0
  6. package/dist/src/components/Atom.d.ts.map +1 -1
  7. package/dist/src/components/Atom.js +3 -0
  8. package/dist/src/components/Atom.js.map +1 -1
  9. package/dist/src/components/CallSignature.d.ts +4 -15
  10. package/dist/src/components/CallSignature.d.ts.map +1 -1
  11. package/dist/src/components/CallSignature.js +13 -67
  12. package/dist/src/components/CallSignature.js.map +1 -1
  13. package/dist/src/components/ClassMethodDeclaration.d.ts +22 -0
  14. package/dist/src/components/ClassMethodDeclaration.d.ts.map +1 -0
  15. package/dist/src/components/ClassMethodDeclaration.js +32 -0
  16. package/dist/src/components/ClassMethodDeclaration.js.map +1 -0
  17. package/dist/src/components/ConstructorDeclaration.d.ts +21 -0
  18. package/dist/src/components/ConstructorDeclaration.d.ts.map +1 -0
  19. package/dist/src/components/ConstructorDeclaration.js +35 -0
  20. package/dist/src/components/ConstructorDeclaration.js.map +1 -0
  21. package/dist/src/components/DunderMethodDeclaration.d.ts +21 -0
  22. package/dist/src/components/DunderMethodDeclaration.d.ts.map +1 -0
  23. package/dist/src/components/DunderMethodDeclaration.js +29 -0
  24. package/dist/src/components/DunderMethodDeclaration.js.map +1 -0
  25. package/dist/src/components/EnumDeclaration.d.ts +11 -32
  26. package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
  27. package/dist/src/components/EnumDeclaration.js +10 -48
  28. package/dist/src/components/EnumDeclaration.js.map +1 -1
  29. package/dist/src/components/EnumMember.js +3 -3
  30. package/dist/src/components/EnumMember.js.map +1 -1
  31. package/dist/src/components/FunctionBase.d.ts +48 -0
  32. package/dist/src/components/FunctionBase.d.ts.map +1 -0
  33. package/dist/src/components/FunctionBase.js +91 -0
  34. package/dist/src/components/FunctionBase.js.map +1 -0
  35. package/dist/src/components/FunctionDeclaration.d.ts +11 -31
  36. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
  37. package/dist/src/components/FunctionDeclaration.js +22 -79
  38. package/dist/src/components/FunctionDeclaration.js.map +1 -1
  39. package/dist/src/components/MethodBase.d.ts +29 -0
  40. package/dist/src/components/MethodBase.d.ts.map +1 -0
  41. package/dist/src/components/MethodBase.js +32 -0
  42. package/dist/src/components/MethodBase.js.map +1 -0
  43. package/dist/src/components/MethodDeclaration.d.ts +22 -0
  44. package/dist/src/components/MethodDeclaration.d.ts.map +1 -0
  45. package/dist/src/components/MethodDeclaration.js +34 -0
  46. package/dist/src/components/MethodDeclaration.js.map +1 -0
  47. package/dist/src/components/PropertyDeclaration.d.ts +71 -0
  48. package/dist/src/components/PropertyDeclaration.d.ts.map +1 -0
  49. package/dist/src/components/PropertyDeclaration.js +227 -0
  50. package/dist/src/components/PropertyDeclaration.js.map +1 -0
  51. package/dist/src/components/PyDoc.d.ts +107 -42
  52. package/dist/src/components/PyDoc.d.ts.map +1 -1
  53. package/dist/src/components/PyDoc.js +845 -181
  54. package/dist/src/components/PyDoc.js.map +1 -1
  55. package/dist/src/components/SourceFile.d.ts +24 -0
  56. package/dist/src/components/SourceFile.d.ts.map +1 -1
  57. package/dist/src/components/SourceFile.js +28 -1
  58. package/dist/src/components/SourceFile.js.map +1 -1
  59. package/dist/src/components/StaticMethodDeclaration.d.ts +22 -0
  60. package/dist/src/components/StaticMethodDeclaration.d.ts.map +1 -0
  61. package/dist/src/components/StaticMethodDeclaration.js +32 -0
  62. package/dist/src/components/StaticMethodDeclaration.js.map +1 -0
  63. package/dist/src/components/TypeArguments.d.ts +9 -0
  64. package/dist/src/components/TypeArguments.d.ts.map +1 -0
  65. package/dist/src/components/TypeArguments.js +18 -0
  66. package/dist/src/components/TypeArguments.js.map +1 -0
  67. package/dist/src/components/TypeReference.d.ts +14 -0
  68. package/dist/src/components/TypeReference.d.ts.map +1 -0
  69. package/dist/src/components/TypeReference.js +29 -0
  70. package/dist/src/components/TypeReference.js.map +1 -0
  71. package/dist/src/components/UnionTypeExpression.d.ts +1 -2
  72. package/dist/src/components/UnionTypeExpression.d.ts.map +1 -1
  73. package/dist/src/components/UnionTypeExpression.js +3 -11
  74. package/dist/src/components/UnionTypeExpression.js.map +1 -1
  75. package/dist/src/components/VariableDeclaration.d.ts.map +1 -1
  76. package/dist/src/components/VariableDeclaration.js +3 -3
  77. package/dist/src/components/VariableDeclaration.js.map +1 -1
  78. package/dist/src/components/index.d.ts +10 -0
  79. package/dist/src/components/index.d.ts.map +1 -1
  80. package/dist/src/components/index.js +9 -0
  81. package/dist/src/components/index.js.map +1 -1
  82. package/dist/src/parameter-descriptor.d.ts +1 -4
  83. package/dist/src/parameter-descriptor.d.ts.map +1 -1
  84. package/dist/src/parameter-descriptor.js +7 -1
  85. package/dist/src/parameter-descriptor.js.map +1 -1
  86. package/dist/src/symbol-creation.d.ts +4 -0
  87. package/dist/src/symbol-creation.d.ts.map +1 -1
  88. package/dist/src/symbol-creation.js +12 -0
  89. package/dist/src/symbol-creation.js.map +1 -1
  90. package/dist/src/symbols/factories.d.ts +15 -0
  91. package/dist/src/symbols/factories.d.ts.map +1 -0
  92. package/dist/src/symbols/factories.js +28 -0
  93. package/dist/src/symbols/factories.js.map +1 -0
  94. package/dist/src/symbols/index.d.ts +1 -0
  95. package/dist/src/symbols/index.d.ts.map +1 -1
  96. package/dist/src/symbols/index.js +1 -0
  97. package/dist/src/symbols/index.js.map +1 -1
  98. package/dist/src/symbols/reference.d.ts +1 -1
  99. package/dist/src/symbols/reference.d.ts.map +1 -1
  100. package/dist/src/symbols/reference.js +1 -1
  101. package/dist/src/symbols/reference.js.map +1 -1
  102. package/dist/src/utils.d.ts.map +1 -1
  103. package/dist/src/utils.js +1 -1
  104. package/dist/src/utils.js.map +1 -1
  105. package/dist/test/callsignatures.test.js +42 -64
  106. package/dist/test/callsignatures.test.js.map +1 -1
  107. package/dist/test/class-method-declaration.test.d.ts +2 -0
  108. package/dist/test/class-method-declaration.test.d.ts.map +1 -0
  109. package/dist/test/class-method-declaration.test.js +61 -0
  110. package/dist/test/class-method-declaration.test.js.map +1 -0
  111. package/dist/test/classdeclarations.test.js +6 -8
  112. package/dist/test/classdeclarations.test.js.map +1 -1
  113. package/dist/test/constructordeclaration.test.d.ts +2 -0
  114. package/dist/test/constructordeclaration.test.d.ts.map +1 -0
  115. package/dist/test/constructordeclaration.test.js +58 -0
  116. package/dist/test/constructordeclaration.test.js.map +1 -0
  117. package/dist/test/dundermethoddeclaration.test.d.ts +2 -0
  118. package/dist/test/dundermethoddeclaration.test.d.ts.map +1 -0
  119. package/dist/test/dundermethoddeclaration.test.js +65 -0
  120. package/dist/test/dundermethoddeclaration.test.js.map +1 -0
  121. package/dist/test/enums.test.js +14 -16
  122. package/dist/test/enums.test.js.map +1 -1
  123. package/dist/test/externals.test.js +2 -4
  124. package/dist/test/externals.test.js.map +1 -1
  125. package/dist/test/factories.test.d.ts +2 -0
  126. package/dist/test/factories.test.d.ts.map +1 -0
  127. package/dist/test/factories.test.js +78 -0
  128. package/dist/test/factories.test.js.map +1 -0
  129. package/dist/test/functiondeclaration.test.js +213 -59
  130. package/dist/test/functiondeclaration.test.js.map +1 -1
  131. package/dist/test/memberexpressions.test.js +1 -1
  132. package/dist/test/memberexpressions.test.js.map +1 -1
  133. package/dist/test/methoddeclaration.test.d.ts +2 -0
  134. package/dist/test/methoddeclaration.test.d.ts.map +1 -0
  135. package/dist/test/methoddeclaration.test.js +239 -0
  136. package/dist/test/methoddeclaration.test.js.map +1 -0
  137. package/dist/test/namepolicies.test.js +1 -2
  138. package/dist/test/namepolicies.test.js.map +1 -1
  139. package/dist/test/propertydeclaration.test.d.ts +2 -0
  140. package/dist/test/propertydeclaration.test.d.ts.map +1 -0
  141. package/dist/test/propertydeclaration.test.js +229 -0
  142. package/dist/test/propertydeclaration.test.js.map +1 -0
  143. package/dist/test/pydocs.test.js +926 -126
  144. package/dist/test/pydocs.test.js.map +1 -1
  145. package/dist/test/references.test.js +1 -5
  146. package/dist/test/references.test.js.map +1 -1
  147. package/dist/test/sourcefiles.test.js +90 -1
  148. package/dist/test/sourcefiles.test.js.map +1 -1
  149. package/dist/test/staticmethoddeclaration.test.d.ts +2 -0
  150. package/dist/test/staticmethoddeclaration.test.d.ts.map +1 -0
  151. package/dist/test/staticmethoddeclaration.test.js +61 -0
  152. package/dist/test/staticmethoddeclaration.test.js.map +1 -0
  153. package/dist/test/typereference.test.d.ts +2 -0
  154. package/dist/test/typereference.test.d.ts.map +1 -0
  155. package/dist/test/typereference.test.js +51 -0
  156. package/dist/test/typereference.test.js.map +1 -0
  157. package/dist/test/uniontypeexpression.test.js +152 -15
  158. package/dist/test/uniontypeexpression.test.js.map +1 -1
  159. package/dist/test/values.test.js +35 -0
  160. package/dist/test/values.test.js.map +1 -1
  161. package/dist/test/variables.test.js +28 -19
  162. package/dist/test/variables.test.js.map +1 -1
  163. package/dist/tsconfig.tsbuildinfo +1 -1
  164. package/package.json +2 -2
  165. package/src/builtins/python.ts +7 -0
  166. package/src/components/Atom.tsx +4 -0
  167. package/src/components/CallSignature.tsx +17 -69
  168. package/src/components/ClassMethodDeclaration.tsx +34 -0
  169. package/src/components/ConstructorDeclaration.tsx +37 -0
  170. package/src/components/DunderMethodDeclaration.tsx +30 -0
  171. package/src/components/EnumDeclaration.tsx +16 -44
  172. package/src/components/EnumMember.tsx +3 -3
  173. package/src/components/FunctionBase.tsx +88 -0
  174. package/src/components/FunctionDeclaration.tsx +18 -82
  175. package/src/components/MethodBase.tsx +53 -0
  176. package/src/components/MethodDeclaration.tsx +27 -0
  177. package/src/components/PropertyDeclaration.tsx +264 -0
  178. package/src/components/PyDoc.tsx +795 -195
  179. package/src/components/SourceFile.tsx +29 -0
  180. package/src/components/StaticMethodDeclaration.tsx +34 -0
  181. package/src/components/TypeArguments.tsx +24 -0
  182. package/src/components/TypeReference.tsx +33 -0
  183. package/src/components/UnionTypeExpression.tsx +4 -15
  184. package/src/components/VariableDeclaration.tsx +1 -3
  185. package/src/components/index.ts +10 -0
  186. package/src/parameter-descriptor.ts +6 -5
  187. package/src/symbol-creation.ts +17 -0
  188. package/src/symbols/factories.ts +39 -0
  189. package/src/symbols/index.ts +1 -0
  190. package/src/symbols/reference.tsx +3 -5
  191. package/src/utils.ts +0 -2
  192. package/temp/api.json +5323 -2288
  193. package/test/callsignatures.test.tsx +102 -74
  194. package/test/class-method-declaration.test.tsx +53 -0
  195. package/test/classdeclarations.test.tsx +7 -9
  196. package/test/constructordeclaration.test.tsx +48 -0
  197. package/test/dundermethoddeclaration.test.tsx +53 -0
  198. package/test/enums.test.tsx +14 -16
  199. package/test/externals.test.tsx +5 -7
  200. package/test/factories.test.tsx +72 -0
  201. package/test/functiondeclaration.test.tsx +196 -44
  202. package/test/memberexpressions.test.tsx +7 -2
  203. package/test/methoddeclaration.test.tsx +202 -0
  204. package/test/namepolicies.test.tsx +1 -2
  205. package/test/propertydeclaration.test.tsx +192 -0
  206. package/test/pydocs.test.tsx +1093 -129
  207. package/test/references.test.tsx +1 -1
  208. package/test/sourcefiles.test.tsx +100 -1
  209. package/test/staticmethoddeclaration.test.tsx +49 -0
  210. package/test/typereference.test.tsx +52 -0
  211. package/test/uniontypeexpression.test.tsx +169 -34
  212. package/test/values.test.tsx +34 -0
  213. package/test/variables.test.tsx +27 -16
@@ -26,8 +26,14 @@ describe("Call Signature Parameters", () => {
26
26
  const result = toSourceText([
27
27
  <py.CallSignatureParameters
28
28
  parameters={[
29
- { name: "a", type: "int" },
30
- { name: "b", type: "str" },
29
+ {
30
+ name: "a",
31
+ type: "int",
32
+ },
33
+ {
34
+ name: "b",
35
+ type: "str",
36
+ },
31
37
  ]}
32
38
  />,
33
39
  ]);
@@ -39,8 +45,16 @@ describe("Call Signature Parameters", () => {
39
45
  const result = toSourceText([
40
46
  <py.CallSignatureParameters
41
47
  parameters={[
42
- { name: "a", type: "int", optional: true },
43
- { name: "b", type: "str", optional: true },
48
+ {
49
+ name: "a",
50
+ type: "int",
51
+ default: null,
52
+ },
53
+ {
54
+ name: "b",
55
+ type: "str",
56
+ default: null,
57
+ },
44
58
  ]}
45
59
  />,
46
60
  ]);
@@ -65,8 +79,16 @@ describe("Call Signature Parameters", () => {
65
79
  const result = toSourceText([
66
80
  <py.CallSignatureParameters
67
81
  parameters={[
68
- { name: "a", type: "int", optional: true, default: 5 },
69
- { name: "b", type: "str", optional: true, default: "hello" },
82
+ {
83
+ name: "a",
84
+ type: "int",
85
+ default: 5,
86
+ },
87
+ {
88
+ name: "b",
89
+ type: "str",
90
+ default: "hello",
91
+ },
70
92
  ]}
71
93
  />,
72
94
  ]);
@@ -77,75 +99,60 @@ describe("Call Signature Parameters", () => {
77
99
  });
78
100
 
79
101
  describe("Call Signature", () => {
80
- it("renders a simple call signature", () => {
102
+ it("renders a call signature with strings", () => {
81
103
  const result = toSourceText([<py.CallSignature parameters={["a", "b"]} />]);
82
104
  expect(result).toRenderTo(d`
83
105
  (a, b)
84
106
  `);
85
107
  });
86
- it("renders a simple call signature with args and kwargs", () => {
87
- const result = toSourceText([
88
- <py.CallSignature parameters={["a", "b"]} args kwargs />,
89
- ]);
90
- expect(result).toRenderTo(d`
91
- (a, b, *args, **kwargs)
92
- `);
93
- });
94
- it("renders a simple call signature with type parameters", () => {
108
+ it("renders a call signature with parameter descriptors", () => {
95
109
  const result = toSourceText([
96
- <py.CallSignature parameters={["a", "b"]} typeParameters={["T", "U"]} />,
110
+ <py.CallSignature parameters={[{ name: "a" }, { name: "b" }]} />,
97
111
  ]);
98
112
  expect(result).toRenderTo(d`
99
- [T, U](a, b)
113
+ (a, b)
100
114
  `);
101
115
  });
102
- it("renders a simple call signature with return type", () => {
116
+ it("renders a call signature with mixed strings and parameter descriptors", () => {
103
117
  const result = toSourceText([
104
- <py.CallSignature parameters={["a", "b"]} returnType="int" />,
118
+ <py.CallSignature parameters={["a", { name: "b" }]} />,
105
119
  ]);
106
120
  expect(result).toRenderTo(d`
107
- (a, b) -> int
121
+ (a, b)
108
122
  `);
109
123
  });
110
- it("renders a simple call signature for a class function", () => {
124
+ it("renders a simple call signature with args and kwargs", () => {
111
125
  const result = toSourceText([
112
- <py.CallSignature parameters={["a", "b"]} classFunction />,
126
+ <py.CallSignature
127
+ parameters={[{ name: "a" }, { name: "b" }]}
128
+ args
129
+ kwargs
130
+ />,
113
131
  ]);
114
132
  expect(result).toRenderTo(d`
115
- (cls, a, b)
133
+ (a, b, *args, **kwargs)
116
134
  `);
117
135
  });
118
- it("renders a simple call signature for an instance function", () => {
136
+ it("renders a simple call signature with type parameters", () => {
119
137
  const result = toSourceText([
120
- <py.CallSignature parameters={["a", "b"]} instanceFunction />,
138
+ <py.CallSignature
139
+ parameters={[{ name: "a" }, { name: "b" }]}
140
+ typeParameters={["T", "U"]}
141
+ />,
121
142
  ]);
122
143
  expect(result).toRenderTo(d`
123
- (self, a, b)
144
+ [T, U](a, b)
124
145
  `);
125
146
  });
126
- it("throws an error for a call signature that's instance and class function at the same time", () => {
127
- expect(() =>
128
- toSourceText([
129
- <py.CallSignature
130
- parameters={["a", "b"]}
131
- instanceFunction
132
- classFunction
133
- />,
134
- ]),
135
- ).toThrowError(/Cannot be both an instance function and a class function/);
136
- });
137
- it("renders a simple call signature with all properties", () => {
147
+ it("renders a simple call signature with return type", () => {
138
148
  const result = toSourceText([
139
149
  <py.CallSignature
140
- parameters={["a", "b"]}
141
- instanceFunction
142
- args
143
- kwargs
150
+ parameters={[{ name: "a" }, { name: "b" }]}
144
151
  returnType="int"
145
152
  />,
146
153
  ]);
147
154
  expect(result).toRenderTo(d`
148
- (self, a, b, *args, **kwargs) -> int
155
+ (a, b) -> int
149
156
  `);
150
157
  });
151
158
  });
@@ -155,8 +162,14 @@ describe("Call Signature - Parameter Descriptors", () => {
155
162
  const result = toSourceText([
156
163
  <py.CallSignature
157
164
  parameters={[
158
- { name: "a", type: "int" },
159
- { name: "b", type: "str" },
165
+ {
166
+ name: "a",
167
+ type: "int",
168
+ },
169
+ {
170
+ name: "b",
171
+ type: "str",
172
+ },
160
173
  ]}
161
174
  />,
162
175
  ]);
@@ -168,8 +181,14 @@ describe("Call Signature - Parameter Descriptors", () => {
168
181
  const result = toSourceText([
169
182
  <py.CallSignature
170
183
  parameters={[
171
- { name: "a", type: "int" },
172
- { name: "b", type: "str" },
184
+ {
185
+ name: "a",
186
+ type: "int",
187
+ },
188
+ {
189
+ name: "b",
190
+ type: "str",
191
+ },
173
192
  ]}
174
193
  args
175
194
  kwargs
@@ -183,8 +202,14 @@ describe("Call Signature - Parameter Descriptors", () => {
183
202
  const result = toSourceText([
184
203
  <py.CallSignature
185
204
  parameters={[
186
- { name: "a", type: "int" },
187
- { name: "b", type: "str" },
205
+ {
206
+ name: "a",
207
+ type: "int",
208
+ },
209
+ {
210
+ name: "b",
211
+ type: "str",
212
+ },
188
213
  ]}
189
214
  returnType="int"
190
215
  />,
@@ -197,45 +222,41 @@ describe("Call Signature - Parameter Descriptors", () => {
197
222
  const result = toSourceText([
198
223
  <py.CallSignature
199
224
  parameters={[
200
- { name: "a", type: "int" },
201
- { name: "b", type: "str" },
225
+ {
226
+ name: "a",
227
+ type: "int",
228
+ },
229
+ {
230
+ name: "b",
231
+ type: "str",
232
+ },
202
233
  ]}
203
- classFunction
204
234
  />,
205
235
  ]);
206
236
  expect(result).toRenderTo(d`
207
- (cls, a: int, b: str)
208
- `);
209
- });
210
- it("renders a call signature with parameter descriptors for an instance function", () => {
211
- const result = toSourceText([
212
- <py.CallSignature
213
- parameters={[
214
- { name: "a", type: "int" },
215
- { name: "b", type: "str" },
216
- ]}
217
- instanceFunction
218
- />,
219
- ]);
220
- expect(result).toRenderTo(d`
221
- (self, a: int, b: str)
237
+ (a: int, b: str)
222
238
  `);
223
239
  });
224
240
  it("renders a call signature with all", () => {
225
241
  const result = toSourceText([
226
242
  <py.CallSignature
227
243
  parameters={[
228
- { name: "a", type: "int" },
229
- { name: "b", type: "str" },
244
+ {
245
+ name: "a",
246
+ type: "int",
247
+ },
248
+ {
249
+ name: "b",
250
+ type: "str",
251
+ },
230
252
  ]}
231
- instanceFunction
232
253
  args
233
254
  kwargs
234
255
  returnType="int"
235
256
  />,
236
257
  ]);
237
258
  expect(result).toRenderTo(d`
238
- (self, a: int, b: str, *args, **kwargs) -> int
259
+ (a: int, b: str, *args, **kwargs) -> int
239
260
  `);
240
261
  });
241
262
  it("renders a more complex call signature with parameter descriptors", () => {
@@ -243,8 +264,15 @@ describe("Call Signature - Parameter Descriptors", () => {
243
264
  <py.CallSignature
244
265
  typeParameters={["T", "U"]}
245
266
  parameters={[
246
- { name: "a", type: "int" },
247
- { name: "b", type: "str", default: "default_value" },
267
+ {
268
+ name: "a",
269
+ type: "int",
270
+ },
271
+ {
272
+ name: "b",
273
+ type: "str",
274
+ default: "default_value",
275
+ },
248
276
  ]}
249
277
  returnType="int"
250
278
  />,
@@ -0,0 +1,53 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { describe, expect, it } from "vitest";
3
+ import * as py from "../src/index.js";
4
+ import { toSourceText } from "./utils.js";
5
+
6
+ describe("ClassMethodDeclaration", () => {
7
+ it("renders async class method", () => {
8
+ const decl = (
9
+ <py.StatementList>
10
+ <py.ClassDeclaration name="MyClass">
11
+ <py.StatementList>
12
+ <py.ClassMethodDeclaration
13
+ async
14
+ name="create"
15
+ returnType={"MyClass"}
16
+ >
17
+ return cls()
18
+ </py.ClassMethodDeclaration>
19
+ </py.StatementList>
20
+ </py.ClassDeclaration>
21
+ </py.StatementList>
22
+ );
23
+
24
+ expect(toSourceText([decl])).toBe(d`
25
+ class MyClass:
26
+ @classmethod
27
+ async def create(cls) -> MyClass:
28
+ return cls()
29
+
30
+
31
+ `);
32
+ });
33
+
34
+ it("renders class method with parameters", () => {
35
+ const parameters = [{ name: "x", type: "int" }];
36
+ const decl = (
37
+ <py.ClassDeclaration name="MyClass">
38
+ <py.ClassMethodDeclaration name="foo" parameters={parameters}>
39
+ self.attribute = "value"
40
+ </py.ClassMethodDeclaration>
41
+ </py.ClassDeclaration>
42
+ );
43
+
44
+ expect(toSourceText([decl])).toBe(d`
45
+ class MyClass:
46
+ @classmethod
47
+ def foo(cls, x: int):
48
+ self.attribute = "value"
49
+
50
+
51
+ `);
52
+ });
53
+ });
@@ -207,9 +207,9 @@ describe("Python Class - VariableDeclaration", () => {
207
207
  <py.StatementList>
208
208
  <py.VariableDeclaration instanceVariable name="just_name" />
209
209
  <py.VariableDeclaration
210
- instanceVariable
211
210
  name="name_and_type"
212
211
  type="number"
212
+ instanceVariable
213
213
  />
214
214
  <py.VariableDeclaration
215
215
  instanceVariable
@@ -269,9 +269,8 @@ describe("Python Class - VariableDeclaration", () => {
269
269
  initializer={42}
270
270
  instanceVariable
271
271
  />
272
- <py.FunctionDeclaration
272
+ <py.MethodDeclaration
273
273
  name="instanceMethod"
274
- instanceFunction
275
274
  refkey={classMethodRk}
276
275
  returnType="int"
277
276
  />
@@ -312,26 +311,24 @@ describe("Python Class - FunctionDeclaration", () => {
312
311
  <py.StatementList>
313
312
  <py.VariableDeclaration name="a" type="int" instanceVariable />
314
313
  <py.VariableDeclaration name="b" type="int" instanceVariable />
315
- <py.FunctionDeclaration
314
+ <py.MethodDeclaration
316
315
  name="my_method"
317
316
  parameters={[
318
317
  { name: "a", type: "int" },
319
318
  { name: "b", type: "int" },
320
319
  ]}
321
320
  returnType="int"
322
- instanceFunction
323
321
  refkey={methodRefkey}
324
322
  >
325
323
  return a + b
326
- </py.FunctionDeclaration>
327
- <py.FunctionDeclaration
324
+ </py.MethodDeclaration>
325
+ <py.ClassMethodDeclaration
328
326
  name="my_class_method"
329
- classFunction
330
327
  returnType="int"
331
328
  refkey={classMethodRefkey}
332
329
  >
333
330
  pass
334
- </py.FunctionDeclaration>
331
+ </py.ClassMethodDeclaration>
335
332
  <py.FunctionDeclaration
336
333
  name="my_standalone_function"
337
334
  returnType="int"
@@ -352,6 +349,7 @@ describe("Python Class - FunctionDeclaration", () => {
352
349
  def my_method(self, a: int, b: int) -> int:
353
350
  return a + b
354
351
 
352
+ @classmethod
355
353
  def my_class_method(cls) -> int:
356
354
  pass
357
355
 
@@ -0,0 +1,48 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { describe, expect, it } from "vitest";
3
+ import * as py from "../src/index.js";
4
+ import { toSourceText } from "./utils.js";
5
+
6
+ describe("ConstructorDeclaration", () => {
7
+ it("renders async constructor with return type", () => {
8
+ const decl = (
9
+ <py.StatementList>
10
+ <py.ClassDeclaration name="MyClass">
11
+ <py.StatementList>
12
+ <py.ConstructorDeclaration async returnType={"MyClass"}>
13
+ return super().__new__(cls)
14
+ </py.ConstructorDeclaration>
15
+ </py.StatementList>
16
+ </py.ClassDeclaration>
17
+ </py.StatementList>
18
+ );
19
+
20
+ expect(toSourceText([decl])).toBe(d`
21
+ class MyClass:
22
+ async def __new__(cls) -> MyClass:
23
+ return super().__new__(cls)
24
+
25
+
26
+ `);
27
+ });
28
+
29
+ it("renders __new__ with spread args", () => {
30
+ const decl = (
31
+ <py.ClassDeclaration name="MyClass">
32
+ <py.StatementList>
33
+ <py.ConstructorDeclaration args kwargs>
34
+ pass
35
+ </py.ConstructorDeclaration>
36
+ </py.StatementList>
37
+ </py.ClassDeclaration>
38
+ );
39
+
40
+ expect(toSourceText([decl])).toBe(d`
41
+ class MyClass:
42
+ def __new__(cls, *args, **kwargs):
43
+ pass
44
+
45
+
46
+ `);
47
+ });
48
+ });
@@ -0,0 +1,53 @@
1
+ import { d } from "@alloy-js/core/testing";
2
+ import { describe, expect, it } from "vitest";
3
+ import * as py from "../src/index.js";
4
+ import { toSourceText } from "./utils.js";
5
+
6
+ describe("Dunder/Constructor Declarations", () => {
7
+ it("renders dunder methods with parameters", () => {
8
+ const parameters = [{ name: "x", type: "int" }];
9
+ const decl = (
10
+ <py.ClassDeclaration name="MyClass">
11
+ <py.StatementList>
12
+ <py.DunderMethodDeclaration name="__init__" parameters={parameters}>
13
+ self.attribute = "value"
14
+ </py.DunderMethodDeclaration>
15
+ <py.DunderMethodDeclaration name="__repr__" parameters={parameters}>
16
+ return "MyClass"
17
+ </py.DunderMethodDeclaration>
18
+ </py.StatementList>
19
+ </py.ClassDeclaration>
20
+ );
21
+
22
+ expect(toSourceText([decl])).toBe(d`
23
+ class MyClass:
24
+ def __init__(self, x: int):
25
+ self.attribute = "value"
26
+
27
+ def __repr__(self, x: int):
28
+ return "MyClass"
29
+
30
+
31
+ `);
32
+ });
33
+
34
+ it("renders dunder methods __new__", () => {
35
+ const decl = (
36
+ <py.ClassDeclaration name="MyClass">
37
+ <py.StatementList>
38
+ <py.ConstructorDeclaration args kwargs>
39
+ pass
40
+ </py.ConstructorDeclaration>
41
+ </py.StatementList>
42
+ </py.ClassDeclaration>
43
+ );
44
+
45
+ expect(toSourceText([decl])).toBe(d`
46
+ class MyClass:
47
+ def __new__(cls, *args, **kwargs):
48
+ pass
49
+
50
+
51
+ `);
52
+ });
53
+ });
@@ -6,10 +6,10 @@ import * as py from "../src/index.js";
6
6
  import { toSourceText } from "./utils.jsx";
7
7
 
8
8
  describe("Python Enum", () => {
9
- it("classic enum with explicit values", () => {
9
+ it("class enum with explicit values", () => {
10
10
  const result = toSourceText(
11
11
  [
12
- <py.EnumDeclaration
12
+ <py.ClassEnumDeclaration
13
13
  name="Color"
14
14
  baseType="IntEnum"
15
15
  members={[
@@ -34,10 +34,10 @@ describe("Python Enum", () => {
34
34
  expect(result).toRenderTo(expected);
35
35
  });
36
36
 
37
- it("classic enum with jsValues", () => {
37
+ it("class enum with jsValues", () => {
38
38
  const result = toSourceText(
39
39
  [
40
- <py.EnumDeclaration
40
+ <py.ClassEnumDeclaration
41
41
  name="Color"
42
42
  baseType="IntEnum"
43
43
  members={[
@@ -62,13 +62,13 @@ describe("Python Enum", () => {
62
62
  expect(result).toRenderTo(expected);
63
63
  });
64
64
 
65
- it("classic enum with a refkey as jsValue", () => {
65
+ it("class enum with a refkey as jsValue", () => {
66
66
  const result = toSourceText(
67
67
  [
68
68
  <py.StatementList>
69
69
  <py.ClassDeclaration name="Dog" refkey={refkey("Dog")} />
70
70
  <py.ClassDeclaration name="Cat" refkey={refkey("Cat")} />
71
- <py.EnumDeclaration
71
+ <py.ClassEnumDeclaration
72
72
  name="Animal"
73
73
  baseType="Enum"
74
74
  members={[
@@ -98,12 +98,12 @@ describe("Python Enum", () => {
98
98
  expect(result).toRenderTo(expected);
99
99
  });
100
100
 
101
- it("enum with auto() values", () => {
101
+ it("class enum with auto() values", () => {
102
102
  const result = toSourceText(
103
103
  [
104
- <py.EnumDeclaration
104
+ <py.ClassEnumDeclaration
105
105
  name="Animal"
106
- style="auto"
106
+ auto
107
107
  members={[{ name: "DOG" }, { name: "CAT" }, { name: "RABBIT" }]}
108
108
  />,
109
109
  ],
@@ -123,13 +123,13 @@ describe("Python Enum", () => {
123
123
  expect(result).toRenderTo(expected);
124
124
  });
125
125
 
126
- it("enum with mixed manual and auto() values", () => {
126
+ it("class enum with mixed manual and auto() values", () => {
127
127
  const result = toSourceText(
128
128
  [
129
- <py.EnumDeclaration
129
+ <py.ClassEnumDeclaration
130
130
  name="Permission"
131
131
  baseType="Flag"
132
- style="auto"
132
+ auto
133
133
  members={[
134
134
  { name: "READ", value: 1 },
135
135
  { name: "WRITE" },
@@ -157,9 +157,8 @@ describe("Python Enum", () => {
157
157
  it("functional enum with list", () => {
158
158
  const result = toSourceText(
159
159
  [
160
- <py.EnumDeclaration
160
+ <py.FunctionalEnumDeclaration
161
161
  name="Direction"
162
- style="functional"
163
162
  members={[
164
163
  { name: "NORTH" },
165
164
  { name: "SOUTH" },
@@ -181,9 +180,8 @@ describe("Python Enum", () => {
181
180
  it("functional enum with mapping", () => {
182
181
  const result = toSourceText(
183
182
  [
184
- <py.EnumDeclaration
183
+ <py.FunctionalEnumDeclaration
185
184
  name="Priority"
186
- style="functional"
187
185
  members={[
188
186
  { name: "HIGH", value: 1 },
189
187
  { name: "MEDIUM", value: 2 },
@@ -118,11 +118,10 @@ it("uses import from external library in multiple class methods", () => {
118
118
  instanceVariable
119
119
  refkey={refkey("some_var")}
120
120
  />
121
- <py.FunctionDeclaration
122
- name="getUser"
121
+ <py.MethodDeclaration
122
+ name={"getUser"}
123
123
  parameters={[{ name: "userId", type: "int" }]}
124
124
  returnType={py.requestsModule["models"]["Response"]}
125
- instanceFunction
126
125
  >
127
126
  <py.StatementList>
128
127
  <py.VariableDeclaration
@@ -138,12 +137,11 @@ it("uses import from external library in multiple class methods", () => {
138
137
  return response.json()
139
138
  `}
140
139
  </py.StatementList>
141
- </py.FunctionDeclaration>
142
- <py.FunctionDeclaration
140
+ </py.MethodDeclaration>
141
+ <py.MethodDeclaration
143
142
  name={"createUser"}
144
143
  parameters={[{ name: "userName", type: "string" }]}
145
144
  returnType={py.requestsModule["models"]["Response"]}
146
- instanceFunction
147
145
  >
148
146
  <py.StatementList>
149
147
  <py.VariableDeclaration
@@ -159,7 +157,7 @@ it("uses import from external library in multiple class methods", () => {
159
157
  return response.json()
160
158
  `}
161
159
  </py.StatementList>
162
- </py.FunctionDeclaration>
160
+ </py.MethodDeclaration>
163
161
  </py.StatementList>
164
162
  </py.ClassDeclaration>,
165
163
  ];