Open rectalogic opened 3 years ago
@RomainMuller wondering what you think about this? This would be a breaking change for jsii packages targeting python. I do agree that the list invariability makes cdk usage annoying in a lot of cases. For example, to get the above example working by using an explicit type definition you have to do:
ingress_rules: List[Union[ec2.CfnSecurityGroup.IngressProperty, core.IResolvable]] = [
ec2.CfnSecurityGroup.IngressProperty(
from_port=22,
to_port=22,
ip_protocol="tcp",
cidr_ip="0.0.0.0/0",
)
]
ec2.CfnSecurityGroup(
self,
"SecurityGroup",
group_description="sample sg",
security_group_ingress=ingress_rules,
)
Is there a cleaner workaround?
If we accept sequences on inputs, we're not breaking anything. We may need to continue returning lists on outputs though.
transferring to jsii
Most of the CDK type signatures use
List
instead ofSequence
.List
is invariant http://mypy.readthedocs.io/en/latest/common_issues.html#varianceSo this means we can't do something simple like pass a list of
ec2.CfnSecurityGroup.IngressProperty
to aec2.CfnSecurityGroup
without mypy errors.Reproduction Steps
Run mypy on this code fragment:
What did you expect to happen?
mypy should succeed
What actually happened?
mypy failed
Environment
Other
In this case
security_group_ingress
is declared in the cdk asSince List is invariant, we must pass exactly this, so a list of
CfnSecurityGroup.IngressProperty
is not valid. This declaration should useSequence
instead ofList
and this is true of many cdk type signatures.This is :bug: Bug Report