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

Viewed 145

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

1 Answers

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

Here is the open issue on their GitHub.

Until this is fixed, 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.

Related