Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ def merge(
return _secrets_masker().merge(new_value, old_value, name, max_depth)


_global_secrets_masker: SecretsMasker | None = None


@cache
def _secrets_masker() -> SecretsMasker:
"""
Get or create the module-level secrets masker instance.
Expand All @@ -139,10 +137,7 @@ def _secrets_masker() -> SecretsMasker:
airflow.sdk._shared) will have separate global variables and thus separate
masker instances.
"""
global _global_secrets_masker
if _global_secrets_masker is None:
_global_secrets_masker = SecretsMasker()
return _global_secrets_masker
return SecretsMasker()


def reset_secrets_masker() -> None:
Expand Down
24 changes: 13 additions & 11 deletions shared/timezones/src/airflow_shared/timezones/timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
utc = pendulum.UTC


class _Timezone:
"""Keep track of current timezone w/o global variable."""

initialized_timezone: FixedTimezone | Timezone = utc


def is_localized(value: dt.datetime) -> bool:
"""
Determine if a given datetime.datetime is aware.
Expand Down Expand Up @@ -87,7 +93,7 @@ def convert_to_utc(value: dt.datetime | None) -> DateTime | None:
return value

if not is_localized(value):
value = pendulum.instance(value, TIMEZONE)
value = pendulum.instance(value, _Timezone.initialized_timezone)

return pendulum.instance(value.astimezone(utc))

Expand All @@ -113,7 +119,7 @@ def make_aware(value: dt.datetime | None, timezone: dt.tzinfo | None = None) ->
:return: localized datetime in settings.TIMEZONE or timezone
"""
if timezone is None:
timezone = TIMEZONE
timezone = _Timezone.initialized_timezone

if not value:
return None
Expand Down Expand Up @@ -146,7 +152,7 @@ def make_naive(value, timezone=None):
:return: naive datetime
"""
if timezone is None:
timezone = TIMEZONE
timezone = _Timezone.initialized_timezone

# Emulate the behavior of astimezone() on Python < 3.6.
if is_naive(value):
Expand All @@ -169,7 +175,7 @@ def datetime(*args, **kwargs):
:return: datetime.datetime
"""
if "tzinfo" not in kwargs:
kwargs["tzinfo"] = TIMEZONE
kwargs["tzinfo"] = _Timezone.initialized_timezone

return dt.datetime(*args, **kwargs)

Expand All @@ -182,7 +188,7 @@ 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
"""
return pendulum.parse(string, tz=timezone or TIMEZONE, strict=strict) # type: ignore
return pendulum.parse(string, tz=timezone or _Timezone.initialized_timezone, strict=strict) # type: ignore


@overload
Expand Down Expand Up @@ -279,20 +285,16 @@ 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()
_Timezone.initialized_timezone = local_timezone()
else:
TIMEZONE = parse_timezone(default_timezone)
_Timezone.initialized_timezone = parse_timezone(default_timezone)


def from_timestamp(timestamp: int | float, tz: str | FixedTimezone | Timezone = utc) -> DateTime:
Expand Down