From eae0e1a61426abcaaa8e19d00b199be992bfbd4e Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 22 May 2022 15:21:08 +0100 Subject: [PATCH] my.time.tz.via_location: provide default (empty) config if user doesn't have time config defined --- my/config.py | 3 +++ my/time/tz/via_location.py | 24 +++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/my/config.py b/my/config.py index b841d31..b1c17d2 100644 --- a/my/config.py +++ b/my/config.py @@ -80,8 +80,11 @@ class location: accuracy: float +from my.core.compat import Literal class time: class tz: + policy: Literal['keep', 'convert', 'throw'] + class via_location: fast: bool sort_locations: bool diff --git a/my/time/tz/via_location.py b/my/time/tz/via_location.py index d31f04b..6b8e835 100644 --- a/my/time/tz/via_location.py +++ b/my/time/tz/via_location.py @@ -7,12 +7,30 @@ REQUIRES = [ ] -from my.config import time +## user might not have tz config section, so makes sense to be more defensive about it +# todo might be useful to extract a helper for this +try: + from my.config import time +except ImportError as ie: + if ie.name != 'time': + raise ie +else: + try: + user_config = time.tz.via_location + except AttributeError as ae: + if not ("'tz'" in str(ae) or "'via_location'"): + raise ae + +# deliberately dynamic to prevent confusing mypy +if 'user_config' not in globals(): + globals()['user_config'] = object +## + + from my.core import dataclass - @dataclass -class config(time.tz.via_location): +class config(user_config): # less precise, but faster fast: bool = True