Skip to content

Commit 4c63315

Browse files
authored
Merge pull request #1718 from polywrap/nk/py-binding
fix: enum in python plugin bindings
2 parents 635f6b7 + ff30543 commit 4c63315

9 files changed

Lines changed: 130 additions & 47 deletions

File tree

packages/schema/bind/src/__tests__/test-cases.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
import fs from "fs";
1616
import path from "path";
1717

18+
import { deepCopy } from "./utils";
19+
1820
describe("Polywrap Binding Test Suite", () => {
1921
const cases = fetchTestCases();
2022

@@ -38,7 +40,7 @@ describe("Polywrap Binding Test Suite", () => {
3840
};
3941

4042
const output = bindSchema({
41-
...testCase.input,
43+
...deepCopy(testCase.input),
4244
bindLanguage: language as BindLanguage,
4345
});
4446

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export function deepCopy(obj: any): any {
2+
var copy;
3+
4+
// Handle the 3 simple types, and null or undefined
5+
if (null == obj || "object" != typeof obj) return obj;
6+
7+
// Handle Date
8+
if (obj instanceof Date) {
9+
copy = new Date();
10+
copy.setTime(obj.getTime());
11+
return copy;
12+
}
13+
14+
// Handle Array
15+
if (obj instanceof Array) {
16+
copy = [];
17+
for (var i = 0, len = obj.length; i < len; i++) {
18+
copy[i] = deepCopy(obj[i]);
19+
}
20+
return copy;
21+
}
22+
23+
// Handle Object
24+
if (obj instanceof Object) {
25+
copy = {};
26+
for (var attr in obj) {
27+
//@ts-ignore
28+
if (obj.hasOwnProperty(attr)) copy[attr] = deepCopy(obj[attr]);
29+
}
30+
return copy;
31+
}
32+
33+
throw new Error("Unable to copy obj! Its type isn't supported.");
34+
}

packages/schema/bind/src/bindings/python/plugin/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import * as Functions from "../functions";
33
import { GenerateBindingFn, renderTemplates } from "../..";
44
import { BindOptions, BindOutput } from "../../..";
5+
import { addEnumMembers } from "../transformers";
56

67
import {
78
transformAbi,
@@ -21,7 +22,7 @@ const templatePath = (subpath: string) =>
2122
path.join(__dirname, "./templates", subpath);
2223

2324
const sort = (obj: Record<string, unknown>) =>
24-
Object.keys(obj)
25+
Object.keys(obj || {})
2526
.sort()
2627
.reduce((map: Record<string, unknown>, key: string) => {
2728
if (typeof obj[key] === "object") {
@@ -71,6 +72,7 @@ function applyTransforms(abi: WrapAbi): WrapAbi {
7172
toPrefixedGraphQLType,
7273
methodParentPointers(),
7374
interfaceUris(),
75+
addEnumMembers,
7476
];
7577

7678
for (const transform of transforms) {

packages/schema/bind/src/bindings/python/plugin/templates/types-py.mustache

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from typing import TypedDict, Optional
6-
from enum import IntEnum, auto
6+
from enum import IntEnum
77

88
from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
99
from polywrap_msgpack import GenericMap
@@ -36,13 +36,13 @@ from polywrap_msgpack import GenericMap
3636
### Enums START ###
3737
{{#enumTypes}}
3838
class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEnum):
39-
{{#constants}}
40-
{{#detectKeyword}}{{.}}{{/detectKeyword}} = auto(), "{{.}}"
41-
{{/constants}}
39+
{{#members}}
40+
{{#detectKeyword}}{{name}}{{/detectKeyword}} = {{value}}, "{{value}}", "{{name}}"
41+
{{/members}}
4242

4343
def __new__(cls, value: int, *aliases: str):
4444
obj = int.__new__(cls)
45-
obj._value_ = value - 1
45+
obj._value_ = value
4646
for alias in aliases:
4747
cls._value2member_map_[alias] = obj
4848
return obj
@@ -68,13 +68,13 @@ class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEn
6868
{{#importedEnumTypes}}
6969
# URI: "{{uri}}" #
7070
class {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}(IntEnum):
71-
{{#constants}}
72-
{{#detectKeyword}}{{.}}{{/detectKeyword}} = auto()
73-
{{/constants}}
71+
{{#members}}
72+
{{#detectKeyword}}{{name}}{{/detectKeyword}} = {{value}}, "{{value}}", "{{name}}"
73+
{{/members}}
7474

7575
def __new__(cls, value: int, *aliases: str):
7676
obj = int.__new__(cls)
77-
obj._value_ = value - 1
77+
obj._value_ = value
7878
for alias in aliases:
7979
cls._value2member_map_[alias] = obj
8080
return obj
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { AbiTransforms } from "@polywrap/schema-parse";
2+
import { EnumDefinition } from "@polywrap/wrap-manifest-types-js";
3+
4+
export const addEnumMembers: AbiTransforms = {
5+
enter: {
6+
// eslint-disable-next-line @typescript-eslint/naming-convention
7+
EnumDefinition: (def: EnumDefinition): EnumDefinition => {
8+
if (!def.constants) {
9+
return { ...def };
10+
}
11+
12+
const members: Array<Record<string, unknown>> = [];
13+
let value = 0;
14+
15+
for (const constant of def.constants) {
16+
members.push({
17+
name: constant,
18+
value: value,
19+
});
20+
value += 1;
21+
}
22+
23+
return {
24+
...def,
25+
members,
26+
} as EnumDefinition;
27+
},
28+
},
29+
};

packages/test-cases/cases/bind/sanity/output/plugin-py/types.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from typing import TypedDict, Optional
6-
from enum import IntEnum, auto
6+
from enum import IntEnum
77

88
from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
99
from polywrap_msgpack import GenericMap
@@ -84,23 +84,23 @@
8484

8585
### Enums START ###
8686
class CustomEnum(IntEnum):
87-
STRING = auto(), "STRING"
88-
BYTES = auto(), "BYTES"
87+
STRING = 0, "0", "STRING"
88+
BYTES = 1, "1", "BYTES"
8989

9090
def __new__(cls, value: int, *aliases: str):
9191
obj = int.__new__(cls)
92-
obj._value_ = value - 1
92+
obj._value_ = value
9393
for alias in aliases:
9494
cls._value2member_map_[alias] = obj
9595
return obj
9696

9797
class While(IntEnum):
98-
r_for = auto(), "for"
99-
r_in = auto(), "in"
98+
r_for = 0, "0", "for"
99+
r_in = 1, "1", "in"
100100

101101
def __new__(cls, value: int, *aliases: str):
102102
obj = int.__new__(cls)
103-
obj._value_ = value - 1
103+
obj._value_ = value
104104
for alias in aliases:
105105
cls._value2member_map_[alias] = obj
106106
return obj
@@ -132,24 +132,24 @@ def __new__(cls, value: int, *aliases: str):
132132

133133
# URI: "testimport.uri.eth" #
134134
class TestImportEnum(IntEnum):
135-
STRING = auto()
136-
BYTES = auto()
135+
STRING = 0, "0", "STRING"
136+
BYTES = 1, "1", "BYTES"
137137

138138
def __new__(cls, value: int, *aliases: str):
139139
obj = int.__new__(cls)
140-
obj._value_ = value - 1
140+
obj._value_ = value
141141
for alias in aliases:
142142
cls._value2member_map_[alias] = obj
143143
return obj
144144

145145
# URI: "testimport.uri.eth" #
146146
class TestImportEnumReturn(IntEnum):
147-
STRING = auto()
148-
BYTES = auto()
147+
STRING = 0, "0", "STRING"
148+
BYTES = 1, "1", "BYTES"
149149

150150
def __new__(cls, value: int, *aliases: str):
151151
obj = int.__new__(cls)
152-
obj._value_ = value - 1
152+
obj._value_ = value
153153
for alias in aliases:
154154
cls._value2member_map_[alias] = obj
155155
return obj

packages/test-cases/cases/bind/sanity/output/plugin-py/wrap_info.py

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/test-cases/cases/cli/codegen/plugin/008-python/expected/wrap/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from typing import TypedDict, Optional
6-
from enum import IntEnum, auto
6+
from enum import IntEnum
77

88
from polywrap_core import InvokerClient, Uri, UriPackageOrWrapper
99
from polywrap_msgpack import GenericMap

yarn.lock

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2670,9 +2670,9 @@
26702670
integrity sha512-wH6Tu9mbiOt0n5EvdoWy0VGQaJMHfLIxY/6wS0xLC7CV1taM6gESEzcYy0ZlWvxxiiljYvfDIvz4hHbUUDRlhw==
26712671

26722672
"@types/node@*", "@types/node@^18.14.6":
2673-
version "18.15.11"
2674-
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.15.11.tgz#b3b790f09cb1696cffcec605de025b088fa4225f"
2675-
integrity sha512-E5Kwq2n4SbMzQOn6wnmBjuK9ouqlURrcZDVfbo9ftDDTFt3nk7ZKK4GMOzoYgnpQJKcxwQw+lGaBvvlMo0qN/Q==
2673+
version "18.16.0"
2674+
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.16.0.tgz#4668bc392bb6938637b47e98b1f2ed5426f33316"
2675+
integrity sha512-BsAaKhB+7X+H4GnSjGhJG9Qi8Tw+inU9nJDwmD5CgOmBLEI6ArdhikpLX7DjbjDRDTbqZzU2LSQNZg8WGPiSZQ==
26762676

26772677
"@types/normalize-package-data@^2.4.0":
26782678
version "2.4.1"
@@ -3650,9 +3650,9 @@ camelcase@^6.0.0:
36503650
integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==
36513651

36523652
caniuse-lite@^1.0.30001449:
3653-
version "1.0.30001480"
3654-
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001480.tgz#9bbd35ee44c2480a1e3a3b9f4496f5066817164a"
3655-
integrity sha512-q7cpoPPvZYgtyC4VaBSN0Bt+PJ4c4EYRf0DrduInOz2SkFpHD5p3LnvEpqBp7UnJn+8x1Ogl1s38saUxe+ihQQ==
3653+
version "1.0.30001481"
3654+
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001481.tgz#f58a717afe92f9e69d0e35ff64df596bfad93912"
3655+
integrity sha512-KCqHwRnaa1InZBtqXzP98LPg0ajCVujMKjqKDhZEthIpAsJl/YEIa3YvXjGXPVqzZVguccuu7ga9KOE1J9rKPQ==
36563656

36573657
capture-exit@^2.0.0:
36583658
version "2.0.0"
@@ -4458,9 +4458,9 @@ electron-fetch@^1.7.2:
44584458
encoding "^0.1.13"
44594459

44604460
electron-to-chromium@^1.4.284:
4461-
version "1.4.368"
4462-
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.368.tgz#75901f97d3e23da2e66feb1e61fbb8e70ac96430"
4463-
integrity sha512-e2aeCAixCj9M7nJxdB/wDjO6mbYX+lJJxSJCXDzlr5YPGYVofuJwGN9nKg2o6wWInjX6XmxRinn3AeJMK81ltw==
4461+
version "1.4.369"
4462+
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.369.tgz#a98d838cdd79be4471cd04e9b4dffe891d037874"
4463+
integrity sha512-LfxbHXdA/S+qyoTEA4EbhxGjrxx7WK2h6yb5K2v0UCOufUKX+VZaHbl3svlzZfv9sGseym/g3Ne4DpsgRULmqg==
44644464

44654465
elliptic@6.5.4:
44664466
version "6.5.4"
@@ -4538,7 +4538,7 @@ error-ex@^1.2.0, error-ex@^1.3.1:
45384538
dependencies:
45394539
is-arrayish "^0.2.1"
45404540

4541-
es-abstract@^1.19.0, es-abstract@^1.20.4:
4541+
es-abstract@^1.19.0, es-abstract@^1.20.4, es-abstract@^1.21.2:
45424542
version "1.21.2"
45434543
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.21.2.tgz#a56b9695322c8a185dc25975aa3b8ec31d0e7eff"
45444544
integrity sha512-y/B5POM2iBnIxCiernH1G7rC9qQoM77lLIMQLuob0zhp8C56Po81+2Nj0WFKnd0pNReDTnkYryc+zhOzpEIROg==
@@ -6401,6 +6401,11 @@ isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
64016401
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
64026402
integrity sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==
64036403

6404+
isarray@^2.0.5:
6405+
version "2.0.5"
6406+
resolved "https://registry.yarnpkg.com/isarray/-/isarray-2.0.5.tgz#8af1e4c1221244cc62459faf38940d4e644a5723"
6407+
integrity sha512-xHjhDr3cNBK0BzdUJSPXZntQUx/mwMS5Rw4A7lPJ90XGAO6ISP/ePDNuo0vhqOZU+UD5JoodwCAAoZQd3FeAKw==
6408+
64046409
isexe@^2.0.0:
64056410
version "2.0.0"
64066411
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
@@ -8284,14 +8289,15 @@ object.assign@^4.1.4:
82848289
object-keys "^1.1.1"
82858290

82868291
object.getownpropertydescriptors@^2.0.3:
8287-
version "2.1.5"
8288-
resolved "https://registry.yarnpkg.com/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.1.5.tgz#db5a9002489b64eef903df81d6623c07e5b4b4d3"
8289-
integrity sha512-yDNzckpM6ntyQiGTik1fKV1DcVDRS+w8bvpWNCBanvH5LfRX9O8WTHqQzG4RZwRAM4I0oU7TV11Lj5v0g20ibw==
8292+
version "2.1.6"
8293+
resolved "https://registry.yarnpkg.com/object.getownpropertydescriptors/-/object.getownpropertydescriptors-2.1.6.tgz#5e5c384dd209fa4efffead39e3a0512770ccc312"
8294+
integrity sha512-lq+61g26E/BgHv0ZTFgRvi7NMEPuAxLkFU7rukXjc/AlwH4Am5xXVnIXy3un1bg/JPbXHrixRkK1itUzzPiIjQ==
82908295
dependencies:
82918296
array.prototype.reduce "^1.0.5"
82928297
call-bind "^1.0.2"
8293-
define-properties "^1.1.4"
8294-
es-abstract "^1.20.4"
8298+
define-properties "^1.2.0"
8299+
es-abstract "^1.21.2"
8300+
safe-array-concat "^1.0.0"
82958301

82968302
object.pick@^1.3.0:
82978303
version "1.3.0"
@@ -8710,9 +8716,9 @@ prettier@2.2.1:
87108716
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
87118717

87128718
prettier@^2.6.2:
8713-
version "2.8.7"
8714-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.7.tgz#bb79fc8729308549d28fe3a98fce73d2c0656450"
8715-
integrity sha512-yPngTo3aXUUmyuTjeTUT75txrf+aMh9FiD7q9ZE/i6r0bPb22g4FsE6Y338PQX1bmfy08i9QQCB7/rcUAVntfw==
8719+
version "2.8.8"
8720+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da"
8721+
integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q==
87168722

87178723
pretty-format@^25.2.1, pretty-format@^25.5.0:
87188724
version "25.5.0"
@@ -9285,6 +9291,16 @@ rxjs@^6.6.0:
92859291
dependencies:
92869292
tslib "^1.9.0"
92879293

9294+
safe-array-concat@^1.0.0:
9295+
version "1.0.0"
9296+
resolved "https://registry.yarnpkg.com/safe-array-concat/-/safe-array-concat-1.0.0.tgz#2064223cba3c08d2ee05148eedbc563cd6d84060"
9297+
integrity sha512-9dVEFruWIsnie89yym+xWTAYASdpw3CJV7Li/6zBewGf9z2i1j31rP6jnY0pHEO4QZh6N0K11bFjWmdR8UGdPQ==
9298+
dependencies:
9299+
call-bind "^1.0.2"
9300+
get-intrinsic "^1.2.0"
9301+
has-symbols "^1.0.3"
9302+
isarray "^2.0.5"
9303+
92889304
safe-buffer@^5.0.1, safe-buffer@^5.1.2, safe-buffer@^5.2.1, safe-buffer@~5.2.0:
92899305
version "5.2.1"
92909306
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
@@ -10624,9 +10640,9 @@ which-boxed-primitive@^1.0.2:
1062410640
is-symbol "^1.0.3"
1062510641

1062610642
which-module@^2.0.0:
10627-
version "2.0.0"
10628-
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a"
10629-
integrity sha512-B+enWhmw6cjfVC7kS8Pj9pCrKSc5txArRyaYGe088shv/FGWH+0Rjx/xPgtsWfsUtS27FkP697E4DDhgrgoc0Q==
10643+
version "2.0.1"
10644+
resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.1.tgz#776b1fe35d90aebe99e8ac15eb24093389a4a409"
10645+
integrity sha512-iBdZ57RDvnOR9AGBhML2vFZf7h8vmBjhoaZqODJBFWHVtKkDmKuHai3cx5PgVMrX5YDNp27AofYbAwctSS+vhQ==
1063010646

1063110647
which-typed-array@^1.1.2, which-typed-array@^1.1.9:
1063210648
version "1.1.9"

0 commit comments

Comments
 (0)