PrefectHQ / prefect

Prefect is a workflow orchestration framework for building resilient data pipelines in Python.
https://prefect.io
Apache License 2.0
16.67k stars 1.61k forks source link

flow.to_deployment() length check issue when schedule is added #15403

Closed elyanah-aco closed 1 month ago

elyanah-aco commented 1 month ago

Bug summary

Adding schedules to flow.to_deployment() raises an error because code is checking for schedule object length, although they have none. Example is for CronSchedule but issue also occurs with IntervalSchedule (and I assume RRuleSchedule, though not tested)

from prefect import flow, serve
from prefect.client.schemas.schedules import CronSchedule

@flow 
def sample_flow(input: str) -> str:
    print(f"Input is: {input}")

if __name__ == "__main__":
    default_deploy = sample_flow.to_deployment(
        name="sample_deployment",
        schedules=CronSchedule(
            cron="0 19 * * 1-5",
            timezone="US/Eastern",
            day_or=True),
        parameters={"input": "hello world!"},
    )
    serve(default_deploy)

Error message:

File "C:\Users\USER\Desktop\local-folder\venv\Lib\site-packages\prefect\deployments\runner.py", line 230, in reconcile_schedules
    return reconcile_schedules_runner(values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\USER\Desktop\local-folder\venv\Lib\site-packages\prefect\_internal\schemas\validators.py", line 226, in reconcile_schedules_runner
    if schedules is not None and len(schedules) > 0:
                                 ^^^^^^^^^^^^^^
TypeError: object of type 'CronSchedule' has no len()

Version info (prefect version output)

Version:             3.0.2
API version:         0.8.4
Python version:      3.11.0
Git commit:          c846de02
Built:               Fri, Sep 13, 2024 10:48 AM
OS/Arch:             win32/AMD64
Profile:             default
Server type:         cloud
Pydantic version:    2.9.1

Additional context

No response

desertaxle commented 1 month ago

Hey @elyanah-aco! The schedules kwarg expects a list of schedules. If you wrap your CronSchedule in a list, your code should work.

elyanah-aco commented 1 month ago

Thanks @desertaxle, that worked! Closing this now