Aws-cdk: [monocdk-experiment] Python - import incorrect

Created on 14 Jul 2020  路  4Comments  路  Source: aws/aws-cdk

When using monocdk-experiment package in Python, stack cannot be created because some package are not imported.

Reproduction Steps

Install dependencies

pip install monocdk.experiment

app.py

#!/usr/bin/env python3
from monocdk_experiment import App, Stack, aws_codebuild

app = App()

class TestStack(Stack):
    def __init__(self, scope, id, **kwargs):
        super().__init__(scope, id, **kwargs)

TestStack(app, "test-stack")

Synthesize stacks

cdk synth

Error Log

Traceback (most recent call last):
  File "app.py", line 2, in <module>
    from monocdk_experiment import App, Stack, aws_codebuild
  File "/Users/zangror/.venv/lib/python3.8/site-packages/monocdk_experiment/aws_codebuild/__init__.py", line 34, in <module>
    from ..aws_ec2 import (
  File "/Users/zangror/.venv/lib/python3.8/site-packages/monocdk_experiment/aws_ec2/__init__.py", line 46, in <module>
    from ..aws_logs import ILogGroup as _ILogGroup_6b54c8e1
  File "/Users/zangror/.venv/lib/python3.8/site-packages/monocdk_experiment/aws_logs/__init__.py", line 2613, in <module>
    class MetricFilter(
  File "/Users/zangror/.venv/lib/python3.8/site-packages/monocdk_experiment/aws_logs/__init__.py", line 2668, in MetricFilter
    period: typing.Optional[_Duration_5170c158] = None,
NameError: name '_Duration_5170c158' is not defined

Environment

  • CLI Version: 1.51.0 (build 8c2d53c)
  • Framework Version: 1.51.0
  • Node.js Version: 12.6
  • OS: Mac OS Catalina 10.15.5
  • Language (Version): Python (3.8.2)

Other

How to fix the error ?

If I open the file /Users/zangror/.venv/lib/python3.8/site-packages/monocdk_experiment/aws_logs/__init__.py this is the content:

import abc
import builtins
import datetime
import enum
import typing

import jsii
import jsii.compat
import publication

from .._jsii import *

from .. import (
    CfnResource as _CfnResource_7760e8e4,
    Construct as _Construct_f50a3f53,
    FromCloudFormationOptions as _FromCloudFormationOptions_5f49f6f1,
    ICfnFinder as _ICfnFinder_3b168f30,
    TreeInspector as _TreeInspector_154f5999,
    IInspectable as _IInspectable_051e6ed8,
    IResolvable as _IResolvable_9ceae33e,
    Resource as _Resource_884d0774,
    IResource as _IResource_72f7ee7e,
    RemovalPolicy as _RemovalPolicy_5986e9f3,
)
from ..aws_cloudwatch import (
    Metric as _Metric_53e89548,
    MetricOptions as _MetricOptions_ad2c4d5d,
)
from ..aws_iam import (
    IRole as _IRole_e69bbae4,
    PolicyStatement as _PolicyStatement_f75dc775,
    PolicyDocument as _PolicyDocument_1d1bca11,
    Grant as _Grant_96af6d2d,
    IGrantable as _IGrantable_0fcfc53a,
)

If I add manually this specific dependency, this import error disappears

import abc
import builtins
import datetime
import enum
import typing

import jsii
import jsii.compat
import publication

from .._jsii import *

from .. import (
    CfnResource as _CfnResource_7760e8e4,
    Construct as _Construct_f50a3f53,
    FromCloudFormationOptions as _FromCloudFormationOptions_5f49f6f1,
    ICfnFinder as _ICfnFinder_3b168f30,
    TreeInspector as _TreeInspector_154f5999,
    IInspectable as _IInspectable_051e6ed8,
    IResolvable as _IResolvable_9ceae33e,
    Resource as _Resource_884d0774,
    IResource as _IResource_72f7ee7e,
    RemovalPolicy as _RemovalPolicy_5986e9f3,
    Duration as _Duration_5170c158,  # <-- Here
)
from ..aws_cloudwatch import (
    Metric as _Metric_53e89548,
    MetricOptions as _MetricOptions_ad2c4d5d,
)
from ..aws_iam import (
    IRole as _IRole_e69bbae4,
    PolicyStatement as _PolicyStatement_f75dc775,
    PolicyDocument as _PolicyDocument_1d1bca11,
    Grant as _Grant_96af6d2d,
    IGrantable as _IGrantable_0fcfc53a,
)

It seems that in all the packages, core.Duration, aws_cloudwatch.Unit are not imported correctly, which is really strange because the others imports are well resolved.


This is :bug: Bug Report

bug efforsmall in-progress languagpython p1 packagmonocdk-experiment

All 4 comments

@SomayaB monocdk is @RomainMuller

This definitely looks like a code generation bug in MonoCDK. I wonder how it didn't manifest itself in my tests of MonoCDK in Python... Anyhow - I'm looking into this now.

Found the root cause of issue being that some of the kwargs lifted here are coming from a super-interface of the props struct, and the code generator did not collect import requirements for parent interfaces in this case.

I'll create an issue on aws/jsii to track this.

Submitted a PR with a fix in jsii.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ababra picture ababra  路  3Comments

artyom-melnikov picture artyom-melnikov  路  3Comments

peterdeme picture peterdeme  路  3Comments

eladb picture eladb  路  3Comments

schof picture schof  路  3Comments