-
Notifications
You must be signed in to change notification settings - Fork 15.3k
Re-create PR for Enable Serde for Pydantic BaseModel and Subclasses #52360
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
base: main
Are you sure you want to change the base?
Conversation
I applied "full tests needed" and resolved conflict with Python 3.9 removal to trigger the build |
@@ -92,7 +92,7 @@ def deserialize(cls: type, version: int, data: dict | str) -> datetime.date | da | |||
if cls is DateTime and isinstance(data, dict): | |||
return DateTime.fromtimestamp(float(data[TIMESTAMP]), tz=tz) | |||
|
|||
if cls is datetime.timedelta and isinstance(data, (str, float)): | |||
if classname == qualname(datetime.timedelta) and isinstance(data, str | float): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to make an update to this. I think if cls is datetime.timedelta and isinstance(data, (str, float)):
should overwrite this line. the use of classname
is before refactoring, and the breeze test script raised an error for str | float
. Will push a commit soon.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isintance(data, str | float)
seems like an invalid syntax (correct me if I am wrong), the second argument of isinstance accept a class or tuple. The change is introduced #52072.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I think I am wrong, this is actually valid > 3.9, I ran on 3.9 and get the error.
5164de8
to
8d4d944
Compare
8d4d944
to
c79af1f
Compare
REbased again - we had some microsoft kiota new version released that broke main |
if not is_pydantic_model(o): | ||
return "", "", 0, False | ||
|
||
model = cast("BaseModel", o) # for mypy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to see if I it is a good practice to replace this line, and another one in the deserialize with # type: ignore
. This line is merely to convince mypy that the model
is actually a pydantic model that has the model_dump
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this cast
and use # type: ignore
resolve conflicts in test case
need to overwrite if classname == qualname(datetime.timedelta) and isinstance(data, str | float) since the second argument of isinstance accept a class or tuple
c79af1f
to
85f95a7
Compare
Hi @bolkedebruin , @potiuk , @amoghrajesh , I would appreciate if I could get your help again to review this PR. I also wanted to share some findings while doing testing. Issue with serializing
|
The chage in #51059 broke canary tests in https://github.com/apache/airflow/actions/runs/15910813471/job/44877861492. The PR is reverted through #52312.
Create this PR to fix the issue.
The issue in #51059
First, the refactored
deserialize
take the actual class instead of classname. Therefore, the following test case is modified from"zi = deserialize("backports.zoneinfo.ZoneInfo", 1, "Asia/Taipei")"
tozi = deserialize(ZoneInfo, 1, "Asia/Taipei")
. I think the "backports.zoneinfo.ZoneInfo" is a backport of the standard library modulezoneinfo
in Python 3.9, to suppor lower version of Python.In the
airflow-core/src/airflow/serialization/serializers/timezone.py
, the line below is looking for the backport ZoneInfo, instead of the standard libraryzoneinfo
. Therefore, when aZoneInfo
class is passed into thedeserialize
, thequalname(cls)
is resolved to "zoneinfo.ZoneInfo" instead of "backports.zoneinfo.ZoneInfo". Therefore, the data will be deserialized throughparse_timezone(data)
, resulting in a different object. Therefore the test case failed.Solution
To resolve this issue, I updated the if condition to
if cls is ZoneInfo and isinstance(data, str):
. I think the minimum version of Python we support is 3.9. So, "backports.zoneinfo.ZoneInfo" should probably be removed.After making the changes, I ran the following tests and all checks passed.
breeze --python 3.9 testing core-tests --test-type Serializationbreeze --python 3.9 testing core-tests --test-type Serialization --downgrade-pendulumbreeze --python 3.10 testing core-tests --test-type Serialization
breeze --python 3.10 testing core-tests --test-type Serialization --downgrade-pendulum
breeze --python 3.11 testing core-tests --test-type Serialization
breeze --python 3.11 testing core-tests --test-type Serialization --downgrade-pendulum
but I got some container issues when running for 3.12. Will look into the full test results and action accordingly.
^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named
{pr_number}.significant.rst
or{issue_number}.significant.rst
, in airflow-core/newsfragments.