question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LambdaFunction does not implement IRuleTarget

See original GitHub issue

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.


This is 🐛 Bug Report

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:16
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
salimhamedcommented, Jun 24, 2020

I’m running into the same issue with aws_cloudwatch.Metric not correctly implementing IMetric. Here’s a snippet that demonstrates the issue:

import aws_cdk.aws_cloudwatch as cloudwatch
import aws_cdk.aws_kinesis as kds
from aws_cdk import core


class MyStack(core.Stack):
    def __init__(self, scope: core.Construct, _id: str, **kwargs) -> None:
        super().__init__(scope, _id, **kwargs)

        self.template_options.description = "Demo"

        stream = kds.Stream(self, "InputStream", shard_count=16)

        dashboard = cloudwatch.Dashboard(self, "Dashboard", dashboard_name=core.Aws.STACK_NAME)

        incoming_records = cloudwatch.Metric(
            namespace="AWS/Kinesis",
            metric_name="IncomingRecords",
            dimensions={"StreamName": stream.stream_name},
            period=core.Duration.minutes(1),
            statistic="sum",
        )

        dashboard.add_widgets(
            cloudwatch.GraphWidget(
                left=[incoming_records],  # 'Metric' is missing following 'IMetric' protocol member
                width=24,
                title="Kinesis data stream (incoming)",
                left_y_axis=cloudwatch.YAxisProps(min=0),
                right_y_axis=cloudwatch.YAxisProps(min=0),
            )
        )

Checking types with Mypy results in the following error:

'Metric' is missing following 'IMetric' protocol member:
__jsii_proxy_class__
List item 0 has incompatible type "Metric"; expected "IMetric"
4reactions
alexmvcommented, Apr 10, 2020

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.MathExpression nor aws_cloudwatch.Metric implement IMetric as they are documented to, also because they don’t implement __jsii_proxy_class__.

Read more comments on GitHub >

github_iconTop Results From Across the Web

https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-c...
No information is available for this page.
Read more >
How to resolve AWS CDK error "Argument of type 'Function' is ...
This error Argument of type 'SomeClass' is not assignable to parameter of type 'ISomeClass' typically occurs when version of CDK ...
Read more >
Schedule AWS Lambda Functions using CDK - YouTube
This is a step-by-step tutorial on creating Scheduled Lambda using CDK. In the previous video, we have already done that using AWS Console, ......
Read more >
@aws-cdk/aws-events - npm
Rules are not processed in a particular order. ... Event targets are objects that implement the IRuleTarget interface.
Read more >
Triggers lambda {eventName} - awslabs/aws-cdk - Gitter
@SkyVikerRumsey_twitter just to make sure, are you making use of the aws-events-targets module and passing IRuleTarget correct? can you try targets via the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found