(python): CDK has incorrect Python type signatures
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 267
- Avg merge
- 1d 25m
- Merged PRs (30d)
- 14
Description
Most of the CDK type signatures use `List` instead of `Sequence`. `List` is invariant http://mypy.readthedocs.io/en/latest/common_issues.html#variance
So this means we can't do something simple like pass a list of `ec2.CfnSecurityGroup.IngressProperty` to a `ec2.CfnSecurityGroup` without mypy errors.
### Reproduction Steps
Run mypy on this code fragment:
```python
from aws_cdk import (
core,
aws_ec2 as ec2,
)
stack = core.Stack()
ingress_rules = [
ec2.CfnSecurityGroup.IngressProperty(
from_port=22,
to_port=22,
ip_protocol="tcp",
cidr_ip="0.0.0.0/0",
)
]
ec2.CfnSecurityGroup(
stack,
"SecurityGroup",
group_description="Sample SG",
security_group_ingress=ingress_rules,
)
```
### What did you expect to happen?
mypy should succeed
### What actually happened?
mypy failed
```
$ ../../env/bin/mypy /tmp/invariant.py
/tmp/invariant.py:19: error: Argument "security_group_ingress" to "CfnSecurityGroup" has incompatible type "List[IngressProperty]"; expected "Union[IResolvable, List[Union[IResolvable, IngressProperty]], None]"
/tmp/invariant.py:19: note: "List" is invariant -- see http://mypy.readthedocs.io/en/latest/common_issues.html#variance
/tmp/invariant.py:19: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)
```
### Environment
- **CDK CLI Version :** 1.88.0 (build f65009b)
- **Framework Version:** 1.90.1
- **Node.js Version:** v14.15.0
- **OS :** macOS 10.15.7
- **Language (Version):** Python 3.8.7, mypy 0.812
### Other
In this case `security_group_ingress` is declared in the cdk as
```python
security_group_ingress: typing.Optional[typing.Union[aws_cdk.core.IResolvable, typing.List[typing.Union[aws_cdk.core.IResolvable, "CfnSecurityGroup.IngressProperty"]]]] = None
```
Since List is invariant, we must pass exactly this, so a list of `CfnSecurityGroup.IngressProperty` is not valid. This declaration should use `Sequence` instead of `List` and this is true of many cdk type signatures.
---
This is :bug: Bug Report
Contributor guide
Research direction
Start by running mypy on the reproduction fragment and inspect the generated CfnSecurityGroup declaration shown in the issue. Trace how the Python signatures are produced, then identify the other signatures using List. Done means compatible list values pass mypy without errors and the affected signatures consistently use Sequence where appropriate.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, python, typescript
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100