This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 28e08fc9970 Make shared timezone library standalone by getting rid of 
`airflow.*` imports (#58888)
28e08fc9970 is described below

commit 28e08fc99703a107f7c641af99a3715d6938f4e1
Author: Amogh Desai <[email protected]>
AuthorDate: Wed Dec 3 04:07:26 2025 +0530

    Make shared timezone library standalone by getting rid of `airflow.*` 
imports (#58888)
    
    * Make shared timezone library standalone by getting rid of airflow.* 
imports
    
    * absolutely not
    
    * fix unit test
---
 airflow-core/src/airflow/settings.py               | 14 +++++++++---
 .../tests/unit/cli/commands/test_db_command.py     |  2 +-
 .../src/airflow_shared/timezones/timezone.py       | 26 +++++++++++++---------
 task-sdk/src/airflow/sdk/timezone.py               |  9 ++++++++
 4 files changed, 37 insertions(+), 14 deletions(-)

diff --git a/airflow-core/src/airflow/settings.py 
b/airflow-core/src/airflow/settings.py
index 980de71c1a5..0f84d450738 100644
--- a/airflow-core/src/airflow/settings.py
+++ b/airflow-core/src/airflow/settings.py
@@ -46,7 +46,12 @@ except ImportError:
 from sqlalchemy.pool import NullPool
 
 from airflow import __version__ as airflow_version, policies
-from airflow._shared.timezones.timezone import local_timezone, parse_timezone, 
utc
+from airflow._shared.timezones.timezone import (
+    initialize as initialize_timezone,
+    local_timezone,
+    parse_timezone,
+    utc,
+)
 from airflow.configuration import AIRFLOW_HOME, conf
 from airflow.exceptions import AirflowInternalRuntimeError
 from airflow.logging_config import configure_logging
@@ -71,12 +76,15 @@ if TYPE_CHECKING:
 log = logging.getLogger(__name__)
 
 try:
-    if (tz := conf.get_mandatory_value("core", "default_timezone")) != 
"system":
-        TIMEZONE = parse_timezone(tz)
+    tz_str = conf.get_mandatory_value("core", "default_timezone")
+    initialize_timezone(tz_str)
+    if tz_str != "system":
+        TIMEZONE = parse_timezone(tz_str)
     else:
         TIMEZONE = local_timezone()
 except Exception:
     TIMEZONE = utc
+    initialize_timezone("UTC")
 
 log.info("Configured default timezone %s", TIMEZONE)
 
diff --git a/airflow-core/tests/unit/cli/commands/test_db_command.py 
b/airflow-core/tests/unit/cli/commands/test_db_command.py
index cbb9a1d269f..1e56647f042 100644
--- a/airflow-core/tests/unit/cli/commands/test_db_command.py
+++ b/airflow-core/tests/unit/cli/commands/test_db_command.py
@@ -694,7 +694,7 @@ class TestCLIDBClean:
         coerced to tz-aware with default timezone
         """
         timestamp = "2021-01-01 00:00:00"
-        with patch("airflow.settings.TIMEZONE", pendulum.timezone(timezone)):
+        with patch("airflow._shared.timezones.timezone.TIMEZONE", 
pendulum.timezone(timezone)):
             args = self.parser.parse_args(["db", "clean", 
"--clean-before-timestamp", f"{timestamp}", "-y"])
             db_command.cleanup_tables(args)
         run_cleanup_mock.assert_called_once_with(
diff --git a/shared/timezones/src/airflow_shared/timezones/timezone.py 
b/shared/timezones/src/airflow_shared/timezones/timezone.py
index 3abdd06ee5e..9621c3856a1 100644
--- a/shared/timezones/src/airflow_shared/timezones/timezone.py
+++ b/shared/timezones/src/airflow_shared/timezones/timezone.py
@@ -87,8 +87,6 @@ def convert_to_utc(value: dt.datetime | None) -> DateTime | 
None:
         return value
 
     if not is_localized(value):
-        from airflow.settings import TIMEZONE
-
         value = pendulum.instance(value, TIMEZONE)
 
     return pendulum.instance(value.astimezone(utc))
@@ -115,8 +113,6 @@ def make_aware(value: dt.datetime | None, timezone: 
dt.tzinfo | None = None) ->
     :return: localized datetime in settings.TIMEZONE or timezone
     """
     if timezone is None:
-        from airflow.settings import TIMEZONE
-
         timezone = TIMEZONE
 
     if not value:
@@ -150,8 +146,6 @@ def make_naive(value, timezone=None):
     :return: naive datetime
     """
     if timezone is None:
-        from airflow.settings import TIMEZONE
-
         timezone = TIMEZONE
 
     # Emulate the behavior of astimezone() on Python < 3.6.
@@ -175,8 +169,6 @@ def datetime(*args, **kwargs):
     :return: datetime.datetime
     """
     if "tzinfo" not in kwargs:
-        from airflow.settings import TIMEZONE
-
         kwargs["tzinfo"] = TIMEZONE
 
     return dt.datetime(*args, **kwargs)
@@ -190,8 +182,6 @@ def parse(string: str, timezone=None, *, strict=False) -> 
DateTime:
     :param timezone: the timezone
     :param strict: if False, it will fall back on the dateutil parser if 
unable to parse with pendulum
     """
-    from airflow.settings import TIMEZONE
-
     return pendulum.parse(string, tz=timezone or TIMEZONE, strict=strict)  # 
type: ignore
 
 
@@ -289,6 +279,22 @@ def local_timezone() -> FixedTimezone | Timezone:
     return pendulum.tz.local_timezone()
 
 
+TIMEZONE: FixedTimezone | Timezone = utc
+
+
+def initialize(default_timezone: str) -> None:
+    """
+    Initialize the default timezone for the timezone library.
+
+    Automatically called by airflow-core and task-sdk during their 
initialization.
+    """
+    global TIMEZONE
+    if default_timezone == "system":
+        TIMEZONE = local_timezone()
+    else:
+        TIMEZONE = parse_timezone(default_timezone)
+
+
 def from_timestamp(timestamp: int | float, tz: str | FixedTimezone | Timezone 
= utc) -> DateTime:
     """
     Parse timestamp and return DateTime in a given time zone.
diff --git a/task-sdk/src/airflow/sdk/timezone.py 
b/task-sdk/src/airflow/sdk/timezone.py
index 1769f510c58..15feec1d026 100644
--- a/task-sdk/src/airflow/sdk/timezone.py
+++ b/task-sdk/src/airflow/sdk/timezone.py
@@ -22,12 +22,21 @@ from airflow.sdk._shared.timezones.timezone import (
     coerce_datetime,
     convert_to_utc,
     datetime,
+    initialize,
     make_naive,
     parse,
     utc,
     utcnow,
 )
 
+try:
+    from airflow.sdk.configuration import conf
+
+    tz_str = conf.get_mandatory_value("core", "default_timezone")
+    initialize(tz_str)
+except Exception:
+    initialize("UTC")
+
 __all__ = [
     "coerce_datetime",
     "convert_to_utc",

Reply via email to