[Schedule] Update type hints for ScheduleCronTrigger to avoid unexpected pydantic behaviour #3286
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When defining type hints for
pydantic
objects withtyping.Union
,pydantic
will attempt to match any of the types defined undertyping.Union
and will use the first one that matches.However, when adding
typing.Optional
(e.g.Optional[Union[datetime, str]]
) the user may face unexpected behaviour in which the order within theUnion
has been changed andpydantic
will match the wrong type (e.g. trying to matchstr
beforedatetime
althoughdatetime
is the first type in theUnion
list). This bug occurs following different imports of 3rd libraries. More info about thispydantic
behaviour can be found here: pydantic/pydantic#4474pydantic/pydantic#4519
To avoid this unexpected behaviour, in this PR we replace
typing.Optional
with defaultNone
value for the fieldsstart_date
andend_date
withinScheduleCronTrigger
object.Pleas note that new tests are not required as this behaviour is already being tested through
test_update_schedule()
.A related JIRA ticket - https://jira.iguazeng.com/browse/ML-3624