Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python: cannot extend generated classes in an idiomatic way #3656

Closed
gshpychka opened this issue Jul 12, 2022 · 3 comments · Fixed by #3695
Closed

Python: cannot extend generated classes in an idiomatic way #3656

gshpychka opened this issue Jul 12, 2022 · 3 comments · Fixed by #3695
Assignees
Labels
bug This issue is a bug. effort/medium Medium work item – a couple days of effort p1

Comments

@gshpychka
Copy link

Describe the bug

Take the class NatProvider:
https://github.com/aws/aws-cdk/blob/d03543246c5659cb287566372d47564e6fc2df4c/packages/%40aws-cdk/aws-ec2/lib/nat.ts#L55

I'm implementing my own provider in Python, so I extend the class.
When I implement the configure_nat method, I cannot use the same method signature as in the generated classes.

In the generated code, the signature for that method is the following:

def configure_nat(self, *, nat_subnets: typing.Sequence["PublicSubnet"], private_subnets: typing.Sequence["PrivateSubnet"], vpc: "Vpc")

When I replicate that in my child class, I cannot use this signature.

Expected Behavior

I should be able to extend the class and use the method signatures that match the ones in the generated python code for the parent class.

Current Behavior

When I replicate the method with the same signature, I get this at synth:

jsii.errors.JSIIError: MyNatProvider.configure_nat() takes 1 positional argument but 2 were given

To actually implement the class, my method signature has to be:

def configure_nat(self, options: ec2.ConfigureNatOptions, /)

Reproduction Steps

Subclass NatProvider and implement configure_nat with a method signature that matches the generated parent class.

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.61.0

Environment details (OS name and version, etc.)

Linux

@gshpychka gshpychka added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 12, 2022
@peterwoodworth peterwoodworth added effort/medium Medium work item – a couple days of effort p1 and removed needs-triage This issue or PR still needs to be triaged. labels Jul 21, 2022
@RomainMuller
Copy link
Contributor

Hey!

Can you provide a minimal repro for this issue? That'll help me diagnose & fix it faster...

Thanks!

@gshpychka
Copy link
Author

@jsii.implements(ec2.IConnectable)
class MyNatProvider(ec2.NatProvider):
    def __init__(
        self,
    ) -> None:
        super().__init__()

    # this synths fine, but produces a typing error (Method "configure_nat" overrides class "NarProvider" in an incompatible manner)
    '''def configure_nat(  # type: ignore[misc]
        self, options: ec2.ConfigureNatOptions, /
    ) -> None:
        pass'''


    # this is the idiomatic way to override the method, but does not synth
     def configure_nat(
        self,
        *,
        nat_subnets: typing.Sequence[ec2.Subnet],
        private_subnets: typing.Sequence[ec2.PrivateSubnet],
        vpc: ec2.Vpc
    ) -> None:
        pass

vpc = ec2.Vpc(
    self,
    "Vpc",
    nat_gateways=1,
    nat_gateway_provider=MyNatProvider(),
)

RomainMuller added a commit that referenced this issue Aug 2, 2022
The callback logic in the jsii runtime library for Python was
incorrectly passing keyword arguments to Python implementations,
forwarding a struct instance as-is instead of destructuring it into a
keyword arguments mapping.

Using `inspect.signature()`, detect the presence of keyword arguments on
the callback target, and destructure the struct as relevant.

Fixes #3656
RomainMuller added a commit that referenced this issue Aug 2, 2022
The callback logic in the jsii runtime library for Python was
incorrectly passing keyword arguments to Python implementations,
forwarding a struct instance as-is instead of destructuring it into a
keyword arguments mapping.

Using `inspect.signature()`, detect the presence of keyword arguments on
the callback target, and destructure the struct as relevant.

Fixes #3656
@RomainMuller RomainMuller self-assigned this Aug 2, 2022
@mergify mergify bot closed this as completed in #3695 Aug 2, 2022
mergify bot pushed a commit that referenced this issue Aug 2, 2022
The callback logic in the jsii runtime library for Python was
incorrectly passing keyword arguments to Python implementations,
forwarding a struct instance as-is instead of destructuring it into a
keyword arguments mapping.

Using `inspect.signature()`, detect the presence of keyword arguments on
the callback target, and destructure the struct as relevant.

Fixes #3656



---

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
@github-actions
Copy link
Contributor

github-actions bot commented Aug 2, 2022

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. effort/medium Medium work item – a couple days of effort p1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants