Skip to content

Commit

Permalink
fix(python): relative import missing for kwargs types (#1768)
Browse files Browse the repository at this point in the history
Relative import statements were not emitted for lifted keyword argument types,
occasionally leading to invalid python modules. This adds the necessary feature
to collect required imports from keyword arguments.



---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
RomainMuller authored Jul 1, 2020
1 parent cacbedb commit c174ac5
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/jsii-calc/lib/submodule/my-class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nested_submodule } from './nested_submodule';
import { Awesomeness, Goodness } from './child';
import { Awesomeness, Goodness, SomeStruct } from './child';
import { AllTypes } from '..';

export class MyClass implements nested_submodule.deeplyNested.INamespaced {
Expand All @@ -8,5 +8,5 @@ export class MyClass implements nested_submodule.deeplyNested.INamespaced {
public readonly awesomeness = Awesomeness.AWESOME;
public allTypes?: AllTypes;

public constructor() { }
public constructor(public readonly props: SomeStruct) { }
}
26 changes: 24 additions & 2 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -12789,7 +12789,15 @@
"initializer": {
"docs": {
"stability": "experimental"
}
},
"parameters": [
{
"name": "props",
"type": {
"fqn": "jsii-calc.submodule.child.SomeStruct"
}
}
]
},
"interfaces": [
"jsii-calc.submodule.nested_submodule.deeplyNested.INamespaced"
Expand Down Expand Up @@ -12845,6 +12853,20 @@
"fqn": "jsii-calc.submodule.child.Goodness"
}
},
{
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodule/my-class.ts",
"line": 11
},
"name": "props",
"type": {
"fqn": "jsii-calc.submodule.child.SomeStruct"
}
},
{
"docs": {
"stability": "experimental"
Expand Down Expand Up @@ -13196,5 +13218,5 @@
}
},
"version": "0.0.0",
"fingerprint": "/0S7DMfFaE1kfja4CpY5k0Gl6j6opo50SUPCiBgy3Zg="
"fingerprint": "5Tv5ECDhqPZgUeGoaL8LTa8pvOZnosdszen+rYjvQ8s="
}
3 changes: 3 additions & 0 deletions packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,9 @@ abstract class BaseMethod implements PythonBase {
...this.parameters.map((param) =>
toTypeName(param).requiredImports(context),
),
...(this.liftedProp?.properties?.map((prop) =>
toTypeName(prop.type).requiredImports(context),
) ?? []),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12789,7 +12789,15 @@
"initializer": {
"docs": {
"stability": "experimental"
}
},
"parameters": [
{
"name": "props",
"type": {
"fqn": "jsii-calc.submodule.child.SomeStruct"
}
}
]
},
"interfaces": [
"jsii-calc.submodule.nested_submodule.deeplyNested.INamespaced"
Expand Down Expand Up @@ -12845,6 +12853,20 @@
"fqn": "jsii-calc.submodule.child.Goodness"
}
},
{
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodule/my-class.ts",
"line": 11
},
"name": "props",
"type": {
"fqn": "jsii-calc.submodule.child.SomeStruct"
}
},
{
"docs": {
"stability": "experimental"
Expand Down Expand Up @@ -13196,5 +13218,5 @@
}
},
"version": "0.0.0",
"fingerprint": "/0S7DMfFaE1kfja4CpY5k0Gl6j6opo50SUPCiBgy3Zg="
"fingerprint": "5Tv5ECDhqPZgUeGoaL8LTa8pvOZnosdszen+rYjvQ8s="
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Amazon.JSII.Tests.CalculatorNamespace.Submodule
/// <remarks>
/// <strong>Stability</strong>: Experimental
/// </remarks>
[JsiiClass(nativeType: typeof(Amazon.JSII.Tests.CalculatorNamespace.Submodule.MyClass), fullyQualifiedName: "jsii-calc.submodule.MyClass")]
[JsiiClass(nativeType: typeof(Amazon.JSII.Tests.CalculatorNamespace.Submodule.MyClass), fullyQualifiedName: "jsii-calc.submodule.MyClass", parametersJson: "[{\"name\":\"props\",\"type\":{\"fqn\":\"jsii-calc.submodule.child.SomeStruct\"}}]")]
public class MyClass : DeputyBase, Amazon.JSII.Tests.CalculatorNamespace.Submodule.NestedSubmodule.DeeplyNested.INamespaced
{
/// <remarks>
/// <strong>Stability</strong>: Experimental
/// </remarks>
public MyClass(): base(new DeputyProps(new object[]{}))
public MyClass(Amazon.JSII.Tests.CalculatorNamespace.Submodule.Child.ISomeStruct props): base(new DeputyProps(new object[]{props}))
{
}

Expand Down Expand Up @@ -58,6 +58,15 @@ public virtual Amazon.JSII.Tests.CalculatorNamespace.Submodule.Child.Goodness Go
get => GetInstanceProperty<Amazon.JSII.Tests.CalculatorNamespace.Submodule.Child.Goodness>();
}

/// <remarks>
/// <strong>Stability</strong>: Experimental
/// </remarks>
[JsiiProperty(name: "props", typeJson: "{\"fqn\":\"jsii-calc.submodule.child.SomeStruct\"}")]
public virtual Amazon.JSII.Tests.CalculatorNamespace.Submodule.Child.ISomeStruct Props
{
get => GetInstanceProperty<Amazon.JSII.Tests.CalculatorNamespace.Submodule.Child.ISomeStruct>();
}

/// <remarks>
/// <strong>Stability</strong>: Experimental
/// </remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ protected MyClass(final software.amazon.jsii.JsiiObject.InitializationMode initi

/**
* EXPERIMENTAL
* <p>
* @param props This parameter is required.
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public MyClass() {
public MyClass(final @org.jetbrains.annotations.NotNull software.amazon.jsii.tests.calculator.submodule.child.SomeStruct props) {
super(software.amazon.jsii.JsiiObject.InitializationMode.JSII);
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this);
software.amazon.jsii.JsiiEngine.getInstance().createNewObject(this, new Object[] { java.util.Objects.requireNonNull(props, "props is required") });
}

/**
Expand Down Expand Up @@ -50,6 +52,14 @@ public MyClass() {
return this.jsiiGet("goodness", software.amazon.jsii.tests.calculator.submodule.child.Goodness.class);
}

/**
* EXPERIMENTAL
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public @org.jetbrains.annotations.NotNull software.amazon.jsii.tests.calculator.submodule.child.SomeStruct getProps() {
return this.jsiiGet("props", software.amazon.jsii.tests.calculator.submodule.child.SomeStruct.class);
}

/**
* EXPERIMENTAL
*/
Expand All @@ -65,4 +75,49 @@ public MyClass() {
public void setAllTypes(final @org.jetbrains.annotations.Nullable software.amazon.jsii.tests.calculator.AllTypes value) {
this.jsiiSet("allTypes", value);
}

/**
* A fluent builder for {@link software.amazon.jsii.tests.calculator.submodule.MyClass}.
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public static final class Builder implements software.amazon.jsii.Builder<software.amazon.jsii.tests.calculator.submodule.MyClass> {
/**
* EXPERIMENTAL
* <p>
* @return a new instance of {@link Builder}.
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public static Builder create() {
return new Builder();
}

private final software.amazon.jsii.tests.calculator.submodule.child.SomeStruct.Builder props;

private Builder() {
this.props = new software.amazon.jsii.tests.calculator.submodule.child.SomeStruct.Builder();
}

/**
* EXPERIMENTAL
* <p>
* @return {@code this}
* @param prop This parameter is required.
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
public Builder prop(final software.amazon.jsii.tests.calculator.submodule.child.SomeEnum prop) {
this.props.prop(prop);
return this;
}

/**
* @returns a newly built instance of {@link software.amazon.jsii.tests.calculator.submodule.MyClass}.
*/
@software.amazon.jsii.Stability(software.amazon.jsii.Stability.Level.Experimental)
@Override
public software.amazon.jsii.tests.calculator.submodule.MyClass build() {
return new software.amazon.jsii.tests.calculator.submodule.MyClass(
this.props.build()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from .._jsii import *

from .. import AllTypes as _AllTypes_b08307c5
from .child import Awesomeness as _Awesomeness_d37a24df, Goodness as _Goodness_2df26737
from .child import (
SomeStruct as _SomeStruct_91627123,
SomeEnum as _SomeEnum_b2e41d92,
Awesomeness as _Awesomeness_d37a24df,
Goodness as _Goodness_2df26737,
)
from .nested_submodule.deeply_nested import INamespaced as _INamespaced_e2f386ad


Expand All @@ -22,12 +27,16 @@ class MyClass(metaclass=jsii.JSIIMeta, jsii_type="jsii-calc.submodule.MyClass"):
:stability: experimental
"""

def __init__(self) -> None:
def __init__(self, *, prop: _SomeEnum_b2e41d92) -> None:
"""
:param prop:
stability
:stability: experimental
"""
jsii.create(MyClass, self, [])
props = _SomeStruct_91627123(prop=prop)

jsii.create(MyClass, self, [props])

@builtins.property
@jsii.member(jsii_name="awesomeness")
Expand Down Expand Up @@ -56,6 +65,15 @@ def goodness(self) -> _Goodness_2df26737:
"""
return jsii.get(self, "goodness")

@builtins.property
@jsii.member(jsii_name="props")
def props(self) -> _SomeStruct_91627123:
"""
stability
:stability: experimental
"""
return jsii.get(self, "props")

@builtins.property
@jsii.member(jsii_name="allTypes")
def all_types(self) -> typing.Optional[_AllTypes_b08307c5]:
Expand Down
11 changes: 9 additions & 2 deletions packages/jsii-reflect/test/__snapshots__/jsii-tree.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ exports[`jsii-tree --all 1`] = `
│ │ └─┬ class MyClass (experimental)
│ │ ├── interfaces: INamespaced
│ │ └─┬ members
│ │ ├── <initializer>() initializer (experimental)
│ │ ├─┬ <initializer>(props) initializer (experimental)
│ │ │ └─┬ parameters
│ │ │ └─┬ props
│ │ │ └── type: jsii-calc.submodule.child.SomeStruct
│ │ ├─┬ awesomeness property (experimental)
│ │ │ ├── immutable
│ │ │ └── type: jsii-calc.submodule.child.Awesomeness
Expand All @@ -184,6 +187,9 @@ exports[`jsii-tree --all 1`] = `
│ │ ├─┬ goodness property (experimental)
│ │ │ ├── immutable
│ │ │ └── type: jsii-calc.submodule.child.Goodness
│ │ ├─┬ props property (experimental)
│ │ │ ├── immutable
│ │ │ └── type: jsii-calc.submodule.child.SomeStruct
│ │ └─┬ allTypes property (experimental)
│ │ └── type: Optional<jsii-calc.AllTypes>
│ └─┬ types
Expand Down Expand Up @@ -3062,10 +3068,11 @@ exports[`jsii-tree --members 1`] = `
│ │ └─┬ types
│ │ └─┬ class MyClass
│ │ └─┬ members
│ │ ├── <initializer>() initializer
│ │ ├── <initializer>(props) initializer
│ │ ├── awesomeness property
│ │ ├── definedAt property
│ │ ├── goodness property
│ │ ├── props property
│ │ └── allTypes property
│ └─┬ types
│ ├─┬ class AbstractClass
Expand Down

0 comments on commit c174ac5

Please sign in to comment.