@alloy-js/python 0.0.1

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 (206) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/LICENSE +7 -0
  3. package/api-extractor.json +4 -0
  4. package/dist/src/builtins/python.d.ts +9 -0
  5. package/dist/src/builtins/python.d.ts.map +1 -0
  6. package/dist/src/builtins/python.js +17 -0
  7. package/dist/src/components/Atom.d.ts +19 -0
  8. package/dist/src/components/Atom.d.ts.map +1 -0
  9. package/dist/src/components/Atom.js +82 -0
  10. package/dist/src/components/CallSignature.d.ts +79 -0
  11. package/dist/src/components/CallSignature.d.ts.map +1 -0
  12. package/dist/src/components/CallSignature.js +201 -0
  13. package/dist/src/components/ClassDeclaration.d.ts +37 -0
  14. package/dist/src/components/ClassDeclaration.d.ts.map +1 -0
  15. package/dist/src/components/ClassDeclaration.js +83 -0
  16. package/dist/src/components/ClassInstantiation.d.ts +24 -0
  17. package/dist/src/components/ClassInstantiation.d.ts.map +1 -0
  18. package/dist/src/components/ClassInstantiation.js +35 -0
  19. package/dist/src/components/Declaration.d.ts +48 -0
  20. package/dist/src/components/Declaration.d.ts.map +1 -0
  21. package/dist/src/components/Declaration.js +37 -0
  22. package/dist/src/components/EnumDeclaration.d.ts +164 -0
  23. package/dist/src/components/EnumDeclaration.d.ts.map +1 -0
  24. package/dist/src/components/EnumDeclaration.js +278 -0
  25. package/dist/src/components/EnumMember.d.ts +46 -0
  26. package/dist/src/components/EnumMember.d.ts.map +1 -0
  27. package/dist/src/components/EnumMember.js +67 -0
  28. package/dist/src/components/FunctionCallExpression.d.ts +19 -0
  29. package/dist/src/components/FunctionCallExpression.d.ts.map +1 -0
  30. package/dist/src/components/FunctionCallExpression.js +40 -0
  31. package/dist/src/components/FunctionDeclaration.d.ts +47 -0
  32. package/dist/src/components/FunctionDeclaration.d.ts.map +1 -0
  33. package/dist/src/components/FunctionDeclaration.js +107 -0
  34. package/dist/src/components/ImportStatement.d.ts +39 -0
  35. package/dist/src/components/ImportStatement.d.ts.map +1 -0
  36. package/dist/src/components/ImportStatement.js +104 -0
  37. package/dist/src/components/MemberExpression.d.ts +97 -0
  38. package/dist/src/components/MemberExpression.d.ts.map +1 -0
  39. package/dist/src/components/MemberExpression.js +308 -0
  40. package/dist/src/components/NoNamePolicy.d.ts +23 -0
  41. package/dist/src/components/NoNamePolicy.d.ts.map +1 -0
  42. package/dist/src/components/NoNamePolicy.js +27 -0
  43. package/dist/src/components/PyDoc.d.ts +90 -0
  44. package/dist/src/components/PyDoc.d.ts.map +1 -0
  45. package/dist/src/components/PyDoc.js +280 -0
  46. package/dist/src/components/PythonBlock.d.ts +23 -0
  47. package/dist/src/components/PythonBlock.d.ts.map +1 -0
  48. package/dist/src/components/PythonBlock.js +31 -0
  49. package/dist/src/components/Reference.d.ts +13 -0
  50. package/dist/src/components/Reference.d.ts.map +1 -0
  51. package/dist/src/components/Reference.js +18 -0
  52. package/dist/src/components/SourceFile.d.ts +46 -0
  53. package/dist/src/components/SourceFile.d.ts.map +1 -0
  54. package/dist/src/components/SourceFile.js +75 -0
  55. package/dist/src/components/StatementList.d.ts +25 -0
  56. package/dist/src/components/StatementList.d.ts.map +1 -0
  57. package/dist/src/components/StatementList.js +29 -0
  58. package/dist/src/components/VariableDeclaration.d.ts +62 -0
  59. package/dist/src/components/VariableDeclaration.d.ts.map +1 -0
  60. package/dist/src/components/VariableDeclaration.js +131 -0
  61. package/dist/src/components/index.d.ts +19 -0
  62. package/dist/src/components/index.d.ts.map +1 -0
  63. package/dist/src/components/index.js +18 -0
  64. package/dist/src/create-module.d.ts +16 -0
  65. package/dist/src/create-module.d.ts.map +1 -0
  66. package/dist/src/create-module.js +64 -0
  67. package/dist/src/index.d.ts +8 -0
  68. package/dist/src/index.d.ts.map +1 -0
  69. package/dist/src/index.js +7 -0
  70. package/dist/src/name-policy.d.ts +5 -0
  71. package/dist/src/name-policy.d.ts.map +1 -0
  72. package/dist/src/name-policy.js +47 -0
  73. package/dist/src/parameter-descriptor.d.ts +31 -0
  74. package/dist/src/parameter-descriptor.d.ts.map +1 -0
  75. package/dist/src/parameter-descriptor.js +1 -0
  76. package/dist/src/symbol-creation.d.ts +4 -0
  77. package/dist/src/symbol-creation.d.ts.map +1 -0
  78. package/dist/src/symbol-creation.js +24 -0
  79. package/dist/src/symbols/custom-output-scope.d.ts +10 -0
  80. package/dist/src/symbols/custom-output-scope.d.ts.map +1 -0
  81. package/dist/src/symbols/custom-output-scope.js +25 -0
  82. package/dist/src/symbols/index.d.ts +7 -0
  83. package/dist/src/symbols/index.d.ts.map +1 -0
  84. package/dist/src/symbols/index.js +6 -0
  85. package/dist/src/symbols/python-member-scope.d.ts +7 -0
  86. package/dist/src/symbols/python-member-scope.d.ts.map +1 -0
  87. package/dist/src/symbols/python-member-scope.js +9 -0
  88. package/dist/src/symbols/python-module-scope.d.ts +25 -0
  89. package/dist/src/symbols/python-module-scope.d.ts.map +1 -0
  90. package/dist/src/symbols/python-module-scope.js +52 -0
  91. package/dist/src/symbols/python-output-symbol.d.ts +19 -0
  92. package/dist/src/symbols/python-output-symbol.d.ts.map +1 -0
  93. package/dist/src/symbols/python-output-symbol.js +22 -0
  94. package/dist/src/symbols/reference.d.ts +4 -0
  95. package/dist/src/symbols/reference.d.ts.map +1 -0
  96. package/dist/src/symbols/reference.js +60 -0
  97. package/dist/src/symbols/scopes.d.ts +5 -0
  98. package/dist/src/symbols/scopes.d.ts.map +1 -0
  99. package/dist/src/symbols/scopes.js +4 -0
  100. package/dist/src/utils.d.ts +7 -0
  101. package/dist/src/utils.d.ts.map +1 -0
  102. package/dist/src/utils.js +12 -0
  103. package/dist/test/callsignatures.test.d.ts +2 -0
  104. package/dist/test/callsignatures.test.d.ts.map +1 -0
  105. package/dist/test/callsignatures.test.js +276 -0
  106. package/dist/test/classdeclarations.test.d.ts +2 -0
  107. package/dist/test/classdeclarations.test.d.ts.map +1 -0
  108. package/dist/test/classdeclarations.test.js +397 -0
  109. package/dist/test/classinstantiations.test.d.ts +2 -0
  110. package/dist/test/classinstantiations.test.d.ts.map +1 -0
  111. package/dist/test/classinstantiations.test.js +168 -0
  112. package/dist/test/enums.test.d.ts +2 -0
  113. package/dist/test/enums.test.d.ts.map +1 -0
  114. package/dist/test/enums.test.js +211 -0
  115. package/dist/test/externals.test.d.ts +2 -0
  116. package/dist/test/externals.test.d.ts.map +1 -0
  117. package/dist/test/externals.test.js +219 -0
  118. package/dist/test/functioncallexpressions.test.d.ts +2 -0
  119. package/dist/test/functioncallexpressions.test.d.ts.map +1 -0
  120. package/dist/test/functioncallexpressions.test.js +156 -0
  121. package/dist/test/functiondeclaration.test.d.ts +2 -0
  122. package/dist/test/functiondeclaration.test.d.ts.map +1 -0
  123. package/dist/test/functiondeclaration.test.js +363 -0
  124. package/dist/test/imports.test.d.ts +2 -0
  125. package/dist/test/imports.test.d.ts.map +1 -0
  126. package/dist/test/imports.test.js +262 -0
  127. package/dist/test/memberexpressions.test.d.ts +2 -0
  128. package/dist/test/memberexpressions.test.d.ts.map +1 -0
  129. package/dist/test/memberexpressions.test.js +879 -0
  130. package/dist/test/namepolicies.test.d.ts +2 -0
  131. package/dist/test/namepolicies.test.d.ts.map +1 -0
  132. package/dist/test/namepolicies.test.js +109 -0
  133. package/dist/test/pydocs.test.d.ts +2 -0
  134. package/dist/test/pydocs.test.d.ts.map +1 -0
  135. package/dist/test/pydocs.test.js +500 -0
  136. package/dist/test/references.test.d.ts +2 -0
  137. package/dist/test/references.test.d.ts.map +1 -0
  138. package/dist/test/references.test.js +49 -0
  139. package/dist/test/sourcefiles.test.d.ts +2 -0
  140. package/dist/test/sourcefiles.test.d.ts.map +1 -0
  141. package/dist/test/sourcefiles.test.js +198 -0
  142. package/dist/test/utils.d.ts +23 -0
  143. package/dist/test/utils.d.ts.map +1 -0
  144. package/dist/test/utils.js +88 -0
  145. package/dist/test/values.test.d.ts +2 -0
  146. package/dist/test/values.test.d.ts.map +1 -0
  147. package/dist/test/values.test.js +78 -0
  148. package/dist/test/variables.test.d.ts +2 -0
  149. package/dist/test/variables.test.d.ts.map +1 -0
  150. package/dist/test/variables.test.js +173 -0
  151. package/dist/tsconfig.tsbuildinfo +1 -0
  152. package/package.json +39 -0
  153. package/src/builtins/python.ts +20 -0
  154. package/src/components/Atom.tsx +76 -0
  155. package/src/components/CallSignature.tsx +251 -0
  156. package/src/components/ClassDeclaration.tsx +98 -0
  157. package/src/components/ClassInstantiation.tsx +54 -0
  158. package/src/components/Declaration.tsx +91 -0
  159. package/src/components/EnumDeclaration.tsx +291 -0
  160. package/src/components/EnumMember.tsx +92 -0
  161. package/src/components/FunctionCallExpression.tsx +36 -0
  162. package/src/components/FunctionDeclaration.tsx +121 -0
  163. package/src/components/ImportStatement.tsx +134 -0
  164. package/src/components/MemberExpression.tsx +456 -0
  165. package/src/components/NoNamePolicy.tsx +31 -0
  166. package/src/components/PyDoc.tsx +331 -0
  167. package/src/components/PythonBlock.tsx +26 -0
  168. package/src/components/Reference.tsx +21 -0
  169. package/src/components/SourceFile.tsx +93 -0
  170. package/src/components/StatementList.tsx +28 -0
  171. package/src/components/VariableDeclaration.tsx +180 -0
  172. package/src/components/index.ts +18 -0
  173. package/src/create-module.ts +102 -0
  174. package/src/index.ts +7 -0
  175. package/src/name-policy.ts +101 -0
  176. package/src/parameter-descriptor.ts +36 -0
  177. package/src/symbol-creation.ts +36 -0
  178. package/src/symbols/custom-output-scope.ts +35 -0
  179. package/src/symbols/index.ts +6 -0
  180. package/src/symbols/python-member-scope.ts +12 -0
  181. package/src/symbols/python-module-scope.ts +89 -0
  182. package/src/symbols/python-output-symbol.ts +36 -0
  183. package/src/symbols/reference.ts +99 -0
  184. package/src/symbols/scopes.ts +9 -0
  185. package/src/utils.ts +27 -0
  186. package/temp/api.json +7207 -0
  187. package/test/callsignatures.test.tsx +256 -0
  188. package/test/classdeclarations.test.tsx +320 -0
  189. package/test/classinstantiations.test.tsx +159 -0
  190. package/test/enums.test.tsx +203 -0
  191. package/test/externals.test.tsx +190 -0
  192. package/test/functioncallexpressions.test.tsx +145 -0
  193. package/test/functiondeclaration.test.tsx +327 -0
  194. package/test/imports.test.tsx +214 -0
  195. package/test/memberexpressions.test.tsx +725 -0
  196. package/test/namepolicies.test.tsx +109 -0
  197. package/test/pydocs.test.tsx +528 -0
  198. package/test/references.test.tsx +36 -0
  199. package/test/sourcefiles.test.tsx +131 -0
  200. package/test/utils.tsx +131 -0
  201. package/test/values.test.tsx +61 -0
  202. package/test/variables.test.tsx +153 -0
  203. package/tsconfig.json +12 -0
  204. package/tsdoc-metadata.json +11 -0
  205. package/vitest.config.ts +10 -0
  206. package/vitest.setup.ts +1 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # Changelog - @alloy-js/python
2
+
3
+ This package is currently in development. No releases yet.
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2024 Microsoft
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
3
+ "extends": "../../api-extractor.base.json"
4
+ }
@@ -0,0 +1,9 @@
1
+ import { SymbolCreator } from "@alloy-js/core";
2
+ export declare const enumModule: import("../create-module.js").ModuleRefkeys<{
3
+ readonly ".": ["auto", "Enum", "Flag", "IntEnum", "IntFlag", "StrEnum"];
4
+ }> & SymbolCreator;
5
+ export declare const requestsModule: import("../create-module.js").ModuleRefkeys<{
6
+ readonly ".": ["get", "post", "put", "delete", "patch", "head", "options"];
7
+ readonly models: ["Response", "Request"];
8
+ }> & SymbolCreator;
9
+ //# sourceMappingURL=python.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"python.d.ts","sourceRoot":"","sources":["../../../src/builtins/python.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAM/C,eAAO,MAAM,UAAU;;kBAKrB,CAAC;AAEH,eAAO,MAAM,cAAc;;;kBAMzB,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { createModule } from "../create-module.js";
2
+
3
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
4
+
5
+ export const enumModule = createModule({
6
+ name: "enum",
7
+ descriptor: {
8
+ ".": ["auto", "Enum", "Flag", "IntEnum", "IntFlag", "StrEnum"]
9
+ }
10
+ });
11
+ export const requestsModule = createModule({
12
+ name: "requests",
13
+ descriptor: {
14
+ ".": ["get", "post", "put", "delete", "patch", "head", "options"],
15
+ models: ["Response", "Request"]
16
+ }
17
+ });
@@ -0,0 +1,19 @@
1
+ export interface AtomProps {
2
+ jsValue?: unknown;
3
+ }
4
+ /**
5
+ * A component that renders a JavaScript value as a Python atom (atomic value).
6
+ * It handles various types of atomic values including numbers, booleans, strings,
7
+ * functions, arrays, and objects, converting them to Python-like syntax.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * <Atom jsValue={42} /> // renders "42"
12
+ * <Atom jsValue={true} /> // renders "True"
13
+ * <Atom jsValue="Hello" /> // renders '"Hello"'
14
+ * <Atom jsValue={[1, 2, 3]} /> // renders "[1, 2, 3]"
15
+ * <Atom jsValue={{ key: "value" }} />
16
+ * ```
17
+ */
18
+ export declare function Atom(props: AtomProps): any;
19
+ //# sourceMappingURL=Atom.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Atom.d.ts","sourceRoot":"","sources":["../../../src/components/Atom.tsx"],"names":[],"mappings":"AAEA,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,SAAS,GAAG,GAAG,CAuD1C"}
@@ -0,0 +1,82 @@
1
+ import { createComponent as _$createComponent, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { For, Indent, memo } from "@alloy-js/core";
3
+ /**
4
+ * A component that renders a JavaScript value as a Python atom (atomic value).
5
+ * It handles various types of atomic values including numbers, booleans, strings,
6
+ * functions, arrays, and objects, converting them to Python-like syntax.
7
+ *
8
+ * @example
9
+ * ```tsx
10
+ * <Atom jsValue={42} /> // renders "42"
11
+ * <Atom jsValue={true} /> // renders "True"
12
+ * <Atom jsValue="Hello" /> // renders '"Hello"'
13
+ * <Atom jsValue={[1, 2, 3]} /> // renders "[1, 2, 3]"
14
+ * <Atom jsValue={{ key: "value" }} />
15
+ * ```
16
+ */
17
+ export function Atom(props) {
18
+ return memo(() => {
19
+ const jsValue = props.jsValue;
20
+ if (typeof jsValue === "undefined") {
21
+ return "None";
22
+ } else if (typeof jsValue === "number") {
23
+ return String(jsValue);
24
+ } else if (typeof jsValue === "boolean") {
25
+ return jsValue ? "True" : "False";
26
+ } else if (typeof jsValue === "string") {
27
+ return `"${jsValue.replace(/"/g, '\\"')}"`;
28
+ } else if (typeof jsValue === "function") {
29
+ // functions are inserted as-is.
30
+ return jsValue;
31
+ } else if (typeof jsValue === "object") {
32
+ if (jsValue === null) {
33
+ return "None";
34
+ } else if (Array.isArray(jsValue)) {
35
+ // Recursively emit each element of arrays
36
+ return _$createIntrinsic("group", {
37
+ get children() {
38
+ return ["[", _$createComponent(Indent, {
39
+ softline: true,
40
+ trailingBreak: true,
41
+ get children() {
42
+ return _$createComponent(For, {
43
+ each: jsValue,
44
+ comma: true,
45
+ line: true,
46
+ children: v => _$createComponent(Atom, {
47
+ jsValue: v
48
+ })
49
+ });
50
+ }
51
+ }), "]"];
52
+ }
53
+ });
54
+ } else {
55
+ const entries = Object.entries(jsValue);
56
+ if (entries.length === 0) {
57
+ return "{}";
58
+ }
59
+ // Recursively emit each key-value pair of objects
60
+ return _$createIntrinsic("group", {
61
+ get children() {
62
+ return ["{", _$createComponent(Indent, {
63
+ softline: true,
64
+ get children() {
65
+ return _$createComponent(For, {
66
+ each: entries,
67
+ comma: true,
68
+ line: true,
69
+ children: ([k, v]) => [_$createComponent(Atom, {
70
+ jsValue: k
71
+ }), ": ", _$createComponent(Atom, {
72
+ jsValue: v
73
+ })]
74
+ });
75
+ }
76
+ }), "}"];
77
+ }
78
+ });
79
+ }
80
+ }
81
+ });
82
+ }
@@ -0,0 +1,79 @@
1
+ import { Children } from "@alloy-js/core";
2
+ import { ParameterDescriptor } from "../parameter-descriptor.js";
3
+ export interface CallSignatureParametersProps {
4
+ readonly parameters?: ParameterDescriptor[] | string[];
5
+ readonly args?: boolean;
6
+ readonly kwargs?: boolean;
7
+ readonly instanceFunction?: boolean;
8
+ readonly classFunction?: boolean;
9
+ }
10
+ /**
11
+ * A call signature parameters declaration, which can be used to define the
12
+ * parameters of a function or other callables.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * <py.CallSignatureParameters parameters={[ "a", "b" ]} />
17
+ * ```
18
+ * This will generate:
19
+ * ```python
20
+ * a, b
21
+ * ```
22
+ */
23
+ export declare function CallSignatureParameters(props: CallSignatureParametersProps): Children;
24
+ export interface CallSignatureProps {
25
+ /**
26
+ * The parameters to the call signature. Can be an array of strings for parameters
27
+ * which don't have a type or a default value. Otherwise, it's an array of
28
+ * {@link ParameterDescriptor}s.
29
+ */
30
+ parameters?: ParameterDescriptor[] | string[];
31
+ /**
32
+ * The type parameters of the call signature, e.g. for a generic function.
33
+ * This is only supported in Python 3.12+.
34
+ */
35
+ typeParameters?: string[];
36
+ /**
37
+ * Indicates if there are positional arguments (`*args`) in the function
38
+ */
39
+ args?: boolean;
40
+ /**
41
+ * Indicates if there are keyword arguments (`**kwargs`) in the function
42
+ */
43
+ kwargs?: boolean;
44
+ /**
45
+ * Indicates that this is an instance function.
46
+ */
47
+ instanceFunction?: boolean;
48
+ /**
49
+ * Indicates that this is a class function.
50
+ */
51
+ classFunction?: boolean;
52
+ /**
53
+ * The return type of the function.
54
+ */
55
+ returnType?: Children;
56
+ }
57
+ /**
58
+ * A Python call signature, e.g. the part after the `def` keyword and the name in a
59
+ * function expression.
60
+ *
61
+ * @example
62
+ * ```tsx
63
+ * <CallSignature
64
+ * parameters={[{ name: "a", type: "int" }, { name: "b", type: "str" }]}
65
+ * returnType="int"
66
+ * />
67
+ * ```
68
+ * renders to
69
+ * ```py
70
+ * (a: int, b: str) -> int
71
+ * ```
72
+ * @remarks
73
+ *
74
+ * Any parameters or type parameters declared in this signature will be placed
75
+ * in the current scope. This component does not make a scope to hold its
76
+ * parameters.
77
+ */
78
+ export declare function CallSignature(props: CallSignatureProps): Children;
79
+ //# sourceMappingURL=CallSignature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CallSignature.d.ts","sourceRoot":"","sources":["../../../src/components/CallSignature.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EAMT,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAMjE,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,UAAU,CAAC,EAAE,mBAAmB,EAAE,GAAG,MAAM,EAAE,CAAC;IACvD,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IACpC,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CAClC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,4BAA4B,YAmD1E;AA+ED,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,UAAU,CAAC,EAAE,mBAAmB,EAAE,GAAG,MAAM,EAAE,CAAC;IAE9C;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;IAE1B;;OAEG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;IAEf;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAE3B;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;OAEG;IACH,UAAU,CAAC,EAAE,QAAQ,CAAC;CACvB;AACD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,YAyBtD"}
@@ -0,0 +1,201 @@
1
+ import { createComponent as _$createComponent, memo as _$memo, createIntrinsic as _$createIntrinsic } from "@alloy-js/core/jsx-runtime";
2
+ import { computed, createSymbolSlot, For, Show, useContext } from "@alloy-js/core";
3
+ import { createPythonSymbol } from "../symbol-creation.js";
4
+ import { Atom } from "./Atom.js";
5
+ import { PythonSourceFileContext } from "./SourceFile.js";
6
+ /**
7
+ * A call signature parameters declaration, which can be used to define the
8
+ * parameters of a function or other callables.
9
+ *
10
+ * @example
11
+ * ```tsx
12
+ * <py.CallSignatureParameters parameters={[ "a", "b" ]} />
13
+ * ```
14
+ * This will generate:
15
+ * ```python
16
+ * a, b
17
+ * ```
18
+ */
19
+ export function CallSignatureParameters(props) {
20
+ // Validate that only one of instanceFunction or classFunction is true
21
+ if (props.instanceFunction && props.classFunction) {
22
+ throw new Error("Cannot be both an instance function and a class function");
23
+ }
24
+ const sfContext = useContext(PythonSourceFileContext);
25
+ const module = sfContext?.module;
26
+ const parameters = normalizeAndDeclareParameters(props.parameters ?? []);
27
+ const parameterList = computed(() => {
28
+ const params = [];
29
+
30
+ // Add self/cls parameter if instance or class function
31
+ if (props.instanceFunction) {
32
+ params.push(parameter({
33
+ symbol: createPythonSymbol("self", {
34
+ module: module
35
+ })
36
+ }));
37
+ } else if (props.classFunction) {
38
+ params.push(parameter({
39
+ symbol: createPythonSymbol("cls", {
40
+ module: module
41
+ })
42
+ }));
43
+ }
44
+
45
+ // Add regular parameters
46
+ parameters.forEach(param => {
47
+ params.push(parameter(param));
48
+ });
49
+
50
+ // Add *args if specified
51
+ if (props.args) {
52
+ params.push("*args");
53
+ }
54
+
55
+ // Add **kwargs if specified
56
+ if (props.kwargs) {
57
+ params.push("**kwargs");
58
+ }
59
+ return params;
60
+ });
61
+ return _$createComponent(For, {
62
+ each: parameterList,
63
+ comma: true,
64
+ space: true,
65
+ children: param => param
66
+ });
67
+ }
68
+ function parameter(param) {
69
+ const SymbolSlot = createSymbolSlot();
70
+ SymbolSlot.instantiateInto(param.symbol);
71
+ return _$createIntrinsic("group", {
72
+ get children() {
73
+ return [_$memo(() => param.symbol.name), _$createComponent(Show, {
74
+ get when() {
75
+ return !!param.type;
76
+ },
77
+ get children() {
78
+ return [": ", _$createComponent(SymbolSlot, {
79
+ get children() {
80
+ return param.type;
81
+ }
82
+ })];
83
+ }
84
+ }), _$createComponent(Show, {
85
+ get when() {
86
+ return !!param.optional;
87
+ },
88
+ get children() {
89
+ return [_$createComponent(Show, {
90
+ get when() {
91
+ return !param.type;
92
+ },
93
+ children: "="
94
+ }), _$createComponent(Show, {
95
+ get when() {
96
+ return !!param.type;
97
+ },
98
+ children: " = "
99
+ }), [_$memo(() => _$memo(() => !!param.default)() ? _$createComponent(Atom, {
100
+ get jsValue() {
101
+ return param.default;
102
+ }
103
+ }) : "None")]];
104
+ }
105
+ }), _$createComponent(Show, {
106
+ get when() {
107
+ return !param.optional && param.default !== undefined;
108
+ },
109
+ get children() {
110
+ return [_$createComponent(Show, {
111
+ get when() {
112
+ return !param.type;
113
+ },
114
+ children: "="
115
+ }), _$createComponent(Show, {
116
+ get when() {
117
+ return !!param.type;
118
+ },
119
+ children: " = "
120
+ }), [_$createComponent(Atom, {
121
+ get jsValue() {
122
+ return param.default;
123
+ }
124
+ })]];
125
+ }
126
+ })];
127
+ }
128
+ });
129
+ }
130
+ function normalizeAndDeclareParameters(parameters) {
131
+ const sfContext = useContext(PythonSourceFileContext);
132
+ const module = sfContext?.module;
133
+ if (parameters.length === 0) {
134
+ return [];
135
+ }
136
+ if (typeof parameters[0] === "string") {
137
+ return parameters.map(paramName => {
138
+ const symbol = createPythonSymbol(paramName, {
139
+ module: module
140
+ }, "parameter", false);
141
+ return {
142
+ refkeys: symbol.refkeys,
143
+ symbol
144
+ };
145
+ });
146
+ } else {
147
+ return parameters.map(param => {
148
+ const symbol = createPythonSymbol(param.name, {
149
+ refkeys: param.refkey,
150
+ module: module
151
+ }, "parameter", false);
152
+ return {
153
+ ...param,
154
+ symbol
155
+ };
156
+ });
157
+ }
158
+ }
159
+ /**
160
+ * A Python call signature, e.g. the part after the `def` keyword and the name in a
161
+ * function expression.
162
+ *
163
+ * @example
164
+ * ```tsx
165
+ * <CallSignature
166
+ * parameters={[{ name: "a", type: "int" }, { name: "b", type: "str" }]}
167
+ * returnType="int"
168
+ * />
169
+ * ```
170
+ * renders to
171
+ * ```py
172
+ * (a: int, b: str) -> int
173
+ * ```
174
+ * @remarks
175
+ *
176
+ * Any parameters or type parameters declared in this signature will be placed
177
+ * in the current scope. This component does not make a scope to hold its
178
+ * parameters.
179
+ */
180
+ export function CallSignature(props) {
181
+ const sParams = _$createComponent(CallSignatureParameters, {
182
+ get parameters() {
183
+ return props.parameters;
184
+ },
185
+ get args() {
186
+ return props.args;
187
+ },
188
+ get kwargs() {
189
+ return props.kwargs;
190
+ },
191
+ get instanceFunction() {
192
+ return props.instanceFunction;
193
+ },
194
+ get classFunction() {
195
+ return props.classFunction;
196
+ }
197
+ });
198
+ const typeParams = props.typeParameters ? `[${props.typeParameters.join(", ")}]` : "";
199
+ const sReturnType = props.returnType ? [" -> ", _$memo(() => props.returnType)] : undefined;
200
+ return [typeParams, "(", sParams, ")", sReturnType];
201
+ }
@@ -0,0 +1,37 @@
1
+ import { Children } from "@alloy-js/core";
2
+ import { BaseDeclarationProps } from "./Declaration.js";
3
+ export interface ClassDeclarationProps extends BaseDeclarationProps {
4
+ /**
5
+ * The base classes that this class inherits from.
6
+ */
7
+ bases?: Children[];
8
+ }
9
+ /**
10
+ * Create a Python class declaration.
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * <ClassDeclaration name="MyClass" bases={["BaseClass"]}>
15
+ * <VariableDeclaration name="a" type="int" />
16
+ * <VariableDeclaration name="b" type="str" />
17
+ * <py.FunctionDeclaration name="my_method" parameters={[{ name: "a", type: "int" }, { name: "b", type: "str" }]} returnType="int">
18
+ * return a + b
19
+ * </py.FunctionDeclaration>
20
+ * </ClassDeclaration>
21
+ * ```
22
+ * renders to
23
+ * ```py
24
+ * class MyClass(BaseClass):
25
+ * a: int = None
26
+ * b: str = None
27
+ * def my_method(self, a: int, b: str) -> int:
28
+ * return a + b
29
+ * ```
30
+ * @remarks
31
+ *
32
+ * Any child declarations (methods, fields, nested classes) will be placed
33
+ * in the class scope. This component creates a class scope to hold its
34
+ * members.
35
+ */
36
+ export declare function ClassDeclaration(props: ClassDeclarationProps): Children;
37
+ //# sourceMappingURL=ClassDeclaration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClassDeclaration.d.ts","sourceRoot":"","sources":["../../../src/components/ClassDeclaration.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EAQT,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,oBAAoB,EAGrB,MAAM,kBAAkB,CAAC;AAG1B,MAAM,WAAW,qBAAsB,SAAQ,oBAAoB;IACjE;;OAEG;IACH,KAAK,CAAC,EAAE,QAAQ,EAAE,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,YA6C5D"}
@@ -0,0 +1,83 @@
1
+ import { createComponent as _$createComponent, memo as _$memo } from "@alloy-js/core/jsx-runtime";
2
+ import { List, Name, OutputSymbolFlags, Scope, Show, childrenArray, takeSymbols } from "@alloy-js/core";
3
+ import { createPythonSymbol } from "../symbol-creation.js";
4
+ import { Declaration } from "./Declaration.js";
5
+ import { PythonBlock } from "./PythonBlock.js";
6
+ /**
7
+ * Create a Python class declaration.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * <ClassDeclaration name="MyClass" bases={["BaseClass"]}>
12
+ * <VariableDeclaration name="a" type="int" />
13
+ * <VariableDeclaration name="b" type="str" />
14
+ * <py.FunctionDeclaration name="my_method" parameters={[{ name: "a", type: "int" }, { name: "b", type: "str" }]} returnType="int">
15
+ * return a + b
16
+ * </py.FunctionDeclaration>
17
+ * </ClassDeclaration>
18
+ * ```
19
+ * renders to
20
+ * ```py
21
+ * class MyClass(BaseClass):
22
+ * a: int = None
23
+ * b: str = None
24
+ * def my_method(self, a: int, b: str) -> int:
25
+ * return a + b
26
+ * ```
27
+ * @remarks
28
+ *
29
+ * Any child declarations (methods, fields, nested classes) will be placed
30
+ * in the class scope. This component creates a class scope to hold its
31
+ * members.
32
+ */
33
+ export function ClassDeclaration(props) {
34
+ const basesPart = props.bases && ["(", _$createComponent(List, {
35
+ get children() {
36
+ return props.bases;
37
+ },
38
+ comma: true,
39
+ space: true
40
+ }), ")"];
41
+ const sym = createPythonSymbol(props.name, {
42
+ refkeys: props.refkey,
43
+ flags: (props.flags ?? OutputSymbolFlags.None) | OutputSymbolFlags.MemberContainer
44
+ }, "class", true);
45
+ takeSymbols(memberSymbol => {
46
+ // Transform emitted symbols into instance/class members
47
+ memberSymbol.flags |= OutputSymbolFlags.InstanceMember;
48
+ });
49
+
50
+ // Propagate the name after the name policy was applied
51
+ const updatedProps = {
52
+ ...props,
53
+ name: sym.name,
54
+ nameKind: "class"
55
+ };
56
+ const hasChildren = childrenArray(() => props.children).filter(c => Boolean(c)).length > 0;
57
+ return _$createComponent(Declaration, {
58
+ symbol: sym,
59
+ get children() {
60
+ return ["class ", _$createComponent(Name, {}), _$createComponent(Scope, {
61
+ get name() {
62
+ return updatedProps.name;
63
+ },
64
+ kind: "class",
65
+ get children() {
66
+ return [basesPart, _$createComponent(PythonBlock, {
67
+ opener: ":",
68
+ get children() {
69
+ return [_$createComponent(Show, {
70
+ get when() {
71
+ return Boolean(props.doc);
72
+ },
73
+ get children() {
74
+ return props.doc;
75
+ }
76
+ }), _$memo(() => hasChildren ? props.children : "pass")];
77
+ }
78
+ })];
79
+ }
80
+ })];
81
+ }
82
+ });
83
+ }
@@ -0,0 +1,24 @@
1
+ import { FunctionCallExpressionProps } from "./FunctionCallExpression.jsx";
2
+ export interface ClassInstantiationProps extends FunctionCallExpressionProps {
3
+ }
4
+ /**
5
+ * Used to create new instances of classes in Python.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * <ClassInstantiation target="MyClass" args={["arg1", "arg2"]} />
10
+ * ```
11
+ * This will generate:
12
+ * ```python
13
+ * MyClass(arg1, arg2)
14
+ * ```
15
+ * @remarks
16
+ *
17
+ * It is similar to FunctionCallExpression but specifically for class instantiation.
18
+ * Args should be a list arguments that can be either simple js values or py.Atoms, which will render as positional arguments,
19
+ * or py.VariableDeclarations, which will render as named arguments in the call statement. This component will
20
+ * not check for the correctness of the python grammar and will just work with any children you provide.
21
+ * It is up to you to ensure that the arguments you provide are valid in the context of a class instantiation.
22
+ */
23
+ export declare function ClassInstantiation(props: ClassInstantiationProps): import("@alloy-js/core").Children;
24
+ //# sourceMappingURL=ClassInstantiation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ClassInstantiation.d.ts","sourceRoot":"","sources":["../../../src/components/ClassInstantiation.tsx"],"names":[],"mappings":"AAOA,OAAO,EAEL,2BAA2B,EAC5B,MAAM,8BAA8B,CAAC;AAGtC,MAAM,WAAW,uBAAwB,SAAQ,2BAA2B;CAAG;AAE/E;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,qCAmBhE"}
@@ -0,0 +1,35 @@
1
+ import { createComponent as _$createComponent } from "@alloy-js/core/jsx-runtime";
2
+ import { emitSymbol, instantiateTakenMembersTo as instantiateTakenSymbolsTo, OutputSymbolFlags, useContext } from "@alloy-js/core";
3
+ import { createPythonSymbol } from "../symbol-creation.js";
4
+ import { FunctionCallExpression } from "./FunctionCallExpression.js";
5
+ import { PythonSourceFileContext } from "./SourceFile.js";
6
+ /**
7
+ * Used to create new instances of classes in Python.
8
+ *
9
+ * @example
10
+ * ```tsx
11
+ * <ClassInstantiation target="MyClass" args={["arg1", "arg2"]} />
12
+ * ```
13
+ * This will generate:
14
+ * ```python
15
+ * MyClass(arg1, arg2)
16
+ * ```
17
+ * @remarks
18
+ *
19
+ * It is similar to FunctionCallExpression but specifically for class instantiation.
20
+ * Args should be a list arguments that can be either simple js values or py.Atoms, which will render as positional arguments,
21
+ * or py.VariableDeclarations, which will render as named arguments in the call statement. This component will
22
+ * not check for the correctness of the python grammar and will just work with any children you provide.
23
+ * It is up to you to ensure that the arguments you provide are valid in the context of a class instantiation.
24
+ */
25
+ export function ClassInstantiation(props) {
26
+ const sfContext = useContext(PythonSourceFileContext);
27
+ const module = sfContext?.module;
28
+ const sym = createPythonSymbol("", {
29
+ flags: OutputSymbolFlags.Transient,
30
+ module: module
31
+ }, undefined, false);
32
+ instantiateTakenSymbolsTo(sym);
33
+ emitSymbol(sym);
34
+ return [_$createComponent(FunctionCallExpression, props)];
35
+ }