@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
@@ -5,20 +5,19 @@ import {
5
5
  For,
6
6
  Show,
7
7
  SymbolSlot,
8
- useContext,
9
8
  } from "@alloy-js/core";
10
- import { ParameterDescriptor } from "../parameter-descriptor.js";
9
+ import {
10
+ isParameterDescriptor,
11
+ ParameterDescriptor,
12
+ } from "../parameter-descriptor.js";
11
13
  import { createPythonSymbol } from "../symbol-creation.js";
12
14
  import { PythonOutputSymbol } from "../symbols/index.js";
13
15
  import { Atom } from "./Atom.jsx";
14
- import { PythonSourceFileContext } from "./SourceFile.jsx";
15
16
 
16
17
  export interface CallSignatureParametersProps {
17
- readonly parameters?: ParameterDescriptor[] | string[];
18
+ readonly parameters?: (ParameterDescriptor | string)[];
18
19
  readonly args?: boolean;
19
20
  readonly kwargs?: boolean;
20
- readonly instanceFunction?: boolean;
21
- readonly classFunction?: boolean;
22
21
  }
23
22
 
24
23
  /**
@@ -35,33 +34,10 @@ export interface CallSignatureParametersProps {
35
34
  * ```
36
35
  */
37
36
  export function CallSignatureParameters(props: CallSignatureParametersProps) {
38
- // Validate that only one of instanceFunction or classFunction is true
39
- if (props.instanceFunction && props.classFunction) {
40
- throw new Error("Cannot be both an instance function and a class function");
41
- }
42
-
43
- const sfContext = useContext(PythonSourceFileContext);
44
- const module = sfContext?.module;
45
37
  const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
46
38
 
47
39
  const parameterList = computed(() => {
48
40
  const params = [];
49
-
50
- // Add self/cls parameter if instance or class function
51
- if (props.instanceFunction) {
52
- params.push(
53
- parameter({
54
- symbol: createPythonSymbol("self", { module: module }),
55
- }),
56
- );
57
- } else if (props.classFunction) {
58
- params.push(
59
- parameter({
60
- symbol: createPythonSymbol("cls", { module: module }),
61
- }),
62
- );
63
- }
64
-
65
41
  // Add regular parameters
66
42
  parameters.forEach((param) => {
67
43
  params.push(parameter(param));
@@ -95,16 +71,7 @@ function parameter(param: DeclaredParameterDescriptor) {
95
71
  <Show when={!!param.type}>
96
72
  : <TypeSlot>{param.type}</TypeSlot>
97
73
  </Show>
98
- <Show when={!!param.optional}>
99
- <Show when={!param.type}>=</Show>
100
- <Show when={!!param.type}> = </Show>
101
- <>
102
- {param.default ?
103
- <Atom jsValue={param.default} />
104
- : "None"}
105
- </>
106
- </Show>
107
- <Show when={!param.optional && param.default !== undefined}>
74
+ <Show when={param.default !== undefined}>
108
75
  <Show when={!param.type}>=</Show>
109
76
  <Show when={!!param.type}> = </Show>
110
77
  <>
@@ -122,19 +89,10 @@ interface DeclaredParameterDescriptor
122
89
  }
123
90
 
124
91
  function normalizeAndDeclareParameters(
125
- parameters: ParameterDescriptor[] | string[],
92
+ parameters: (ParameterDescriptor | string)[],
126
93
  ): DeclaredParameterDescriptor[] {
127
- if (parameters.length === 0) {
128
- return [];
129
- }
130
- if (typeof parameters[0] === "string") {
131
- return (parameters as string[]).map((paramName) => {
132
- const symbol = createPythonSymbol(paramName, {}, "parameter");
133
-
134
- return { refkeys: symbol.refkeys, symbol };
135
- });
136
- } else {
137
- return (parameters as ParameterDescriptor[]).map((param) => {
94
+ return parameters.map((param) => {
95
+ if (isParameterDescriptor(param)) {
138
96
  const TypeSlot = createSymbolSlot();
139
97
 
140
98
  const symbol = createPythonSymbol(
@@ -151,17 +109,19 @@ function normalizeAndDeclareParameters(
151
109
  symbol,
152
110
  TypeSlot,
153
111
  };
154
- });
155
- }
112
+ } else {
113
+ const symbol = createPythonSymbol(param, {}, "parameter");
114
+ return { refkeys: symbol.refkeys, symbol };
115
+ }
116
+ });
156
117
  }
157
118
 
158
119
  export interface CallSignatureProps {
159
120
  /**
160
- * The parameters to the call signature. Can be an array of strings for parameters
161
- * which don't have a type or a default value. Otherwise, it's an array of
162
- * {@link ParameterDescriptor}s.
121
+ * The parameters to the call signature. Can be an array of strings (for parameters
122
+ * which don't have a type or a default value) or {@link ParameterDescriptor}s.
163
123
  */
164
- parameters?: ParameterDescriptor[] | string[];
124
+ parameters?: (ParameterDescriptor | string)[];
165
125
 
166
126
  /**
167
127
  * The type parameters of the call signature, e.g. for a generic function.
@@ -179,16 +139,6 @@ export interface CallSignatureProps {
179
139
  */
180
140
  kwargs?: boolean;
181
141
 
182
- /**
183
- * Indicates that this is an instance function.
184
- */
185
- instanceFunction?: boolean; // true if this is an instance function
186
-
187
- /**
188
- * Indicates that this is a class function.
189
- */
190
- classFunction?: boolean; // true if this is a class function
191
-
192
142
  /**
193
143
  * The return type of the function.
194
144
  */
@@ -221,8 +171,6 @@ export function CallSignature(props: CallSignatureProps) {
221
171
  parameters={props.parameters}
222
172
  args={props.args}
223
173
  kwargs={props.kwargs}
224
- instanceFunction={props.instanceFunction}
225
- classFunction={props.classFunction}
226
174
  />
227
175
  );
228
176
  const typeParams =
@@ -0,0 +1,34 @@
1
+ import { createMethodSymbol } from "../symbols/factories.js";
2
+ import type { CommonFunctionProps } from "./FunctionBase.js";
3
+ import { MethodDeclarationBase } from "./MethodBase.js";
4
+
5
+ /**
6
+ * A Python class method declaration component.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * <py.ClassMethodDeclaration name="create" parameters={[{ name: "value", type: "str" }]}>
11
+ * return cls(value)
12
+ * </py.ClassMethodDeclaration>
13
+ * ```
14
+ * Generates:
15
+ * ```python
16
+ * @classmethod
17
+ * def create(cls, value: str) -> None:
18
+ * return cls(value)
19
+ * ```
20
+ */
21
+ export interface ClassMethodDeclarationProps extends CommonFunctionProps {
22
+ abstract?: boolean;
23
+ }
24
+
25
+ export function ClassMethodDeclaration(props: ClassMethodDeclarationProps) {
26
+ const sym = createMethodSymbol(props.name, { refkeys: props.refkey });
27
+ return (
28
+ <>
29
+ {"@classmethod"}
30
+ <hbr />
31
+ <MethodDeclarationBase functionType="class" {...props} sym={sym} />
32
+ </>
33
+ );
34
+ }
@@ -0,0 +1,37 @@
1
+ import { namekey } from "@alloy-js/core";
2
+ import { createMethodSymbol } from "../symbols/factories.js";
3
+ import type { CommonFunctionProps } from "./FunctionBase.js";
4
+ import { MethodDeclarationBase } from "./MethodBase.js";
5
+
6
+ /**
7
+ * A Python constructor declaration for `__new__`.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * <py.ConstructorDeclaration parameters={[{ name: "value", type: "int" }]}>
12
+ * pass
13
+ * </py.ConstructorDeclaration>
14
+ * ```
15
+ * Generates:
16
+ * ```python
17
+ * def __new__(cls, value: int):
18
+ * pass
19
+ * ```
20
+ */
21
+ export interface ConstructorDeclarationProps
22
+ extends Omit<CommonFunctionProps, "name"> {
23
+ abstract?: boolean;
24
+ }
25
+
26
+ export function ConstructorDeclaration(props: ConstructorDeclarationProps) {
27
+ const name = namekey("__new__", { ignoreNamePolicy: true });
28
+ const sym = createMethodSymbol(name, { refkeys: props.refkey });
29
+ return (
30
+ <MethodDeclarationBase
31
+ {...props}
32
+ name={name}
33
+ functionType="class"
34
+ sym={sym}
35
+ />
36
+ );
37
+ }
@@ -0,0 +1,30 @@
1
+ import { isNamekey, namekey } from "@alloy-js/core";
2
+ import type { CommonFunctionProps } from "./FunctionBase.js";
3
+ import { MethodDeclaration } from "./MethodDeclaration.js";
4
+
5
+ /**
6
+ * A Python dunder method declaration.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * <py.DunderMethodDeclaration name="__repr__" returnType="str">
11
+ * return f"<MyType>"
12
+ * </py.DunderMethodDeclaration>
13
+ * ```
14
+ * Generates:
15
+ * ```python
16
+ * def __repr__(self) -> str:
17
+ * return f"<MyType>"
18
+ * ```
19
+ */
20
+ export interface DunderMethodDeclarationProps extends CommonFunctionProps {
21
+ abstract?: boolean;
22
+ }
23
+
24
+ export function DunderMethodDeclaration(props: DunderMethodDeclarationProps) {
25
+ const finalName =
26
+ isNamekey(props.name) ?
27
+ props.name
28
+ : namekey(props.name as string, { ignoreNamePolicy: true });
29
+ return <MethodDeclaration {...props} name={finalName} />;
30
+ }
@@ -8,11 +8,10 @@ import { enumModule } from "../builtins/python.js";
8
8
  import { createPythonSymbol } from "../symbol-creation.js";
9
9
  import { BaseDeclarationProps } from "./Declaration.js";
10
10
  import { EnumMember, EnumMemberProps } from "./EnumMember.js";
11
- import { SimpleCommentBlock } from "./index.js";
12
11
  import { MemberScope } from "./MemberScope.jsx";
13
12
  import { PythonBlock } from "./PythonBlock.jsx";
14
13
 
15
- export interface EnumProps extends BaseDeclarationProps {
14
+ export interface EnumPropsBase extends BaseDeclarationProps {
16
15
  /**
17
16
  * The base type of the enum. One of: 'Enum', 'IntEnum', 'StrEnum', 'Flag', 'IntFlag'.
18
17
  * Defaults to 'Enum'.
@@ -22,47 +21,13 @@ export interface EnumProps extends BaseDeclarationProps {
22
21
  * Members of the enum as an array of objects.
23
22
  */
24
23
  members?: Array<EnumMemberProps>;
25
- /**
26
- * The enum style: 'classic' (default), 'auto', or 'functional'.
27
- */
28
- style?: "classic" | "auto" | "functional";
29
24
  /**
30
25
  * Optional docstring for the enum.
31
26
  */
32
27
  doc?: Children;
33
28
  }
34
29
 
35
- /**
36
- * A Python enum declaration, following https://docs.python.org/3.11/library/enum.html.
37
- *
38
- * @example
39
- * ```tsx
40
- * <EnumDeclaration name="Direction" style="functional">
41
- * members={[
42
- * { name: "NORTH" },
43
- * { name: "SOUTH" },
44
- * { name: "EAST" },
45
- * { name: "WEST" },
46
- * ]}
47
- * />
48
- * ```
49
- * This will generate:
50
- * ```python
51
- * from enum import Enum
52
- * class Direction(Enum):
53
- * NORTH = "NORTH"
54
- * SOUTH = "SOUTH"
55
- * EAST = "EAST"
56
- * WEST = "WEST"
57
- * ```
58
- */
59
- export function EnumDeclaration(props: EnumProps) {
60
- // Handle enum styles
61
- if (props.style === "functional") {
62
- return <FunctionalEnumDeclaration {...props} />;
63
- }
64
- return <ClassEnumDeclaration {...props} />;
65
- }
30
+ export interface FunctionalEnumProps extends EnumPropsBase {}
66
31
 
67
32
  /**
68
33
  * Create a Python enum using the functional syntax.
@@ -105,7 +70,7 @@ export function EnumDeclaration(props: EnumProps) {
105
70
  * Status = Enum('Status', {'PENDING': 1, 'ACTIVE': 2, 'INACTIVE': 3})
106
71
  * ```
107
72
  */
108
- export function FunctionalEnumDeclaration(props: EnumProps) {
73
+ export function FunctionalEnumDeclaration(props: FunctionalEnumProps) {
109
74
  const sym = createPythonSymbol(
110
75
  props.name,
111
76
  {
@@ -143,6 +108,13 @@ export function FunctionalEnumDeclaration(props: EnumProps) {
143
108
  );
144
109
  }
145
110
 
111
+ export interface ClassEnumProps extends EnumPropsBase {
112
+ /**
113
+ * Indicates that the enum members should be auto-generated.
114
+ */
115
+ auto?: boolean;
116
+ }
117
+
146
118
  /**
147
119
  * Create a Python enum using the class-based syntax.
148
120
  *
@@ -212,7 +184,7 @@ export function FunctionalEnumDeclaration(props: EnumProps) {
212
184
  * BLUE = auto()
213
185
  * ```
214
186
  */
215
- export function ClassEnumDeclaration(props: EnumProps) {
187
+ export function ClassEnumDeclaration(props: ClassEnumProps) {
216
188
  const baseType = props.baseType || "Enum";
217
189
  const sym = createPythonSymbol(
218
190
  props.name,
@@ -224,20 +196,20 @@ export function ClassEnumDeclaration(props: EnumProps) {
224
196
  let memberList: Array<EnumMemberProps> = (props.members ?? []).map((m) =>
225
197
  m.value === undefined ? { ...m, auto: false } : m,
226
198
  );
227
- if (props.style === "auto") {
199
+ if (props.auto) {
228
200
  memberList = memberList.map((m) =>
229
201
  m.value === undefined ? { name: m.name, auto: true } : m,
230
202
  );
231
203
  }
232
204
  return (
233
205
  <CoreDeclaration symbol={sym}>
234
- <Show when={Boolean(props.doc)}>
235
- <SimpleCommentBlock children={props.doc} />
236
- <hbr />
237
- </Show>
238
206
  class {sym.name}({enumModule["."][baseType]})
239
207
  <MemberScope ownerSymbol={sym}>
240
208
  <PythonBlock opener=":">
209
+ <Show when={Boolean(props.doc)}>
210
+ {props.doc}
211
+ <hbr />
212
+ </Show>
241
213
  <For each={memberList} hardline>
242
214
  {(member) => (
243
215
  <EnumMember
@@ -3,7 +3,7 @@ import { enumModule } from "../builtins/python.js";
3
3
  import { createPythonSymbol } from "../symbol-creation.js";
4
4
  import { PythonOutputSymbol } from "../symbols/index.js";
5
5
  import { Atom } from "./Atom.jsx";
6
- import { SimpleInlineComment } from "./index.js";
6
+ import { SimpleInlineMemberComment } from "./PyDoc.jsx";
7
7
 
8
8
  export interface EnumMemberProps {
9
9
  /**
@@ -73,7 +73,7 @@ export function EnumMember(props: EnumMemberProps) {
73
73
  <>
74
74
  '{sym.name}'<Show when={valueCode !== undefined}> : {valueCode}</Show>
75
75
  <Show when={props.doc !== undefined}>
76
- <SimpleInlineComment>{props.doc}</SimpleInlineComment>
76
+ <SimpleInlineMemberComment>{props.doc}</SimpleInlineMemberComment>
77
77
  </Show>
78
78
  </>
79
79
  );
@@ -83,7 +83,7 @@ export function EnumMember(props: EnumMemberProps) {
83
83
  {sym.name}
84
84
  <Show when={valueCode !== undefined}> = {valueCode}</Show>
85
85
  <Show when={props.doc !== undefined}>
86
- <SimpleInlineComment>{props.doc}</SimpleInlineComment>
86
+ <SimpleInlineMemberComment>{props.doc}</SimpleInlineMemberComment>
87
87
  </Show>
88
88
  </>
89
89
  );
@@ -0,0 +1,88 @@
1
+ import { Name, Show } from "@alloy-js/core";
2
+ import { PythonOutputSymbol } from "../index.js";
3
+ import { getCallSignatureProps } from "../utils.js";
4
+ import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
5
+ import { BaseDeclarationProps, Declaration } from "./Declaration.js";
6
+ import { LexicalScope } from "./LexicalScope.jsx";
7
+ import { PythonBlock } from "./PythonBlock.jsx";
8
+
9
+ /**
10
+ * Shared base interface for function-like components.
11
+ *
12
+ * @remarks
13
+ * This interface is consumed by public components like `FunctionDeclaration`,
14
+ * `MethodDeclaration`, and property method helpers. It combines declaration
15
+ * metadata with call signature shape.
16
+ */
17
+ export interface CommonFunctionProps
18
+ extends BaseDeclarationProps,
19
+ CallSignatureProps {
20
+ /** Indicates that the function is async. */
21
+ async?: boolean;
22
+ }
23
+
24
+ /**
25
+ * Props for `BaseFunctionDeclaration`.
26
+ *
27
+ * @remarks
28
+ * This low-level component powers function/method-like declarations by
29
+ * handling symbol creation, parameter injection for instance/class methods,
30
+ * and common rendering concerns. Intended primarily for internal reuse.
31
+ */
32
+ export interface BaseFunctionDeclarationProps extends CommonFunctionProps {
33
+ /** Indicates the type of function. */
34
+ functionType?: "instance" | "class" | "static";
35
+ /** Pre-created symbol to render. Must be provided by caller. */
36
+ sym: PythonOutputSymbol;
37
+ }
38
+
39
+ /**
40
+ * Base function declaration that handles instance/class/static parameter injection
41
+ * and symbol creation. Exported for internal reuse by method-like components.
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * // Not typically used directly. Prefer higher-level components.
46
+ * <BaseFunctionDeclaration name="helper" parameters={[{ name: "x" }]}>
47
+ * return x
48
+ * </BaseFunctionDeclaration>
49
+ * ```
50
+ * Generates:
51
+ * ```python
52
+ * def helper(x):
53
+ * return x
54
+ * ```
55
+ */
56
+ export function BaseFunctionDeclaration(props: BaseFunctionDeclarationProps) {
57
+ const asyncKwd = props.async ? "async " : "";
58
+ let parameters;
59
+ switch (props.functionType) {
60
+ case "instance":
61
+ parameters = [{ name: "self" }, ...(props.parameters || [])];
62
+ break;
63
+ case "class":
64
+ parameters = [{ name: "cls" }, ...(props.parameters || [])];
65
+ break;
66
+ default:
67
+ parameters = props.parameters;
68
+ }
69
+ const sym: PythonOutputSymbol = props.sym;
70
+
71
+ return (
72
+ <>
73
+ <Declaration {...props} nameKind="function" symbol={sym}>
74
+ {asyncKwd}def <Name />
75
+ <LexicalScope name={sym.name}>
76
+ <CallSignature
77
+ {...getCallSignatureProps(props, {})}
78
+ parameters={parameters}
79
+ />
80
+ <PythonBlock opener=":">
81
+ <Show when={Boolean(props.doc)}>{props.doc}</Show>
82
+ {props.children ?? "pass"}
83
+ </PythonBlock>
84
+ </LexicalScope>
85
+ </Declaration>
86
+ </>
87
+ );
88
+ }
@@ -1,16 +1,11 @@
1
- import { emitSymbol, Name, namekey, Show } from "@alloy-js/core";
2
- import { createPythonSymbol } from "../symbol-creation.js";
3
- import { getCallSignatureProps } from "../utils.js";
4
- import { CallSignature, CallSignatureProps } from "./CallSignature.jsx";
5
- import { BaseDeclarationProps, Declaration } from "./Declaration.js";
6
- import { PythonBlock } from "./PythonBlock.jsx";
7
- import { LexicalScope } from "./index.js";
1
+ import { createFunctionSymbol } from "../symbols/factories.js";
2
+ import type { CommonFunctionProps } from "./FunctionBase.js";
3
+ import { BaseFunctionDeclaration } from "./FunctionBase.js";
8
4
 
9
- export interface FunctionDeclarationProps
10
- extends BaseDeclarationProps,
11
- CallSignatureProps {
12
- async?: boolean;
13
- }
5
+ // Types are sourced from FunctionBase to avoid duplicate exports
6
+
7
+ // Clean public interface extending common properties
8
+ export interface FunctionDeclarationProps extends CommonFunctionProps {}
14
9
 
15
10
  /**
16
11
  * A Python function declaration.
@@ -18,84 +13,25 @@ export interface FunctionDeclarationProps
18
13
  * @example
19
14
  * ```tsx
20
15
  * <FunctionDeclaration
21
- * name="my_function"
22
- * returnType="int"
23
- * parameters=[{name: "a", type: "int"},{name: "b", type: "str"}]>
16
+ * name="my_function"
17
+ * returnType="int"
18
+ * parameters={[{ name: "a", type: { children: "int" } }, { name: "b", type: { children: "str" } }]}
19
+ * >
24
20
  * return a + b
25
21
  * </FunctionDeclaration>
26
22
  * ```
27
23
  * This will generate:
28
24
  * ```python
29
25
  * def my_function(a: int, b: str) -> int:
30
- * return a + b
31
- * ```
32
- */
33
- export function FunctionDeclaration(props: FunctionDeclarationProps) {
34
- const asyncKwd = props.async ? "async " : "";
35
- const callSignatureProps = getCallSignatureProps(props, {});
36
- const sym = createPythonSymbol(
37
- props.name,
38
- {
39
- instance: props.instanceFunction,
40
- refkeys: props.refkey,
41
- },
42
- "function",
43
- );
44
- emitSymbol(sym);
45
-
46
- return (
47
- <>
48
- <Declaration {...props} nameKind="function" symbol={sym}>
49
- {asyncKwd}def <Name />
50
- <LexicalScope name={sym.name}>
51
- <CallSignature
52
- {...callSignatureProps}
53
- returnType={props.returnType}
54
- />
55
- <PythonBlock opener=":">
56
- <Show when={Boolean(props.doc)}>{props.doc}</Show>
57
- {props.children ?? "pass"}
58
- </PythonBlock>
59
- </LexicalScope>
60
- </Declaration>
61
- </>
62
- );
63
- }
64
-
65
- export interface InitFunctionDeclarationProps
66
- extends Omit<
67
- FunctionDeclarationProps,
68
- "name" | "instanceFunction" | "classFunction"
69
- > {}
70
-
71
- /**
72
- * A Python `__init__` function declaration.
73
- *
74
- * @example
75
- * ```tsx
76
- * <InitFunctionDeclaration>
77
- * self.attribute = "value"
78
- * </InitFunctionDeclaration>
79
- * ```
80
- * This will generate:
81
- * ```python
82
- * def __init__(self: MyClass) -> None:
83
- * self.attribute = "value"
26
+ * return a + b
84
27
  * ```
85
28
  *
86
29
  * @remarks
87
- *
88
- * This is a convenience component that sets the name to `__init__`, marks it as
89
- * an instance function, and forces the name to be `__init__` without applying
90
- * the name policy.
30
+ * This component creates a Python function declaration with optional type annotations,
31
+ * parameters, and return types. It supports async functions and automatically
32
+ * handles symbol creation and emission.
91
33
  */
92
- export function InitFunctionDeclaration(props: InitFunctionDeclarationProps) {
93
- return (
94
- <FunctionDeclaration
95
- {...props}
96
- name={namekey("__init__", { ignoreNamePolicy: true })}
97
- instanceFunction={true}
98
- classFunction={false}
99
- />
100
- );
34
+ export function FunctionDeclaration(props: FunctionDeclarationProps) {
35
+ const sym = createFunctionSymbol(props.name, { refkeys: props.refkey });
36
+ return <BaseFunctionDeclaration {...props} sym={sym} />;
101
37
  }
@@ -0,0 +1,53 @@
1
+ import { abcModule } from "../builtins/python.js";
2
+ import {
3
+ BaseFunctionDeclaration,
4
+ BaseFunctionDeclarationProps,
5
+ CommonFunctionProps,
6
+ } from "./FunctionBase.js";
7
+
8
+ /**
9
+ * Shared base props for all method-like declarations.
10
+ */
11
+ export interface MethodDeclarationBaseProps extends CommonFunctionProps {
12
+ /** Indicates that the method is abstract. */
13
+ abstract?: boolean;
14
+ }
15
+
16
+ /**
17
+ * Internal base for method-like components.
18
+ *
19
+ * @remarks
20
+ * Applies `@abstractmethod` when requested and delegates to
21
+ * `BaseFunctionDeclaration` for the function/method body.
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * <MethodDeclarationBase name="do_work" functionType="instance">
26
+ * return 1
27
+ * </MethodDeclarationBase>
28
+ * ```
29
+ * Generates:
30
+ * ```python
31
+ * def do_work(self):
32
+ * return 1
33
+ * ```
34
+ */
35
+ export function MethodDeclarationBase(
36
+ props: MethodDeclarationBaseProps &
37
+ Pick<BaseFunctionDeclarationProps, "functionType" | "sym">,
38
+ ) {
39
+ const abstractMethod =
40
+ props.abstract ?
41
+ <>
42
+ @{abcModule["."].abstractmethod}
43
+ <hbr />
44
+ </>
45
+ : undefined;
46
+
47
+ return (
48
+ <>
49
+ {abstractMethod}
50
+ <BaseFunctionDeclaration {...props} />
51
+ </>
52
+ );
53
+ }
@@ -0,0 +1,27 @@
1
+ import { createMethodSymbol } from "../symbols/factories.js";
2
+ import type { MethodDeclarationBaseProps } from "./MethodBase.js";
3
+ import { MethodDeclarationBase } from "./MethodBase.js";
4
+
5
+ /**
6
+ * A Python instance method declaration component.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * <py.MethodDeclaration name="do_work" parameters={[{ name: "value", type: "int" }]}>
11
+ * return value * 2
12
+ * </py.MethodDeclaration>
13
+ * ```
14
+ * Generates:
15
+ * ```python
16
+ * def do_work(self, value: int) -> None:
17
+ * return value * 2
18
+ * ```
19
+ *
20
+ * @remarks
21
+ * Automatically injects the `self` parameter for instance methods and enforces
22
+ * that the declaration appears within a class body.
23
+ */
24
+ export function MethodDeclaration(props: MethodDeclarationBaseProps) {
25
+ const sym = createMethodSymbol(props.name, { refkeys: props.refkey });
26
+ return <MethodDeclarationBase functionType="instance" {...props} sym={sym} />;
27
+ }