Skip to content

Commit fe9d63e

Browse files
RomainMullermergify[bot]
authored andcommitted
fix(python): correctly handle nested structs-as-dict (#973)
* fix(python): correctly handle nested structs-as-dict Added code to up-cast from `dict` to the relevant struct type in the generated python code whenever the value type is some unambiguous struct. Whenever a UNION of types is involved, the user must construct the struct type themselves, as the runtime would then have no way to choose which type should be used on it's own without additional information. * add None explicit test
1 parent 42c5225 commit fe9d63e

20 files changed

Lines changed: 947 additions & 4 deletions

File tree

packages/jsii-calc/lib/compliance.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,3 +2175,36 @@ class PrivateBell implements IBell {
21752175
this.rung = true;
21762176
}
21772177
}
2178+
2179+
/**
2180+
* This is here to check that we can pass a nested struct into a kwargs by specifying it as an
2181+
* in-line dictionary. This is cheating with the declared types, but Python people don't play by
2182+
* the rules much apparently.
2183+
*/
2184+
export interface RootStruct {
2185+
/**
2186+
* May not be empty.
2187+
*/
2188+
readonly stringProp: string;
2189+
readonly nestedStruct?: NestedStruct;
2190+
}
2191+
export interface NestedStruct {
2192+
/**
2193+
* When provided, must be > 0.
2194+
*/
2195+
readonly numberProp: number;
2196+
}
2197+
export class RootStructValidator {
2198+
public static validate(struct: RootStruct): void {
2199+
if (!struct.stringProp) {
2200+
throw new Error('Missing required field: stringProp');
2201+
}
2202+
if (struct.nestedStruct) {
2203+
if (struct.nestedStruct.numberProp <= 0) {
2204+
throw new Error('numberProp must be > 0');
2205+
}
2206+
}
2207+
}
2208+
2209+
private constructor() { }
2210+
}

packages/jsii-calc/test/assembly.jsii

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6799,6 +6799,38 @@
67996799
}
68006800
]
68016801
},
6802+
"jsii-calc.NestedStruct": {
6803+
"assembly": "jsii-calc",
6804+
"datatype": true,
6805+
"docs": {
6806+
"stability": "experimental"
6807+
},
6808+
"fqn": "jsii-calc.NestedStruct",
6809+
"kind": "interface",
6810+
"locationInModule": {
6811+
"filename": "lib/compliance.ts",
6812+
"line": 2191
6813+
},
6814+
"name": "NestedStruct",
6815+
"properties": [
6816+
{
6817+
"abstract": true,
6818+
"docs": {
6819+
"stability": "experimental",
6820+
"summary": "When provided, must be > 0."
6821+
},
6822+
"immutable": true,
6823+
"locationInModule": {
6824+
"filename": "lib/compliance.ts",
6825+
"line": 2195
6826+
},
6827+
"name": "numberProp",
6828+
"type": {
6829+
"primitive": "number"
6830+
}
6831+
}
6832+
]
6833+
},
68026834
"jsii-calc.NodeStandardLibrary": {
68036835
"assembly": "jsii-calc",
68046836
"docs": {
@@ -8215,6 +8247,90 @@
82158247
}
82168248
]
82178249
},
8250+
"jsii-calc.RootStruct": {
8251+
"assembly": "jsii-calc",
8252+
"datatype": true,
8253+
"docs": {
8254+
"remarks": "This is cheating with the declared types, but Python people don't play by\nthe rules much apparently.",
8255+
"stability": "experimental",
8256+
"summary": "This is here to check that we can pass a nested struct into a kwargs by specifying it as an in-line dictionary."
8257+
},
8258+
"fqn": "jsii-calc.RootStruct",
8259+
"kind": "interface",
8260+
"locationInModule": {
8261+
"filename": "lib/compliance.ts",
8262+
"line": 2184
8263+
},
8264+
"name": "RootStruct",
8265+
"properties": [
8266+
{
8267+
"abstract": true,
8268+
"docs": {
8269+
"stability": "experimental",
8270+
"summary": "May not be empty."
8271+
},
8272+
"immutable": true,
8273+
"locationInModule": {
8274+
"filename": "lib/compliance.ts",
8275+
"line": 2188
8276+
},
8277+
"name": "stringProp",
8278+
"type": {
8279+
"primitive": "string"
8280+
}
8281+
},
8282+
{
8283+
"abstract": true,
8284+
"docs": {
8285+
"stability": "experimental"
8286+
},
8287+
"immutable": true,
8288+
"locationInModule": {
8289+
"filename": "lib/compliance.ts",
8290+
"line": 2189
8291+
},
8292+
"name": "nestedStruct",
8293+
"optional": true,
8294+
"type": {
8295+
"fqn": "jsii-calc.NestedStruct"
8296+
}
8297+
}
8298+
]
8299+
},
8300+
"jsii-calc.RootStructValidator": {
8301+
"assembly": "jsii-calc",
8302+
"docs": {
8303+
"stability": "experimental"
8304+
},
8305+
"fqn": "jsii-calc.RootStructValidator",
8306+
"kind": "class",
8307+
"locationInModule": {
8308+
"filename": "lib/compliance.ts",
8309+
"line": 2197
8310+
},
8311+
"methods": [
8312+
{
8313+
"docs": {
8314+
"stability": "experimental"
8315+
},
8316+
"locationInModule": {
8317+
"filename": "lib/compliance.ts",
8318+
"line": 2198
8319+
},
8320+
"name": "validate",
8321+
"parameters": [
8322+
{
8323+
"name": "struct",
8324+
"type": {
8325+
"fqn": "jsii-calc.RootStruct"
8326+
}
8327+
}
8328+
],
8329+
"static": true
8330+
}
8331+
],
8332+
"name": "RootStructValidator"
8333+
},
82188334
"jsii-calc.RuntimeTypeChecking": {
82198335
"assembly": "jsii-calc",
82208336
"docs": {
@@ -10797,5 +10913,5 @@
1079710913
}
1079810914
},
1079910915
"version": "0.20.3",
10800-
"fingerprint": "r/K0k7ocrPmzUuFsCAvxLY2UdNEi6r/ruaoO3PnHiCA="
10916+
"fingerprint": "1F+uskR3++T5mjRcWge9oG3H/jJvXm1C3IhR1AwsBTE="
1080110917
}

packages/jsii-kernel/lib/serialization.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,11 @@ export const SERIALIZERS: {[k: string]: Serializer} = {
360360
value = data;
361361
}
362362

363+
// Python, for example, allows using plain mapping objects instead of Structs (dyanmic typing, YOLO!)
364+
if (api.isWireMap(value)) {
365+
value = value[api.TOKEN_MAP];
366+
}
367+
363368
value = validateRequiredProps(value as any, namedType.fqn, props);
364369

365370
// Return a dict COPY, we have by-value semantics anyway.

packages/jsii-pacmak/lib/targets/python.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,13 @@ class Struct extends BasePythonClassType {
665665
code.openBlock(`def __init__(${constructorArguments.join(', ')})`);
666666
this.emitConstructorDocstring(code);
667667

668+
// Re-type struct arguments that were passed as "dict"
669+
for (const member of members.filter(m => m.isStruct(this.generator))) {
670+
// Note that "None" is NOT an instance of dict (that's convenient!)
671+
const typeName = resolver.resolve(member.type, { ignoreOptional: true });
672+
code.line(`if isinstance(${member.pythonName}, dict): ${member.pythonName} = ${typeName}(**${member.pythonName})`);
673+
}
674+
668675
// Required properties, those will always be put into the dict
669676
code.line('self._values = {');
670677
for (const member of members.filter(m => !m.optional)) {
@@ -723,7 +730,7 @@ class StructField implements PythonBase {
723730
public readonly pythonName: string;
724731
public readonly jsiiName: string;
725732
public readonly docs?: spec.Docs;
726-
private readonly type: spec.OptionalValue;
733+
public readonly type: spec.OptionalValue;
727734

728735
public constructor(public readonly prop: spec.Property) {
729736
this.pythonName = toPythonPropertyName(prop.name);

packages/jsii-pacmak/test/expected.jsii-calc/dotnet/Amazon.JSII.Tests.CalculatorPackageId/.jsii

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6799,6 +6799,38 @@
67996799
}
68006800
]
68016801
},
6802+
"jsii-calc.NestedStruct": {
6803+
"assembly": "jsii-calc",
6804+
"datatype": true,
6805+
"docs": {
6806+
"stability": "experimental"
6807+
},
6808+
"fqn": "jsii-calc.NestedStruct",
6809+
"kind": "interface",
6810+
"locationInModule": {
6811+
"filename": "lib/compliance.ts",
6812+
"line": 2191
6813+
},
6814+
"name": "NestedStruct",
6815+
"properties": [
6816+
{
6817+
"abstract": true,
6818+
"docs": {
6819+
"stability": "experimental",
6820+
"summary": "When provided, must be > 0."
6821+
},
6822+
"immutable": true,
6823+
"locationInModule": {
6824+
"filename": "lib/compliance.ts",
6825+
"line": 2195
6826+
},
6827+
"name": "numberProp",
6828+
"type": {
6829+
"primitive": "number"
6830+
}
6831+
}
6832+
]
6833+
},
68026834
"jsii-calc.NodeStandardLibrary": {
68036835
"assembly": "jsii-calc",
68046836
"docs": {
@@ -8215,6 +8247,90 @@
82158247
}
82168248
]
82178249
},
8250+
"jsii-calc.RootStruct": {
8251+
"assembly": "jsii-calc",
8252+
"datatype": true,
8253+
"docs": {
8254+
"remarks": "This is cheating with the declared types, but Python people don't play by\nthe rules much apparently.",
8255+
"stability": "experimental",
8256+
"summary": "This is here to check that we can pass a nested struct into a kwargs by specifying it as an in-line dictionary."
8257+
},
8258+
"fqn": "jsii-calc.RootStruct",
8259+
"kind": "interface",
8260+
"locationInModule": {
8261+
"filename": "lib/compliance.ts",
8262+
"line": 2184
8263+
},
8264+
"name": "RootStruct",
8265+
"properties": [
8266+
{
8267+
"abstract": true,
8268+
"docs": {
8269+
"stability": "experimental",
8270+
"summary": "May not be empty."
8271+
},
8272+
"immutable": true,
8273+
"locationInModule": {
8274+
"filename": "lib/compliance.ts",
8275+
"line": 2188
8276+
},
8277+
"name": "stringProp",
8278+
"type": {
8279+
"primitive": "string"
8280+
}
8281+
},
8282+
{
8283+
"abstract": true,
8284+
"docs": {
8285+
"stability": "experimental"
8286+
},
8287+
"immutable": true,
8288+
"locationInModule": {
8289+
"filename": "lib/compliance.ts",
8290+
"line": 2189
8291+
},
8292+
"name": "nestedStruct",
8293+
"optional": true,
8294+
"type": {
8295+
"fqn": "jsii-calc.NestedStruct"
8296+
}
8297+
}
8298+
]
8299+
},
8300+
"jsii-calc.RootStructValidator": {
8301+
"assembly": "jsii-calc",
8302+
"docs": {
8303+
"stability": "experimental"
8304+
},
8305+
"fqn": "jsii-calc.RootStructValidator",
8306+
"kind": "class",
8307+
"locationInModule": {
8308+
"filename": "lib/compliance.ts",
8309+
"line": 2197
8310+
},
8311+
"methods": [
8312+
{
8313+
"docs": {
8314+
"stability": "experimental"
8315+
},
8316+
"locationInModule": {
8317+
"filename": "lib/compliance.ts",
8318+
"line": 2198
8319+
},
8320+
"name": "validate",
8321+
"parameters": [
8322+
{
8323+
"name": "struct",
8324+
"type": {
8325+
"fqn": "jsii-calc.RootStruct"
8326+
}
8327+
}
8328+
],
8329+
"static": true
8330+
}
8331+
],
8332+
"name": "RootStructValidator"
8333+
},
82188334
"jsii-calc.RuntimeTypeChecking": {
82198335
"assembly": "jsii-calc",
82208336
"docs": {
@@ -10797,5 +10913,5 @@
1079710913
}
1079810914
},
1079910915
"version": "0.20.3",
10800-
"fingerprint": "r/K0k7ocrPmzUuFsCAvxLY2UdNEi6r/ruaoO3PnHiCA="
10916+
"fingerprint": "1F+uskR3++T5mjRcWge9oG3H/jJvXm1C3IhR1AwsBTE="
1080110917
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Amazon.JSII.Runtime.Deputy;
2+
3+
namespace Amazon.JSII.Tests.CalculatorNamespace
4+
{
5+
/// <remarks>
6+
/// stability: Experimental
7+
/// </remarks>
8+
[JsiiInterface(nativeType: typeof(INestedStruct), fullyQualifiedName: "jsii-calc.NestedStruct")]
9+
public interface INestedStruct
10+
{
11+
/// <summary>When provided, must be > 0.</summary>
12+
/// <remarks>
13+
/// stability: Experimental
14+
/// </remarks>
15+
[JsiiProperty(name: "numberProp", typeJson: "{\"primitive\":\"number\"}")]
16+
double NumberProp
17+
{
18+
get;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)