Skip to content

Commit

Permalink
fix(python): correctly handle nested structs-as-dict (#973)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
RomainMuller committed Nov 12, 2019
1 parent 26aa77c commit 9fd2499
Show file tree
Hide file tree
Showing 20 changed files with 947 additions and 4 deletions.
33 changes: 33 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2175,3 +2175,36 @@ class PrivateBell implements IBell {
this.rung = true;
}
}

/**
* This is here to check that we can pass a nested struct into a kwargs by specifying it as an
* in-line dictionary. This is cheating with the declared types, but Python people don't play by
* the rules much apparently.
*/
export interface RootStruct {
/**
* May not be empty.
*/
readonly stringProp: string;
readonly nestedStruct?: NestedStruct;
}
export interface NestedStruct {
/**
* When provided, must be > 0.
*/
readonly numberProp: number;
}
export class RootStructValidator {
public static validate(struct: RootStruct): void {
if (!struct.stringProp) {
throw new Error('Missing required field: stringProp');
}
if (struct.nestedStruct) {
if (struct.nestedStruct.numberProp <= 0) {
throw new Error('numberProp must be > 0');
}
}
}

private constructor() { }
}
118 changes: 117 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -6799,6 +6799,38 @@
}
]
},
"jsii-calc.NestedStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.NestedStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2191
},
"name": "NestedStruct",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "When provided, must be > 0."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2195
},
"name": "numberProp",
"type": {
"primitive": "number"
}
}
]
},
"jsii-calc.NodeStandardLibrary": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -8215,6 +8247,90 @@
}
]
},
"jsii-calc.RootStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"remarks": "This is cheating with the declared types, but Python people don't play by\nthe rules much apparently.",
"stability": "experimental",
"summary": "This is here to check that we can pass a nested struct into a kwargs by specifying it as an in-line dictionary."
},
"fqn": "jsii-calc.RootStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2184
},
"name": "RootStruct",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "May not be empty."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2188
},
"name": "stringProp",
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2189
},
"name": "nestedStruct",
"optional": true,
"type": {
"fqn": "jsii-calc.NestedStruct"
}
}
]
},
"jsii-calc.RootStructValidator": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.RootStructValidator",
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2197
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2198
},
"name": "validate",
"parameters": [
{
"name": "struct",
"type": {
"fqn": "jsii-calc.RootStruct"
}
}
],
"static": true
}
],
"name": "RootStructValidator"
},
"jsii-calc.RuntimeTypeChecking": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -10797,5 +10913,5 @@
}
},
"version": "0.20.3",
"fingerprint": "r/K0k7ocrPmzUuFsCAvxLY2UdNEi6r/ruaoO3PnHiCA="
"fingerprint": "1F+uskR3++T5mjRcWge9oG3H/jJvXm1C3IhR1AwsBTE="
}
5 changes: 5 additions & 0 deletions packages/jsii-kernel/lib/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,11 @@ export const SERIALIZERS: {[k: string]: Serializer} = {
value = data;
}

// Python, for example, allows using plain mapping objects instead of Structs (dyanmic typing, YOLO!)
if (api.isWireMap(value)) {
value = value[api.TOKEN_MAP];
}

value = validateRequiredProps(value as any, namedType.fqn, props);

// Return a dict COPY, we have by-value semantics anyway.
Expand Down
9 changes: 8 additions & 1 deletion packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,13 @@ class Struct extends BasePythonClassType {
code.openBlock(`def __init__(${constructorArguments.join(', ')})`);
this.emitConstructorDocstring(code);

// Re-type struct arguments that were passed as "dict"
for (const member of members.filter(m => m.isStruct(this.generator))) {
// Note that "None" is NOT an instance of dict (that's convenient!)
const typeName = resolver.resolve(member.type, { ignoreOptional: true });
code.line(`if isinstance(${member.pythonName}, dict): ${member.pythonName} = ${typeName}(**${member.pythonName})`);
}

// Required properties, those will always be put into the dict
code.line('self._values = {');
for (const member of members.filter(m => !m.optional)) {
Expand Down Expand Up @@ -719,7 +726,7 @@ class StructField implements PythonBase {
public readonly pythonName: string;
public readonly jsiiName: string;
public readonly docs?: spec.Docs;
private readonly type: spec.OptionalValue;
public readonly type: spec.OptionalValue;

public constructor(public readonly prop: spec.Property) {
this.pythonName = toPythonPropertyName(prop.name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6799,6 +6799,38 @@
}
]
},
"jsii-calc.NestedStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.NestedStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2191
},
"name": "NestedStruct",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "When provided, must be > 0."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2195
},
"name": "numberProp",
"type": {
"primitive": "number"
}
}
]
},
"jsii-calc.NodeStandardLibrary": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -8215,6 +8247,90 @@
}
]
},
"jsii-calc.RootStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"remarks": "This is cheating with the declared types, but Python people don't play by\nthe rules much apparently.",
"stability": "experimental",
"summary": "This is here to check that we can pass a nested struct into a kwargs by specifying it as an in-line dictionary."
},
"fqn": "jsii-calc.RootStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2184
},
"name": "RootStruct",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "May not be empty."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2188
},
"name": "stringProp",
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2189
},
"name": "nestedStruct",
"optional": true,
"type": {
"fqn": "jsii-calc.NestedStruct"
}
}
]
},
"jsii-calc.RootStructValidator": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.RootStructValidator",
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2197
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 2198
},
"name": "validate",
"parameters": [
{
"name": "struct",
"type": {
"fqn": "jsii-calc.RootStruct"
}
}
],
"static": true
}
],
"name": "RootStructValidator"
},
"jsii-calc.RuntimeTypeChecking": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -10797,5 +10913,5 @@
}
},
"version": "0.20.3",
"fingerprint": "r/K0k7ocrPmzUuFsCAvxLY2UdNEi6r/ruaoO3PnHiCA="
"fingerprint": "1F+uskR3++T5mjRcWge9oG3H/jJvXm1C3IhR1AwsBTE="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Amazon.JSII.Runtime.Deputy;

namespace Amazon.JSII.Tests.CalculatorNamespace
{
/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiInterface(nativeType: typeof(INestedStruct), fullyQualifiedName: "jsii-calc.NestedStruct")]
public interface INestedStruct
{
/// <summary>When provided, must be > 0.</summary>
/// <remarks>
/// stability: Experimental
/// </remarks>
[JsiiProperty(name: "numberProp", typeJson: "{\"primitive\":\"number\"}")]
double NumberProp
{
get;
}
}
}
Loading

0 comments on commit 9fd2499

Please sign in to comment.