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

(ec2): Subnet and ISubnet variable naming causes Pylance type error #20125

Closed
jakestambaugh opened this issue Apr 28, 2022 · 7 comments
Closed
Labels
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. language/python Related to Python bindings p1

Comments

@jakestambaugh
Copy link

jakestambaugh commented Apr 28, 2022

Describe the bug

The L2 construct Subnet doesn't use the same method parameter names as ISubnet in associate_network_acl. This makes Pylance believe that Subnet does not implement the interface ISubnet, causing it to raise warnings.

Expected Behavior

Pylance would understand that "Subnet" implements "ISubnet" and not raise errors.

Current Behavior

(variable) subnet: Subnet
Argument of type "list[Subnet]" cannot be assigned to parameter "subnets" of type "Sequence[ISubnet] | None" in function "__init__"
  Type "list[Subnet]" cannot be assigned to type "Sequence[ISubnet] | None"
    TypeVar "_T_co@Sequence" is covariant
      "Subnet" is incompatible with protocol "ISubnet"
        "associate_network_acl" is an incompatible type
          Type "(id: str, network_acl: INetworkAcl) -> None" cannot be assigned to type "(id: str, acl: INetworkAcl) -> None"

Reproduction Steps

In VS Code with the "Pylance" language server extension enabled, add this stack to a CDK project and look for the red squiggly lines in the SubnetSelection constructor.

from aws_cdk import (
    # Duration,
    Stack,
    aws_ec2 as ec2,
)
from constructs import Construct


class ReproStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        vpc = ec2.Vpc(self, "vpc")
        subnet = ec2.Subnet(
            self,
            "subnet",
            availability_zone="us-east-1a",
            vpc_id=vpc.vpc_id,
            cidr_block="10.0.0.0/16",
        )

        subnet_filter = ec2.SubnetSelection(subnets=[subnet])

Possible Solution

Change the parameter name for the INetworkAcl type to be consistent across Subnet and ISubnet.

associateNetworkAcl(id: string, acl: INetworkAcl): void;

public associateNetworkAcl(id: string, networkAcl: INetworkAcl) {

Additional Information/Context

No response

CDK CLI Version

2.22.0 (build 1db4b16)

Framework Version

Pylance v2022.4.3

Node.js Version

v16.14.2

OS

Ubuntu 20.04.4 LTS (WSL 2)

Language

Python

Language Version

3.10.4

Other information

No response

@jakestambaugh jakestambaugh added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 28, 2022
@github-actions github-actions bot added the @aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud label Apr 28, 2022
@ryparker ryparker added the p2 label Apr 28, 2022
@corymhall
Copy link
Contributor

@jakestambaugh I was not able to reproduce this issue. I created a new project with

npx cdk init --language=python app

I then opened VSCode and installed the
Pylance extension
and used the code that you provided in the description.

Is there anything else about your setup that could be causing the issue?

@corymhall corymhall added the needs-reproduction This issue needs reproduction. label May 3, 2022
@jakestambaugh
Copy link
Author

I think that I installed Pylance as a dependency of the "Python" package (https://marketplace.visualstudio.com/items?itemName=ms-python.python). It could be that the default Pylance extension settings don't configure linting.

FWIW, I was able to reproduce this on a fresh Ubuntu installation with the Python extension.

If you want to add this to the .vscode/extensions.json file it will suggest the Python plugin:

{
    "recommendations": [
        "ms-python.python"
    ]
}

@corymhall corymhall added the language/python Related to Python bindings label May 23, 2022
@NGL321 NGL321 removed the needs-triage This issue or PR still needs to be triaged. label May 25, 2022
@corymhall corymhall added p1 and removed p2 needs-reproduction This issue needs reproduction. labels Aug 2, 2022
@corymhall corymhall removed their assignment Aug 2, 2022
@gshpychka
Copy link
Contributor

This happens in a lot of constructs and is being tracked here: aws/jsii#4541 and aws/jsii#2927

@skalinets
Copy link

Are there any workarounds available? Like muting pyright warnings etc. The issues from previous comment are more than one year old :) Just wondering how people work with python CDK.

@gshpychka
Copy link
Contributor

Are there any workarounds available? Like muting pyright warnings etc. The issues from previous comment are more than one year old :) Just wondering how people work with python CDK.

yes, the current workaround is to use typing.cast to satisfy the type checker.

@MrArnoldPalmer
Copy link
Contributor

Closing this as a duplicate of aws/jsii#4541 track there for updates/comments.

@github-actions
Copy link

⚠️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
@aws-cdk/aws-ec2 Related to Amazon Elastic Compute Cloud bug This issue is a bug. language/python Related to Python bindings p1
Projects
None yet
Development

No branches or pull requests

7 participants