jsii: MyPy throwing type errors when it shouldn't
In the Python CDK, attempting to pass aws_events_targets.LambdaFunction as the target of an events.Rule fails to typecheck:
$ mypy test.py
test.py:19: error: List item 0 has incompatible type "LambdaFunction"; expected "IRuleTarget"
test.py:19: note: 'LambdaFunction' is missing following 'IRuleTarget' protocol member:
test.py:19: note: __jsii_proxy_class__
Found 1 error in 1 file (checked 1 source file)
It does run perfectly fine, however – this is purely an error in the typesystem.
Reproduction Steps
from aws_cdk import aws_events, aws_events_targets, aws_lambda, core
class Example(core.Construct):
def __init__(self, scope: core.Construct):
super().__init__(scope, "some-name")
some_lambda = aws_lambda.Function(
self,
"some-handler",
runtime=aws_lambda.Runtime.PYTHON_3_8,
code=aws_lambda.AssetCode("some/path"),
handler="some_package.handler",
retry_attempts=0,
)
aws_events.Rule(
self,
"some-rule",
schedule=aws_events.Schedule.cron(minute="*/5"),
targets=[aws_events_targets.LambdaFunction(some_lambda)],
)
pip install mypy && mypy test.py
Error Log
test.py:19: note: 'LambdaFunction' is missing following 'IRuleTarget' protocol member:
test.py:19: note: __jsii_proxy_class__
Environment
- CLI Version: 1.17.13
- Framework Version: 1.31.0
- OS: OS X 10.14.6
- Language: Python 3.8.0 / mypy 0.770
Other
It is also possible this is a bug in mypy.
Possible duplicate: https://github.com/aws/jsii/issues/1262
This is 🐛 Bug Report
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 18
- Comments: 15 (5 by maintainers)
I’m running into the same issue with
aws_cloudwatch.Metricnot correctly implementingIMetric. Here’s a snippet that demonstrates the issue:Checking types with Mypy results in the following error:
Sorry for not making clear that yeah, this is not about the functionality (which works great!), but about the type-correctness of the
jsii-generated typing of the code.I’ve found two other ones: neither
aws_cloudwatch.MathExpressionnoraws_cloudwatch.MetricimplementIMetricas they are documented to, also because they don’t implement__jsii_proxy_class__.@MrArnoldPalmer @RomainMuller Running into this error as well.
@MrArnoldPalmer @RomainMuller any updates on this? All interfaces have this error for me.
We recently had MyPy start throwing some new type errors that previously it wasn’t. Its likely that previously it wasn’t catching this missing implementation and this was fixed in a recent release of MyPy. We can investigate this on the JSII side but its likely that the python bindings are missing MyPy annotations in places. We will have to figure out why this isn’t caught during build time as well.
Please re-read the code I posted. I am using the
aws_cdk.aws_events_targets.LambdaFunctionclass. The bug is thatLambdaFunctiondoes not implementIRuleTargetas it is documented to, because it’s missing a__jsii_proxy_class__method.Which appears to be accurate. Looking at this more closely,
IRuleTargetis a protocol which defines the non-abstract static method__jsii_proxy_class__, as well asbind. Because both are part of the protocol’s definition, it means thatLambdaFunctionmust itself define a static__jsii_proxy_class__method to be anIRuleTarget, which it doesn’t currently: