core: migrate code to benefit from 3.9 stuff (#401)

for now keeping ruff on 3.8 target version, need to sort out modules as well
This commit is contained in:
karlicoss 2024-10-19 20:55:09 +01:00 committed by GitHub
parent bc7c3ac253
commit d3f9a8e8b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
43 changed files with 515 additions and 404 deletions

View file

@ -1,5 +1,7 @@
from functools import lru_cache
from typing import Dict, Sequence
from __future__ import annotations
from collections.abc import Sequence
from functools import cache, lru_cache
import pytz
@ -11,6 +13,7 @@ def user_forced() -> Sequence[str]:
# https://stackoverflow.com/questions/36067621/python-all-possible-timezone-abbreviations-for-given-timezone-name-and-vise-ve
try:
from my.config import time as user_config
return user_config.tz.force_abbreviations # type: ignore[attr-defined] # noqa: TRY300
# note: noqa since we're catching case where config doesn't have attribute here as well
except:
@ -19,15 +22,15 @@ def user_forced() -> Sequence[str]:
@lru_cache(1)
def _abbr_to_timezone_map() -> Dict[str, pytz.BaseTzInfo]:
def _abbr_to_timezone_map() -> dict[str, pytz.BaseTzInfo]:
# also force UTC to always correspond to utc
# this makes more sense than Zulu it ends up by default
timezones = [*pytz.all_timezones, 'UTC', *user_forced()]
res: Dict[str, pytz.BaseTzInfo] = {}
res: dict[str, pytz.BaseTzInfo] = {}
for tzname in timezones:
tz = pytz.timezone(tzname)
infos = getattr(tz, '_tzinfos', []) # not sure if can rely on attr always present?
infos = getattr(tz, '_tzinfos', []) # not sure if can rely on attr always present?
for info in infos:
abbr = info[-1]
# todo could support this with a better error handling strategy?
@ -43,7 +46,7 @@ def _abbr_to_timezone_map() -> Dict[str, pytz.BaseTzInfo]:
return res
@lru_cache(maxsize=None)
@cache
def abbr_to_timezone(abbr: str) -> pytz.BaseTzInfo:
return _abbr_to_timezone_map()[abbr]