Skip to content

❗ 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

@russbiggs

Description

@russbiggs

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions