Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❗ NOTICE: Python users using Schedule class in aws_events, aws_applicationautoscaling, aws_autoscaling, aws_backup, get synth-time "TypeError: ...got aws_cdk._ScheduleProxy instead" in CDK 2.97.0 #27259

Closed
russbiggs opened this issue Sep 22, 2023 · 13 comments · Fixed by #27284
Labels
@aws-cdk/aws-applicationautoscaling Related to AWS Application Auto Scaling @aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling @aws-cdk/aws-events Related to CloudWatch Events @aws-cdk/aws-synthetics Related to Amazon CloudWatch Synthetics bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0

Comments

@russbiggs
Copy link

russbiggs commented Sep 22, 2023

Please add your +1 👍 to let us know you have encountered this

Status: IN-PROGRESS

Overview:

In CDK 2.97.0, we released #27105: "feat(core): Schedule class". This broke python users of the 4 stable modules that were updated:

  • aws_events
  • aws_applicationautoscaling
  • aws_autoscaling
  • aws_backup

As well as two experimental modules:

  • aws_scheduler_alpha
  • aws_synthetics_alpha

When passing in a schedule to any of the above modules in python, cron, expression, rate, at all return aws_cdk._ScheduleProxy type instead of the module specific schedule type.

This was allowed because TypeScript does structural typing and we were relying on this to cast core.Schedule as module.Schedule. jsii did not throw an error for this kind of type casting so it was allowed into v2.97.0.

Complete Error Message:

TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got
aws_cdk._ScheduleProxy instead

Workaround:

Downgrade to CDK v2.96.2
Upgrade to 2.97.1 or later

Solution:

We will revert the offending PR first. There will be a following PR to reimplement this and another PR in jsii to ensure that structural typing is caught by jsii errors.

Related Issues:


Original bug report reproduced here:

Describe the bug

When creating a aws_events Rule creation fails when passing aws_cdk.aws_events.Schedule. The cron, expression, rate methods all seem to return a aws_cdk._ScheduleProxy type not the documented and required aws_cdk.Schedule type:

TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got aws_cdk._ScheduleProxy instead

Expected Behavior

calling aws_events.Schedule methods should return a aws_events.Schedule type as documented in the CDK docs and in type annotation.

Current Behavior

aws_events.Schedule method return a aws_cdk._ScheduleProxy type. When attempting to use the Schedule constructor for a Schedule type as in the example in the docs:

https://docs.aws.amazon.com/cdk/api/v2/python/aws_cdk.aws_events/Rule.html:

schedule (Optional[Schedule]) – The schedule or rate (frequency) that determines when EventBridge runs the rule. You must specify this property, the eventPattern property, or both. For more information, see Schedule Expression Syntax for Rules in the Amazon EventBridge User Guide. Default: - None.

e.g.

rule = events.Rule(self, "Rule",
    schedule=events.Schedule.rate(cdk.Duration.hours(1))
)

This fails with the following error:

TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got aws_cdk._ScheduleProxy instead

Reproduction Steps

Using the documented example will reproduce the error:

rule = events.Rule(self, "Rule",
    schedule=events.Schedule.rate(cdk.Duration.hours(1))
)

using events.Schedule.expression(...) and events.Schedule.cron(...) produce the same problem.

Outside of a CDK application checking the return type in plain Python also shows that the method incorrectly return a _ScheduleProxy object

from aws_cdk import aws_events

print(type(aws_events.Schedule.rate(Duration.hours(1))))

# returns <class 'aws_cdk._ScheduleProxy'>

Possible Solution

No response

Additional Information/Context

using aws-cdk-lib==2.97.0 in python 3.11.4

The type annotation in the source documents the intended return type as Schedule as well

e.g.:

    @jsii.member(jsii_name="cron")
    @builtins.classmethod
    def cron(
        cls,
        *,
        day: typing.Optional[builtins.str] = None,
        hour: typing.Optional[builtins.str] = None,
        minute: typing.Optional[builtins.str] = None,
        month: typing.Optional[builtins.str] = None,
        week_day: typing.Optional[builtins.str] = None,
        year: typing.Optional[builtins.str] = None,
    ) -> "Schedule":

CDK CLI Version

2.97.0

Framework Version

No response

Node.js Version

v18.15.0

OS

Mac OS 13.1 (22C65)

Language

Python

Language Version

3.11.4

Other information

No response

@russbiggs russbiggs added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 22, 2023
@github-actions github-actions bot added the @aws-cdk/aws-events Related to CloudWatch Events label Sep 22, 2023
@danielwilton-metoffice
Copy link

danielwilton-metoffice commented Sep 22, 2023

I believe this is because the new core.Schedule functionality is actually returning a core.LiteralSchedule that extends the core.Schedule class, which has protected members and isn't the matching consumer aws_events.Schedule with public members, causing issues when compiled with JSII.

@russbiggs
Copy link
Author

Can confirm this is a new issue in 2.97.0 with the changes around core.Schedule. downgrading to 2.96.0 resolves the issue.

@fucksmile
Copy link

Can confirm this is a new issue in 2.97.0 with the changes around core.Schedule. downgrading to 2.96.0 resolves the issue.

Hello,
New issue when downgrading:
This CDK CLI is not compatible with the CDK library used by your application. Please upgrade the CLI to the latest version.
(Cloud assembly schema version mismatch: Maximum schema version supported is 32.0.0, but found 34.0.0)

@russbiggs
Copy link
Author

Can confirm this is a new issue in 2.97.0 with the changes around core.Schedule. downgrading to 2.96.0 resolves the issue.

Hello, New issue when downgrading: This CDK CLI is not compatible with the CDK library used by your application. Please upgrade the CLI to the latest version. (Cloud assembly schema version mismatch: Maximum schema version supported is 32.0.0, but found 34.0.0)

This isnt an issue with CDK instead an issue with your environment. When downgrading you need to update your CLI version to match the correct schema, as indicated in the error message you posted.

@fucksmile
Copy link

fucksmile commented Sep 23, 2023

as indicated in the error message you posted.

downgrading to 2.96.0 resolves the issues/27259
upgrade the CLI to the latest version just as issues/27259:TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got aws_cdk._ScheduleProxy instead.
Sorry, my problem is I don't know how to downgrade CDK library to match CDK CLI 2.96.0
Any help?

@danielwilton-metoffice
Copy link

as indicated in the error message you posted.

downgrading to 2.96.0 resolves the issues/27259 upgrade the CLI to the latest version just as issues/27259:TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got aws_cdk._ScheduleProxy instead. Sorry, my problem is I don't know how to downgrade CDK library to match CDK CLI 2.96.0 Any help?

This is an issue with the CDK version you are using with your application. Not the CDK CLI version.

@fucksmile
Copy link

This is an issue with the CDK version you are using with your application. Not the CDK CLI version.

Sorry, my problem is I don't know how to downgrade CDK library to match CDK CLI 2.96.0 Any help?
Where is CDK library, How to install CDK library? This is the first time I have used CDK
I installed CDK CLI 2.96.0 run with npm install -g aws-cdk@2.96.0
I ran with python3 -m pip install aws-cdk-lib==2.96.0 not work

@russbiggs
Copy link
Author

This is an issue with the CDK version you are using with your application. Not the CDK CLI version.

Sorry, my problem is I don't know how to downgrade CDK library to match CDK CLI 2.96.0 Any help?

Where is CDK library, How to install CDK library? This is the first time I have used CDK

I installed CDK CLI 2.96.0 run with npm install -g aws-cdk@2.96.0

I ran with python3 -m pip install aws-cdk-lib==2.96.0 not work

This isn't really the right forum for support on how to use CDK and the CDK CLI since this is a bug report. I recommend checking out the CDK docs https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html for a guide on how to use CDK.

@pwrmiller
Copy link

pwrmiller commented Sep 25, 2023

As a note, this also impacts applicationautoscaling.Schedule; (aws-cdk-lib@2.97.0, cdk 2.94.0)

        if isinstance(resp, _OkayResponse):
            return self._serializer.structure(resp.ok, response_type)
        elif isinstance(resp, _CallbackResponse):
            return resp.callback
        else:
            if resp.name == ErrorType.JSII_FAULT.value:
                raise JSIIError(resp.error) from JavaScriptError(resp.stack)
>           raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
E           RuntimeError: type of argument schedule must be aws_cdk.aws_applicationautoscaling.Schedule; got aws_cdk._ScheduleProxy instead

Of course, downgrading to CDK 2.96.2 works around the issue for now. In pyproject.toml, one can use:

aws-cdk-lib = "2.96.2"

@riskersen
Copy link

Possible workaround, pinning cdk lib version in requirements.txt to 2.96.0

aws-cdk-lib==2.96.0

@danielwilton-metoffice
Copy link

As a note, this also impacts applicationautoscaling.Schedule; (aws-cdk-lib@2.97.0, cdk 2.94.0)

        if isinstance(resp, _OkayResponse):
            return self._serializer.structure(resp.ok, response_type)
        elif isinstance(resp, _CallbackResponse):
            return resp.callback
        else:
            if resp.name == ErrorType.JSII_FAULT.value:
                raise JSIIError(resp.error) from JavaScriptError(resp.stack)
>           raise RuntimeError(resp.error) from JavaScriptError(resp.stack)
E           RuntimeError: type of argument schedule must be aws_cdk.aws_applicationautoscaling.Schedule; got aws_cdk._ScheduleProxy instead

Yeah, this is due to the refactor that moved all Schedule functionality to core.Schedule.

Linked issue:
#27258

@peterwoodworth peterwoodworth added p0 and removed needs-triage This issue or PR still needs to be triaged. labels Sep 25, 2023
@peterwoodworth
Copy link
Contributor

We're working on it, thanks for the reports 👍🏻

@kaizencc kaizencc changed the title aws_events: aws_events.Schedule methods cron, expression, rate return aws_cdk._ScheduleProxy ❗ NOTICE: Python users using aws_events.Schedule in 2.97.0 get "TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got aws_cdk._ScheduleProxy instead" Sep 25, 2023
@kaizencc kaizencc changed the title ❗ NOTICE: Python users using aws_events.Schedule in 2.97.0 get "TypeError: type of argument schedule must be one of (aws_cdk.aws_events.Schedule, NoneType); got aws_cdk._ScheduleProxy instead" ❗ NOTICE: Python users using aws_events.Schedule get synth-time "TypeError: ...got aws_cdk._ScheduleProxy instead" in CDK 2.97.0 Sep 25, 2023
@kaizencc kaizencc changed the title ❗ NOTICE: Python users using aws_events.Schedule get synth-time "TypeError: ...got aws_cdk._ScheduleProxy instead" in CDK 2.97.0 ❗ NOTICE: Python users using Schedule class in aws_events, aws_applicationautoscaling, aws_autoscaling, aws_backup, get synth-time "TypeError: ...got aws_cdk._ScheduleProxy instead" in CDK 2.97.0 Sep 25, 2023
@kaizencc kaizencc added @aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling @aws-cdk/aws-applicationautoscaling Related to AWS Application Auto Scaling @aws-cdk/aws-synthetics Related to Amazon CloudWatch Synthetics labels Sep 25, 2023
@kaizencc kaizencc pinned this issue Sep 25, 2023
@kaizencc kaizencc added the management/tracking Issues that track a subject or multiple issues label Sep 25, 2023
@mergify mergify bot closed this as completed in #27284 Sep 25, 2023
mergify bot pushed a commit that referenced this issue Sep 25, 2023
Reverts #27105

This fixes #27259 by reverting the offending commit.
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

kaizencc added a commit that referenced this issue Sep 25, 2023
Reverts #27105

This fixes #27259 by reverting the offending commit.
mergify bot pushed a commit to cdklabs/aws-cdk-notices that referenced this issue Sep 26, 2023
We don't have a way of targeting specific languages, so this is going to blast all users not just python ones.
@MrArnoldPalmer MrArnoldPalmer unpinned this issue Oct 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-applicationautoscaling Related to AWS Application Auto Scaling @aws-cdk/aws-autoscaling Related to Amazon EC2 Auto Scaling @aws-cdk/aws-events Related to CloudWatch Events @aws-cdk/aws-synthetics Related to Amazon CloudWatch Synthetics bug This issue is a bug. management/tracking Issues that track a subject or multiple issues p0
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants