AWS-CDK Type "(cls: Runtime) -> Runtime" cannot be assigned to type "Runtime"

Question:

I get this flycheck error pointing to the runtime=_lambda.. variable:

Argument of type "(cls: Runtime) -> Runtime" cannot be assigned to parameter "runtime" of type "Runtime" in function "__init__"   Type "(cls: Runtime) -> Runtime" cannot be assigned to type "Runtime"

# create lambda function
# executed as root
function = _lambda.Function(self,
                            "lambda_function",
                            runtime=_lambda.Runtime.PYTHON_3_7,
                            handler="lambda_handler.main",
                            code=_lambda.Code.from_asset("./lambda"),
                            environment={
                                'EC2_INSTANCE_ID':
                                instance.instance_id,
                                'S3_OUTPUT':
                                output_s3.s3_url_for_object(),
                                'S3_INPUT':
                                input_s3.s3_url_for_object()
                            })

It’s a rather cosmetic IDE issue, the code itself works

Asked By: jjk

||

Answers:

This was a bug in jsii, the library CDK uses to transpile TypeScript (the language in which CDK is written) to Python.

Here is the PR that fixed it. The fix was released in 1.64.0

If you are using a version before 1.64.0, you can use casting to suppress the error:

import typing

...

function = lambda_.Function(
    self,
    "function",
    ...
    runtime=typing.cast(lambda_.Runtime, lambda_.Runtime.PYTHON_3_7)
)

Or just append # type: ignore to the end of the line to disable type checking on that particular line.

Answered By: gshpychka
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.