@alloy-js/python 0.2.0-dev.2 → 0.2.0-dev.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/builtins/python.d.ts +3 -0
- package/dist/src/builtins/python.d.ts.map +1 -1
- package/dist/src/builtins/python.js +6 -0
- package/dist/src/builtins/python.js.map +1 -1
- package/dist/src/components/CallSignature.d.ts +4 -15
- package/dist/src/components/CallSignature.d.ts.map +1 -1
- package/dist/src/components/CallSignature.js +13 -67
- package/dist/src/components/CallSignature.js.map +1 -1
- package/dist/src/components/ClassMethodDeclaration.d.ts +22 -0
- package/dist/src/components/ClassMethodDeclaration.d.ts.map +1 -0
- package/dist/src/components/ClassMethodDeclaration.js +32 -0
- package/dist/src/components/ClassMethodDeclaration.js.map +1 -0
- package/dist/src/components/ConstructorDeclaration.d.ts +21 -0
- package/dist/src/components/ConstructorDeclaration.d.ts.map +1 -0
- package/dist/src/components/ConstructorDeclaration.js +35 -0
- package/dist/src/components/ConstructorDeclaration.js.map +1 -0
- package/dist/src/components/DunderMethodDeclaration.d.ts +21 -0
- package/dist/src/components/DunderMethodDeclaration.d.ts.map +1 -0
- package/dist/src/components/DunderMethodDeclaration.js +29 -0
- package/dist/src/components/DunderMethodDeclaration.js.map +1 -0
- package/dist/src/components/EnumDeclaration.d.ts +11 -32
- package/dist/src/components/EnumDeclaration.d.ts.map +1 -1
- package/dist/src/components/EnumDeclaration.js +10 -48
- package/dist/src/components/EnumDeclaration.js.map +1 -1
- package/dist/src/components/EnumMember.js +3 -3
- package/dist/src/components/EnumMember.js.map +1 -1
- package/dist/src/components/FunctionBase.d.ts +48 -0
- package/dist/src/components/FunctionBase.d.ts.map +1 -0
- package/dist/src/components/FunctionBase.js +91 -0
- package/dist/src/components/FunctionBase.js.map +1 -0
- package/dist/src/components/FunctionDeclaration.d.ts +11 -31
- package/dist/src/components/FunctionDeclaration.d.ts.map +1 -1
- package/dist/src/components/FunctionDeclaration.js +22 -79
- package/dist/src/components/FunctionDeclaration.js.map +1 -1
- package/dist/src/components/MemberExpression.d.ts.map +1 -1
- package/dist/src/components/MemberExpression.js +12 -13
- package/dist/src/components/MemberExpression.js.map +1 -1
- package/dist/src/components/MethodBase.d.ts +29 -0
- package/dist/src/components/MethodBase.d.ts.map +1 -0
- package/dist/src/components/MethodBase.js +32 -0
- package/dist/src/components/MethodBase.js.map +1 -0
- package/dist/src/components/MethodDeclaration.d.ts +22 -0
- package/dist/src/components/MethodDeclaration.d.ts.map +1 -0
- package/dist/src/components/MethodDeclaration.js +34 -0
- package/dist/src/components/MethodDeclaration.js.map +1 -0
- package/dist/src/components/PropertyDeclaration.d.ts +71 -0
- package/dist/src/components/PropertyDeclaration.d.ts.map +1 -0
- package/dist/src/components/PropertyDeclaration.js +227 -0
- package/dist/src/components/PropertyDeclaration.js.map +1 -0
- package/dist/src/components/PyDoc.d.ts +107 -42
- package/dist/src/components/PyDoc.d.ts.map +1 -1
- package/dist/src/components/PyDoc.js +845 -181
- package/dist/src/components/PyDoc.js.map +1 -1
- package/dist/src/components/SourceFile.d.ts +24 -0
- package/dist/src/components/SourceFile.d.ts.map +1 -1
- package/dist/src/components/SourceFile.js +28 -1
- package/dist/src/components/SourceFile.js.map +1 -1
- package/dist/src/components/StaticMethodDeclaration.d.ts +22 -0
- package/dist/src/components/StaticMethodDeclaration.d.ts.map +1 -0
- package/dist/src/components/StaticMethodDeclaration.js +32 -0
- package/dist/src/components/StaticMethodDeclaration.js.map +1 -0
- package/dist/src/components/TypeArguments.d.ts +9 -0
- package/dist/src/components/TypeArguments.d.ts.map +1 -0
- package/dist/src/components/TypeArguments.js +18 -0
- package/dist/src/components/TypeArguments.js.map +1 -0
- package/dist/src/components/TypeReference.d.ts +14 -0
- package/dist/src/components/TypeReference.d.ts.map +1 -0
- package/dist/src/components/TypeReference.js +29 -0
- package/dist/src/components/TypeReference.js.map +1 -0
- package/dist/src/components/UnionTypeExpression.d.ts +1 -2
- package/dist/src/components/UnionTypeExpression.d.ts.map +1 -1
- package/dist/src/components/UnionTypeExpression.js +3 -11
- package/dist/src/components/UnionTypeExpression.js.map +1 -1
- package/dist/src/components/VariableDeclaration.d.ts.map +1 -1
- package/dist/src/components/VariableDeclaration.js +3 -3
- package/dist/src/components/VariableDeclaration.js.map +1 -1
- package/dist/src/components/index.d.ts +10 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/components/index.js +9 -0
- package/dist/src/components/index.js.map +1 -1
- package/dist/src/parameter-descriptor.d.ts +1 -4
- package/dist/src/parameter-descriptor.d.ts.map +1 -1
- package/dist/src/parameter-descriptor.js +7 -1
- package/dist/src/parameter-descriptor.js.map +1 -1
- package/dist/src/symbol-creation.d.ts +4 -0
- package/dist/src/symbol-creation.d.ts.map +1 -1
- package/dist/src/symbol-creation.js +12 -0
- package/dist/src/symbol-creation.js.map +1 -1
- package/dist/src/symbols/factories.d.ts +15 -0
- package/dist/src/symbols/factories.d.ts.map +1 -0
- package/dist/src/symbols/factories.js +28 -0
- package/dist/src/symbols/factories.js.map +1 -0
- package/dist/src/symbols/index.d.ts +1 -0
- package/dist/src/symbols/index.d.ts.map +1 -1
- package/dist/src/symbols/index.js +1 -0
- package/dist/src/symbols/index.js.map +1 -1
- package/dist/src/symbols/reference.d.ts +1 -1
- package/dist/src/symbols/reference.d.ts.map +1 -1
- package/dist/src/symbols/reference.js +1 -1
- package/dist/src/symbols/reference.js.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +1 -1
- package/dist/src/utils.js.map +1 -1
- package/dist/test/callsignatures.test.js +42 -64
- package/dist/test/callsignatures.test.js.map +1 -1
- package/dist/test/class-method-declaration.test.d.ts +2 -0
- package/dist/test/class-method-declaration.test.d.ts.map +1 -0
- package/dist/test/class-method-declaration.test.js +61 -0
- package/dist/test/class-method-declaration.test.js.map +1 -0
- package/dist/test/classdeclarations.test.js +6 -8
- package/dist/test/classdeclarations.test.js.map +1 -1
- package/dist/test/constructordeclaration.test.d.ts +2 -0
- package/dist/test/constructordeclaration.test.d.ts.map +1 -0
- package/dist/test/constructordeclaration.test.js +58 -0
- package/dist/test/constructordeclaration.test.js.map +1 -0
- package/dist/test/dundermethoddeclaration.test.d.ts +2 -0
- package/dist/test/dundermethoddeclaration.test.d.ts.map +1 -0
- package/dist/test/dundermethoddeclaration.test.js +65 -0
- package/dist/test/dundermethoddeclaration.test.js.map +1 -0
- package/dist/test/enums.test.js +14 -16
- package/dist/test/enums.test.js.map +1 -1
- package/dist/test/externals.test.js +2 -4
- package/dist/test/externals.test.js.map +1 -1
- package/dist/test/factories.test.d.ts +2 -0
- package/dist/test/factories.test.d.ts.map +1 -0
- package/dist/test/factories.test.js +78 -0
- package/dist/test/factories.test.js.map +1 -0
- package/dist/test/functiondeclaration.test.js +213 -59
- package/dist/test/functiondeclaration.test.js.map +1 -1
- package/dist/test/memberexpressions.test.js +1 -1
- package/dist/test/memberexpressions.test.js.map +1 -1
- package/dist/test/methoddeclaration.test.d.ts +2 -0
- package/dist/test/methoddeclaration.test.d.ts.map +1 -0
- package/dist/test/methoddeclaration.test.js +239 -0
- package/dist/test/methoddeclaration.test.js.map +1 -0
- package/dist/test/namepolicies.test.js +1 -2
- package/dist/test/namepolicies.test.js.map +1 -1
- package/dist/test/propertydeclaration.test.d.ts +2 -0
- package/dist/test/propertydeclaration.test.d.ts.map +1 -0
- package/dist/test/propertydeclaration.test.js +229 -0
- package/dist/test/propertydeclaration.test.js.map +1 -0
- package/dist/test/pydocs.test.js +926 -126
- package/dist/test/pydocs.test.js.map +1 -1
- package/dist/test/references.test.js +1 -5
- package/dist/test/references.test.js.map +1 -1
- package/dist/test/sourcefiles.test.js +90 -1
- package/dist/test/sourcefiles.test.js.map +1 -1
- package/dist/test/staticmethoddeclaration.test.d.ts +2 -0
- package/dist/test/staticmethoddeclaration.test.d.ts.map +1 -0
- package/dist/test/staticmethoddeclaration.test.js +61 -0
- package/dist/test/staticmethoddeclaration.test.js.map +1 -0
- package/dist/test/typereference.test.d.ts +2 -0
- package/dist/test/typereference.test.d.ts.map +1 -0
- package/dist/test/typereference.test.js +51 -0
- package/dist/test/typereference.test.js.map +1 -0
- package/dist/test/uniontypeexpression.test.js +152 -15
- package/dist/test/uniontypeexpression.test.js.map +1 -1
- package/dist/test/variables.test.js +28 -19
- package/dist/test/variables.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/src/builtins/python.ts +7 -0
- package/src/components/CallSignature.tsx +17 -69
- package/src/components/ClassMethodDeclaration.tsx +34 -0
- package/src/components/ConstructorDeclaration.tsx +37 -0
- package/src/components/DunderMethodDeclaration.tsx +30 -0
- package/src/components/EnumDeclaration.tsx +16 -44
- package/src/components/EnumMember.tsx +3 -3
- package/src/components/FunctionBase.tsx +88 -0
- package/src/components/FunctionDeclaration.tsx +18 -82
- package/src/components/MemberExpression.tsx +6 -19
- package/src/components/MethodBase.tsx +53 -0
- package/src/components/MethodDeclaration.tsx +27 -0
- package/src/components/PropertyDeclaration.tsx +264 -0
- package/src/components/PyDoc.tsx +795 -195
- package/src/components/SourceFile.tsx +29 -0
- package/src/components/StaticMethodDeclaration.tsx +34 -0
- package/src/components/TypeArguments.tsx +24 -0
- package/src/components/TypeReference.tsx +33 -0
- package/src/components/UnionTypeExpression.tsx +4 -15
- package/src/components/VariableDeclaration.tsx +1 -3
- package/src/components/index.ts +10 -0
- package/src/parameter-descriptor.ts +6 -5
- package/src/symbol-creation.ts +17 -0
- package/src/symbols/factories.ts +39 -0
- package/src/symbols/index.ts +1 -0
- package/src/symbols/reference.tsx +3 -5
- package/src/utils.ts +0 -2
- package/temp/api.json +5281 -2273
- package/test/callsignatures.test.tsx +102 -74
- package/test/class-method-declaration.test.tsx +53 -0
- package/test/classdeclarations.test.tsx +7 -9
- package/test/constructordeclaration.test.tsx +48 -0
- package/test/dundermethoddeclaration.test.tsx +53 -0
- package/test/enums.test.tsx +14 -16
- package/test/externals.test.tsx +5 -7
- package/test/factories.test.tsx +72 -0
- package/test/functiondeclaration.test.tsx +196 -44
- package/test/memberexpressions.test.tsx +7 -2
- package/test/methoddeclaration.test.tsx +202 -0
- package/test/namepolicies.test.tsx +1 -2
- package/test/propertydeclaration.test.tsx +192 -0
- package/test/pydocs.test.tsx +1093 -129
- package/test/references.test.tsx +1 -1
- package/test/sourcefiles.test.tsx +100 -1
- package/test/staticmethoddeclaration.test.tsx +49 -0
- package/test/typereference.test.tsx +52 -0
- package/test/uniontypeexpression.test.tsx +169 -34
- 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 {
|
|
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
|
|
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={
|
|
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
|
|
92
|
+
parameters: (ParameterDescriptor | string)[],
|
|
126
93
|
): DeclaredParameterDescriptor[] {
|
|
127
|
-
|
|
128
|
-
|
|
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
|
|
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
|
|
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
|
|
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:
|
|
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:
|
|
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.
|
|
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 {
|
|
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
|
-
<
|
|
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
|
-
<
|
|
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 {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
89
|
-
*
|
|
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
|
|
93
|
-
|
|
94
|
-
|
|
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
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Children,
|
|
3
3
|
childrenArray,
|
|
4
|
-
code,
|
|
5
4
|
computed,
|
|
6
5
|
For,
|
|
7
6
|
isComponentCreator,
|
|
7
|
+
List,
|
|
8
8
|
OutputSymbol,
|
|
9
9
|
reactive,
|
|
10
10
|
ref,
|
|
@@ -397,11 +397,6 @@ function isSubscriptionDescriptor(
|
|
|
397
397
|
return "type" in part && part.type === MEMBER_ACCESS_TYPES.SUBSCRIPTION;
|
|
398
398
|
}
|
|
399
399
|
|
|
400
|
-
function getNameForRefkey(refkey: Children): Children {
|
|
401
|
-
const parsedValue = getSymbolForRefkey(refkey as Refkey).value;
|
|
402
|
-
return parsedValue !== undefined ? parsedValue.name : refkey;
|
|
403
|
-
}
|
|
404
|
-
|
|
405
400
|
export interface SubscriptionProps {
|
|
406
401
|
/**
|
|
407
402
|
* Single key for subscription access (obj[key] or obj[0]).
|
|
@@ -429,28 +424,20 @@ export interface SubscriptionProps {
|
|
|
429
424
|
function getSubscriptionValue(partProps: SubscriptionProps): Children {
|
|
430
425
|
// Handle tuple keys: obj[a, b] → (a, b)
|
|
431
426
|
if (partProps.keys?.length) {
|
|
432
|
-
|
|
433
|
-
getNameForRefkey(key as Refkey),
|
|
434
|
-
);
|
|
435
|
-
return code`${parsedKeys.join(", ")}`;
|
|
427
|
+
return <List comma line children={partProps.keys} />;
|
|
436
428
|
}
|
|
437
429
|
|
|
438
430
|
// Handle slice: obj[start:stop:step]
|
|
439
431
|
if (partProps.slice && Object.keys(partProps.slice).length > 0) {
|
|
440
432
|
const { start, stop, step } = partProps.slice;
|
|
441
|
-
const parts = [
|
|
442
|
-
start ? getNameForRefkey(start as Refkey) : "",
|
|
443
|
-
":",
|
|
444
|
-
stop ? getNameForRefkey(stop as Refkey) : "",
|
|
445
|
-
];
|
|
433
|
+
const parts = [start ? start : "", ":", stop ? stop : ""];
|
|
446
434
|
|
|
447
435
|
if (step) {
|
|
448
|
-
parts.push(":",
|
|
436
|
+
parts.push(":", step);
|
|
449
437
|
}
|
|
450
438
|
|
|
451
|
-
return
|
|
439
|
+
return parts;
|
|
452
440
|
}
|
|
453
441
|
|
|
454
|
-
|
|
455
|
-
return getNameForRefkey(partProps.key as Refkey);
|
|
442
|
+
return partProps.key;
|
|
456
443
|
}
|