@alloy-js/python 0.1.0-dev.2 → 0.1.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.
@@ -0,0 +1,40 @@
1
+ import { Children, childrenArray, For, memo } from "@alloy-js/core";
2
+
3
+ export interface UnionTypeExpressionProps {
4
+ children: Children[];
5
+ optional?: boolean;
6
+ }
7
+
8
+ export function UnionTypeExpression(props: UnionTypeExpressionProps) {
9
+ const resolvedChildren = memo(() => {
10
+ const children = childrenArray(() => props.children, {
11
+ preserveFragments: true,
12
+ });
13
+
14
+ if (props.optional) {
15
+ children.push("None");
16
+ }
17
+ return children;
18
+ });
19
+
20
+ return (
21
+ <group>
22
+ <ifBreak>(</ifBreak>
23
+ <indent>
24
+ <sbr />
25
+ <For
26
+ each={resolvedChildren}
27
+ joiner={
28
+ <>
29
+ <br />|{" "}
30
+ </>
31
+ }
32
+ >
33
+ {(child) => child}
34
+ </For>
35
+ </indent>
36
+ <sbr />
37
+ <ifBreak>)</ifBreak>
38
+ </group>
39
+ );
40
+ }
@@ -15,4 +15,5 @@ export * from "./PythonBlock.js";
15
15
  export * from "./Reference.js";
16
16
  export * from "./SourceFile.js";
17
17
  export * from "./StatementList.js";
18
+ export * from "./UnionTypeExpression.js";
18
19
  export * from "./VariableDeclaration.js";
package/temp/api.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "metadata": {
3
3
  "toolPackage": "@microsoft/api-extractor",
4
- "toolVersion": "7.52.8",
4
+ "toolVersion": "7.52.9",
5
5
  "schemaVersion": 1011,
6
6
  "oldestForwardsCompatibleVersion": 1001,
7
7
  "tsdocConfig": {
@@ -4325,21 +4325,8 @@
4325
4325
  },
4326
4326
  {
4327
4327
  "kind": "Reference",
4328
- "text": "Omit",
4329
- "canonicalReference": "!Omit:type"
4330
- },
4331
- {
4332
- "kind": "Content",
4333
- "text": "<"
4334
- },
4335
- {
4336
- "kind": "Reference",
4337
- "text": "FunctionDeclarationProps",
4338
- "canonicalReference": "@alloy-js/python!FunctionDeclarationProps:interface"
4339
- },
4340
- {
4341
- "kind": "Content",
4342
- "text": ", \"name\" | \"instanceFunction\" | \"classFunction\">"
4328
+ "text": "InitFunctionDeclarationProps",
4329
+ "canonicalReference": "@alloy-js/python!InitFunctionDeclarationProps:interface"
4343
4330
  },
4344
4331
  {
4345
4332
  "kind": "Content",
@@ -4361,8 +4348,8 @@
4361
4348
  ],
4362
4349
  "fileUrlPath": "src/components/FunctionDeclaration.tsx",
4363
4350
  "returnTypeTokenRange": {
4364
- "startIndex": 6,
4365
- "endIndex": 8
4351
+ "startIndex": 3,
4352
+ "endIndex": 5
4366
4353
  },
4367
4354
  "releaseTag": "Public",
4368
4355
  "overloadIndex": 1,
@@ -4371,13 +4358,57 @@
4371
4358
  "parameterName": "props",
4372
4359
  "parameterTypeTokenRange": {
4373
4360
  "startIndex": 1,
4374
- "endIndex": 5
4361
+ "endIndex": 2
4375
4362
  },
4376
4363
  "isOptional": false
4377
4364
  }
4378
4365
  ],
4379
4366
  "name": "InitFunctionDeclaration"
4380
4367
  },
4368
+ {
4369
+ "kind": "Interface",
4370
+ "canonicalReference": "@alloy-js/python!InitFunctionDeclarationProps:interface",
4371
+ "docComment": "",
4372
+ "excerptTokens": [
4373
+ {
4374
+ "kind": "Content",
4375
+ "text": "export interface InitFunctionDeclarationProps extends "
4376
+ },
4377
+ {
4378
+ "kind": "Reference",
4379
+ "text": "Omit",
4380
+ "canonicalReference": "!Omit:type"
4381
+ },
4382
+ {
4383
+ "kind": "Content",
4384
+ "text": "<"
4385
+ },
4386
+ {
4387
+ "kind": "Reference",
4388
+ "text": "FunctionDeclarationProps",
4389
+ "canonicalReference": "@alloy-js/python!FunctionDeclarationProps:interface"
4390
+ },
4391
+ {
4392
+ "kind": "Content",
4393
+ "text": ", \"name\" | \"instanceFunction\" | \"classFunction\">"
4394
+ },
4395
+ {
4396
+ "kind": "Content",
4397
+ "text": " "
4398
+ }
4399
+ ],
4400
+ "fileUrlPath": "src/components/FunctionDeclaration.tsx",
4401
+ "releaseTag": "Public",
4402
+ "name": "InitFunctionDeclarationProps",
4403
+ "preserveMemberOrder": false,
4404
+ "members": [],
4405
+ "extendsTokenRanges": [
4406
+ {
4407
+ "startIndex": 1,
4408
+ "endIndex": 5
4409
+ }
4410
+ ]
4411
+ },
4381
4412
  {
4382
4413
  "kind": "Function",
4383
4414
  "canonicalReference": "@alloy-js/python!MemberExpression:function(1)",
@@ -5440,18 +5471,10 @@
5440
5471
  "kind": "Content",
5441
5472
  "text": "export declare function PythonBlock(props: "
5442
5473
  },
5443
- {
5444
- "kind": "Content",
5445
- "text": "{\n children: "
5446
- },
5447
5474
  {
5448
5475
  "kind": "Reference",
5449
- "text": "Children",
5450
- "canonicalReference": "@alloy-js/core!Children:type"
5451
- },
5452
- {
5453
- "kind": "Content",
5454
- "text": ";\n opener?: string;\n}"
5476
+ "text": "PythonBlockProps",
5477
+ "canonicalReference": "@alloy-js/python!PythonBlockProps:interface"
5455
5478
  },
5456
5479
  {
5457
5480
  "kind": "Content",
@@ -5469,8 +5492,8 @@
5469
5492
  ],
5470
5493
  "fileUrlPath": "src/components/PythonBlock.tsx",
5471
5494
  "returnTypeTokenRange": {
5472
- "startIndex": 5,
5473
- "endIndex": 6
5495
+ "startIndex": 3,
5496
+ "endIndex": 4
5474
5497
  },
5475
5498
  "releaseTag": "Public",
5476
5499
  "overloadIndex": 1,
@@ -5479,13 +5502,86 @@
5479
5502
  "parameterName": "props",
5480
5503
  "parameterTypeTokenRange": {
5481
5504
  "startIndex": 1,
5482
- "endIndex": 4
5505
+ "endIndex": 2
5483
5506
  },
5484
5507
  "isOptional": false
5485
5508
  }
5486
5509
  ],
5487
5510
  "name": "PythonBlock"
5488
5511
  },
5512
+ {
5513
+ "kind": "Interface",
5514
+ "canonicalReference": "@alloy-js/python!PythonBlockProps:interface",
5515
+ "docComment": "",
5516
+ "excerptTokens": [
5517
+ {
5518
+ "kind": "Content",
5519
+ "text": "export interface PythonBlockProps "
5520
+ }
5521
+ ],
5522
+ "fileUrlPath": "src/components/PythonBlock.tsx",
5523
+ "releaseTag": "Public",
5524
+ "name": "PythonBlockProps",
5525
+ "preserveMemberOrder": false,
5526
+ "members": [
5527
+ {
5528
+ "kind": "PropertySignature",
5529
+ "canonicalReference": "@alloy-js/python!PythonBlockProps#children:member",
5530
+ "docComment": "",
5531
+ "excerptTokens": [
5532
+ {
5533
+ "kind": "Content",
5534
+ "text": "children: "
5535
+ },
5536
+ {
5537
+ "kind": "Reference",
5538
+ "text": "Children",
5539
+ "canonicalReference": "@alloy-js/core!Children:type"
5540
+ },
5541
+ {
5542
+ "kind": "Content",
5543
+ "text": ";"
5544
+ }
5545
+ ],
5546
+ "isReadonly": false,
5547
+ "isOptional": false,
5548
+ "releaseTag": "Public",
5549
+ "name": "children",
5550
+ "propertyTypeTokenRange": {
5551
+ "startIndex": 1,
5552
+ "endIndex": 2
5553
+ }
5554
+ },
5555
+ {
5556
+ "kind": "PropertySignature",
5557
+ "canonicalReference": "@alloy-js/python!PythonBlockProps#opener:member",
5558
+ "docComment": "",
5559
+ "excerptTokens": [
5560
+ {
5561
+ "kind": "Content",
5562
+ "text": "opener?: "
5563
+ },
5564
+ {
5565
+ "kind": "Content",
5566
+ "text": "string"
5567
+ },
5568
+ {
5569
+ "kind": "Content",
5570
+ "text": ";"
5571
+ }
5572
+ ],
5573
+ "isReadonly": false,
5574
+ "isOptional": true,
5575
+ "releaseTag": "Public",
5576
+ "name": "opener",
5577
+ "propertyTypeTokenRange": {
5578
+ "startIndex": 1,
5579
+ "endIndex": 2
5580
+ }
5581
+ }
5582
+ ],
5583
+ "extendsTokenRanges": []
5584
+ },
5489
5585
  {
5490
5586
  "kind": "TypeAlias",
5491
5587
  "canonicalReference": "@alloy-js/python!PythonElements:type",
@@ -6885,6 +6981,130 @@
6885
6981
  ],
6886
6982
  "extendsTokenRanges": []
6887
6983
  },
6984
+ {
6985
+ "kind": "Function",
6986
+ "canonicalReference": "@alloy-js/python!UnionTypeExpression:function(1)",
6987
+ "docComment": "",
6988
+ "excerptTokens": [
6989
+ {
6990
+ "kind": "Content",
6991
+ "text": "export declare function UnionTypeExpression(props: "
6992
+ },
6993
+ {
6994
+ "kind": "Reference",
6995
+ "text": "UnionTypeExpressionProps",
6996
+ "canonicalReference": "@alloy-js/python!UnionTypeExpressionProps:interface"
6997
+ },
6998
+ {
6999
+ "kind": "Content",
7000
+ "text": "): "
7001
+ },
7002
+ {
7003
+ "kind": "Reference",
7004
+ "text": "Children",
7005
+ "canonicalReference": "@alloy-js/core!Children:type"
7006
+ },
7007
+ {
7008
+ "kind": "Content",
7009
+ "text": ";"
7010
+ }
7011
+ ],
7012
+ "fileUrlPath": "src/components/UnionTypeExpression.tsx",
7013
+ "returnTypeTokenRange": {
7014
+ "startIndex": 3,
7015
+ "endIndex": 4
7016
+ },
7017
+ "releaseTag": "Public",
7018
+ "overloadIndex": 1,
7019
+ "parameters": [
7020
+ {
7021
+ "parameterName": "props",
7022
+ "parameterTypeTokenRange": {
7023
+ "startIndex": 1,
7024
+ "endIndex": 2
7025
+ },
7026
+ "isOptional": false
7027
+ }
7028
+ ],
7029
+ "name": "UnionTypeExpression"
7030
+ },
7031
+ {
7032
+ "kind": "Interface",
7033
+ "canonicalReference": "@alloy-js/python!UnionTypeExpressionProps:interface",
7034
+ "docComment": "",
7035
+ "excerptTokens": [
7036
+ {
7037
+ "kind": "Content",
7038
+ "text": "export interface UnionTypeExpressionProps "
7039
+ }
7040
+ ],
7041
+ "fileUrlPath": "src/components/UnionTypeExpression.tsx",
7042
+ "releaseTag": "Public",
7043
+ "name": "UnionTypeExpressionProps",
7044
+ "preserveMemberOrder": false,
7045
+ "members": [
7046
+ {
7047
+ "kind": "PropertySignature",
7048
+ "canonicalReference": "@alloy-js/python!UnionTypeExpressionProps#children:member",
7049
+ "docComment": "",
7050
+ "excerptTokens": [
7051
+ {
7052
+ "kind": "Content",
7053
+ "text": "children: "
7054
+ },
7055
+ {
7056
+ "kind": "Reference",
7057
+ "text": "Children",
7058
+ "canonicalReference": "@alloy-js/core!Children:type"
7059
+ },
7060
+ {
7061
+ "kind": "Content",
7062
+ "text": "[]"
7063
+ },
7064
+ {
7065
+ "kind": "Content",
7066
+ "text": ";"
7067
+ }
7068
+ ],
7069
+ "isReadonly": false,
7070
+ "isOptional": false,
7071
+ "releaseTag": "Public",
7072
+ "name": "children",
7073
+ "propertyTypeTokenRange": {
7074
+ "startIndex": 1,
7075
+ "endIndex": 3
7076
+ }
7077
+ },
7078
+ {
7079
+ "kind": "PropertySignature",
7080
+ "canonicalReference": "@alloy-js/python!UnionTypeExpressionProps#optional:member",
7081
+ "docComment": "",
7082
+ "excerptTokens": [
7083
+ {
7084
+ "kind": "Content",
7085
+ "text": "optional?: "
7086
+ },
7087
+ {
7088
+ "kind": "Content",
7089
+ "text": "boolean"
7090
+ },
7091
+ {
7092
+ "kind": "Content",
7093
+ "text": ";"
7094
+ }
7095
+ ],
7096
+ "isReadonly": false,
7097
+ "isOptional": true,
7098
+ "releaseTag": "Public",
7099
+ "name": "optional",
7100
+ "propertyTypeTokenRange": {
7101
+ "startIndex": 1,
7102
+ "endIndex": 2
7103
+ }
7104
+ }
7105
+ ],
7106
+ "extendsTokenRanges": []
7107
+ },
6888
7108
  {
6889
7109
  "kind": "Function",
6890
7110
  "canonicalReference": "@alloy-js/python!usePythonNamePolicy:function(1)",
@@ -0,0 +1,77 @@
1
+ import { code } from "@alloy-js/core";
2
+ import { d } from "@alloy-js/core/testing";
3
+ import { describe, expect, it } from "vitest";
4
+ import * as py from "../src/index.js";
5
+ import { toSourceText } from "./utils.jsx";
6
+
7
+ describe("UnionTypeExpression", () => {
8
+ it("renders a Python union expression - 1 item", () => {
9
+ const elements = [code`int`];
10
+ expect(
11
+ toSourceText([
12
+ <py.UnionTypeExpression>{elements}</py.UnionTypeExpression>,
13
+ ]),
14
+ ).toRenderTo("int");
15
+ });
16
+ it("renders a Python union expression - 2 items", () => {
17
+ const elements = [code`int`, code`str`];
18
+ expect(
19
+ toSourceText([
20
+ <py.UnionTypeExpression>{elements}</py.UnionTypeExpression>,
21
+ ]),
22
+ ).toRenderTo("int | str");
23
+ });
24
+ it("renders a Python union expression - N items", () => {
25
+ const elements = [
26
+ code`int`,
27
+ code`str`,
28
+ code`float`,
29
+ code`bool`,
30
+ code`list`,
31
+ code`dict`,
32
+ code`set`,
33
+ code`tuple`,
34
+ code`frozenset`,
35
+ code`bytes`,
36
+ code`bytearray`,
37
+ code`memoryview`,
38
+ code`complex`,
39
+ ];
40
+ expect(
41
+ toSourceText([
42
+ <py.UnionTypeExpression>{elements}</py.UnionTypeExpression>,
43
+ ]),
44
+ ).toRenderTo(d`
45
+ (
46
+ int
47
+ | str
48
+ | float
49
+ | bool
50
+ | list
51
+ | dict
52
+ | set
53
+ | tuple
54
+ | frozenset
55
+ | bytes
56
+ | bytearray
57
+ | memoryview
58
+ | complex
59
+ )`);
60
+ });
61
+ it("renders a Python union expression - 2 items", () => {
62
+ const elements = [code`int`, code`str`];
63
+ expect(
64
+ toSourceText([
65
+ <py.UnionTypeExpression>{elements}</py.UnionTypeExpression>,
66
+ ]),
67
+ ).toRenderTo("int | str");
68
+ });
69
+ it("renders a Python union expression - 2 items with optional", () => {
70
+ const elements = [code`int`, code`str`];
71
+ expect(
72
+ toSourceText([
73
+ <py.UnionTypeExpression optional>{elements}</py.UnionTypeExpression>,
74
+ ]),
75
+ ).toRenderTo("int | str | None");
76
+ });
77
+ });
@@ -1,4 +1,4 @@
1
- import { refkey } from "@alloy-js/core";
1
+ import { code, refkey } from "@alloy-js/core";
2
2
  import { d } from "@alloy-js/core/testing";
3
3
  import { describe, expect, it } from "vitest";
4
4
  import * as py from "../src/index.js";
@@ -95,6 +95,34 @@ describe("Python Variable", () => {
95
95
  expect(res).toBe(`12`);
96
96
  });
97
97
 
98
+ it("declares a python variable with an optional type", () => {
99
+ const elements = [code`int`];
100
+ const typing = (
101
+ <py.UnionTypeExpression optional>{elements}</py.UnionTypeExpression>
102
+ );
103
+ const res = toSourceText([
104
+ <py.StatementList>
105
+ <py.VariableDeclaration name="my_var" type={typing} />
106
+ </py.StatementList>,
107
+ ]);
108
+ expect(res).toBe(d`
109
+ my_var: int | None = None`);
110
+ });
111
+
112
+ it("declares a python variable with an optional type omitting none", () => {
113
+ const elements = [code`int`];
114
+ const typing = (
115
+ <py.UnionTypeExpression optional>{elements}</py.UnionTypeExpression>
116
+ );
117
+ const res = toSourceText([
118
+ <py.StatementList>
119
+ <py.VariableDeclaration name="my_var" type={typing} omitNone />
120
+ </py.StatementList>,
121
+ ]);
122
+ expect(res).toBe(d`
123
+ my_var: int | None`);
124
+ });
125
+
98
126
  it("declares a python variable with a class type", () => {
99
127
  const res = toSourceText([
100
128
  <py.StatementList>
@@ -5,7 +5,7 @@
5
5
  "toolPackages": [
6
6
  {
7
7
  "packageName": "@microsoft/api-extractor",
8
- "packageVersion": "7.52.8"
8
+ "packageVersion": "7.52.9"
9
9
  }
10
10
  ]
11
11
  }