uranusjr commented on a change in pull request #16631:
URL: https://github.com/apache/airflow/pull/16631#discussion_r658010775
##########
File path: airflow/serialization/serialized_objects.py
##########
@@ -195,6 +196,19 @@ def serialize_to_json(
serialized_object[key] = value
return serialized_object
+ @classmethod
+ def serialize_timezone(cls, var: datetime.tzinfo) -> Any:
+ """Serializes a timezone to json"""
+ tz_name = getattr(var, 'name', None)
+ if not tz_name:
+ try:
+ tz_name = var.tzname(datetime.datetime.utcnow())
+ except InvalidTimezone:
+ raise TypeError(f"the timezone {var} can't be serialized")
+
+
+ return cls._encode(str(tz_name), type_=DAT.TIMEZONE)
Review comment:
`tzname()` can return `None` for naive datetimes, so we can’t just cast
it to str. We either need to raise or do something about it.
##########
File path: airflow/serialization/serialized_objects.py
##########
@@ -195,6 +196,19 @@ def serialize_to_json(
serialized_object[key] = value
return serialized_object
+ @classmethod
+ def serialize_timezone(cls, var: datetime.tzinfo) -> Any:
+ """Serializes a timezone to json"""
+ tz_name = getattr(var, 'name', None)
+ if not tz_name:
+ try:
+ tz_name = var.tzname(datetime.datetime.utcnow())
+ except InvalidTimezone:
+ raise TypeError(f"the timezone {var} can't be serialized")
Review comment:
I believe we need to also catch `NotImplementedError` since it’s the
default implementation in `datetime.zoneinfo`.
##########
File path: airflow/serialization/serialized_objects.py
##########
@@ -195,6 +196,19 @@ def serialize_to_json(
serialized_object[key] = value
return serialized_object
+ @classmethod
+ def serialize_timezone(cls, var: datetime.tzinfo) -> Any:
+ """Serializes a timezone to json"""
+ tz_name = getattr(var, 'name', None)
+ if not tz_name:
Review comment:
Can we also use `tzname` for pendulum Timezone as well? It’s a standard
method that should always yield something useful.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]