(ec2): Subnet and ISubnet variable naming causes Pylance type error
See original GitHub issueDescribe 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
.
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:
- Created a year ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
yes, the current workaround is to use typing.cast to satisfy the type checker.