question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

https://github.com/aws/aws-cdk/blob/f199faddc0b8e565aa413e480e51d25fed5321bf/packages/%40aws-cdk/aws-ec2/lib/vpc.ts#L61

https://github.com/aws/aws-cdk/blob/f199faddc0b8e565aa413e480e51d25fed5321bf/packages/%40aws-cdk/aws-ec2/lib/vpc.ts#L1757

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

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
gshpychkacommented, Sep 7, 2022

This happens in a lot of constructs and is being tracked here: https://github.com/aws/aws-cdk/issues/15584 and https://github.com/aws/jsii/issues/2927

1reaction
gshpychkacommented, Oct 14, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

(@aws_cdk): Python: incompatible interface implementations ...
More importantly, it makes these python classes incompatible with their ... (ec2): Subnet and ISubnet variable naming causes Pylance type error #20125.
Read more >
Error codes for the Amazon EC2 API - AWS Documentation
Amazon EC2 has two types of error codes: Client errors. These errors are usually caused by something the client did, such as specifying...
Read more >
How to use the @aws-cdk/core.Tag function in @aws-cdk/core
const tagAllSubnets = (type: string, subnets: ec2.ISubnet[], tag: string) => { for (const subnet of subnets) { // if this is not a...
Read more >
Error with Boto and creating my instance - Stack Overflow
You are trying to use Subnet[0] when there is no such variable defined in the scope. ... reservation = conn.ec2.run_instances(image_id, ...
Read more >
How to fix Name Not Defined Error in Python - YouTube
This video will explain how to solve the 'something' is not defined error in Python.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found