Python constructs do not implement their interfaces
See original GitHub issueMany constructs in python do not implement their corresponding interfaces (e.g. aws_ec2.SecurityGroup
does not implement aws_ec2.ISecurityGroup
, Vpc
does not implement IVpc
, HostedZone
does not implment IHostedZone
etc.)
This causes typing errors since some methods are declared as accepting the interface and so will not accept the implementation.
Reproduction Steps
Run mypy on this:
#!/usr/bin/env python3
from aws_cdk import (
core,
aws_ec2 as ec2,
aws_ecs as ecs,
aws_route53 as route53,
aws_ecs_patterns as ecs_patterns,
)
app = core.App()
stack = core.Stack(app, "stack")
vpc = ec2.Vpc(stack, "VPC", cidr="10.0.0.0/16")
sg = ec2.SecurityGroup(
stack,
"SG",
vpc=vpc,
)
zone = route53.HostedZone(
stack,
"Zone",
zone_name="example.com",
vpcs=[vpc],
)
ecs_patterns.ApplicationLoadBalancedFargateService(
stack,
"FGS",
vpc=vpc,
security_groups=[sg],
domain_name="f.example.com",
domain_zone=zone,
task_image_options=ecs_patterns.ApplicationLoadBalancedTaskImageOptions(
image=ecs.ContainerImage.from_registry("foobar"),
container_name="foobar",
),
)
app.synth()
What did you expect to happen?
No errors.
What actually happened?
Multiple typing errors.
$ mypy app.py
app.py:18: error: Argument "vpc" to "SecurityGroup" has incompatible type "Vpc"; expected "IVpc"
app.py:18: note: Following member(s) of "Vpc" have conflicts:
app.py:18: note: Expected:
app.py:18: note: def __jsii_proxy_class__() -> Type[_IVpcProxy]
app.py:18: note: Got:
app.py:18: note: def __jsii_proxy_class__() -> Type[_ResourceProxy]
app.py:24: error: List item 0 has incompatible type "Vpc"; expected "IVpc"
app.py:24: note: Following member(s) of "Vpc" have conflicts:
app.py:24: note: Expected:
app.py:24: note: def __jsii_proxy_class__() -> Type[_IVpcProxy]
app.py:24: note: Got:
app.py:24: note: def __jsii_proxy_class__() -> Type[_ResourceProxy]
app.py:29: error: Argument "vpc" to "ApplicationLoadBalancedFargateService" has incompatible type "Vpc"; expected "Optional[IVpc]"
app.py:30: error: List item 0 has incompatible type "SecurityGroup"; expected "ISecurityGroup"
app.py:30: note: Following member(s) of "SecurityGroup" have conflicts:
app.py:30: note: Expected:
app.py:30: note: def __jsii_proxy_class__() -> Type[_ISecurityGroupProxy]
app.py:30: note: Got:
app.py:30: note: def __jsii_proxy_class__() -> Type[_ResourceProxy]
app.py:32: error: Argument "domain_zone" to "ApplicationLoadBalancedFargateService" has incompatible type "HostedZone"; expected "Optional[IHostedZone]"
Found 5 errors in 1 file (checked 1 source file)
Environment
- CDK CLI Version : 1.92.0 (build de536cc)
- Framework Version: 1.92.0
- Node.js Version: v14.15.0
- OS : macOS 10.15.7
- Language (Version): Python 3.8.7
Other
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Why use (or not use) interfaces in Python? - Chelsea Troy
As far as Python is concerned, an interface is just a special case of abstract class that implements no methods and requires subclasses...
Read more >Python Classes and Interfaces - ThePythonGuru.com
Item 39: Use @classmethod Polymorphism to Construct Objects Generically #. In Python, not only do objects support polymorphism, but classes do ...
Read more >Constructs - AWS Cloud Development Kit (AWS CDK) v2
Constructs are the basic building blocks of AWS CDK apps. A construct represents a "cloud component" and encapsulates everything AWS CloudFormation needs to ......
Read more >Python Interfaces :: CC 410 Textbook
The Python programming language doesn't include direct support for interfaces in the same way as other object-oriented programming languages. However, it is ......
Read more >Pythonic Interfaces - CERN Indico
Currently, the Python language does not have such a construct. Any sort of interface implementation used now relies on verbal, error-prone, “hand-shake” ...
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 FreeTop 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
Top GitHub Comments
OK, I pushed a better test that reproduces the original issue more closely
Verified are all the same version. Looking at the python source you can see the issue, e.g.
aws_cdk/aws_ec2/__init__.py
hasand
So
Vpc
inherits core.Resource inaws_cdk/core/__init__.py
which has:So
Vpc
andIVpc
have different return type signatures on__jsii_proxy_class__
and this is true for many classes and their interfaces in CDK, and so mypy complains.