无法将 AWS-CDK 类型“(cls: Runtime) -> Runtime”分配给类型 "Runtime"

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

我收到指向 runtime=_lambda.. 变量的 flycheck 错误:

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()
                            })

这是一个相当表面化的问题 IDE,代码本身有效

这是 jsii 中的错误,CDK 库用于将 TypeScript(编写 CDK 的语言)转换为 Python。

这里是the open issue on their GitHub

在修复此问题之前,您可以使用转换来抑制错误:

import typing

...

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

或者只是将 # type: ignore 附加到行尾以禁用对该特定行的类型检查。