From abbaa47aaf1d6be1d7c63318dbf4eba969e75134 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Sun, 27 Sep 2020 18:00:19 +0200 Subject: [PATCH] core.warnings: handle stacklevel properly add more warnings about deprecated config arguments --- my/core/compat.py | 10 ++++------ my/core/warnings.py | 6 ++++-- my/github/ghexport.py | 7 +++++-- my/reddit.py | 7 +++++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/my/core/compat.py b/my/core/compat.py index 2a173b6..27786ea 100644 --- a/my/core/compat.py +++ b/my/core/compat.py @@ -1,9 +1,8 @@ ''' Some backwards compatibility stuff/deprecation helpers ''' -import warnings - -from ..common import LazyLogger +from . import warnings +from .common import LazyLogger logger = LazyLogger('my.core.compat') @@ -23,11 +22,10 @@ def pre_pip_dal_handler( raise e try: dal = _get_dal(cfg, name) - # todo this is fairly high severity, would be nice to highlight in the terminal or something - warnings.warn(f''' + warnings.high(f''' Specifying modules' dependencies in the config or in my/config/repos is deprecated! Please install {' '.join(requires)} as PIP packages (see the corresponding README instructions). -'''.strip()) +'''.strip(), stacklevel=2) except ModuleNotFoundError as ee: dal = None diff --git a/my/core/warnings.py b/my/core/warnings.py index 3d95095..2a71235 100644 --- a/my/core/warnings.py +++ b/my/core/warnings.py @@ -8,6 +8,8 @@ E.g. would be nice to propagate the warnings in the UI (it's even a subclass of import sys import warnings +# just bring in the scope of this module for convenience +from warnings import warn def _colorize(x: str, color=None) -> str: if color is None: @@ -28,8 +30,8 @@ def _colorize(x: str, color=None) -> str: def _warn(message: str, *args, color=None, **kwargs) -> None: - if 'stacklevel' not in kwargs: - kwargs['stacklevel'] = 3 # 1 for this function, 1 for medium/high wrapper + stacklevel = kwargs.get('stacklevel', 1) + kwargs['stacklevel'] = stacklevel + 2 # +1 for this function, +1 for medium/high wrapper warnings.warn(_colorize(message, color=color), *args, **kwargs) diff --git a/my/github/ghexport.py b/my/github/ghexport.py index bef5440..35849a9 100644 --- a/my/github/ghexport.py +++ b/my/github/ghexport.py @@ -43,8 +43,11 @@ class github(user_config): from ..core.cfg import make_config, Attrs def migration(attrs: Attrs) -> Attrs: - if 'export_dir' in attrs: # legacy name - attrs['export_path'] = attrs['export_dir'] + export_dir = 'export_dir' + if export_dir in attrs: # legacy name + attrs['export_path'] = attrs[export_dir] + from ..core.warnings import high + high(f'"{export_dir}" is deprecated! Please use "export_path" instead."') return attrs config = make_config(github, migration=migration) diff --git a/my/reddit.py b/my/reddit.py index 5e1f83b..58c1edf 100755 --- a/my/reddit.py +++ b/my/reddit.py @@ -39,8 +39,11 @@ class reddit(uconfig): from .core.cfg import make_config, Attrs # hmm, also nice thing about this is that migration is possible to test without the rest of the config? def migration(attrs: Attrs) -> Attrs: - if 'export_dir' in attrs: # legacy name - attrs['export_path'] = attrs['export_dir'] + export_dir = 'export_dir' + if export_dir in attrs: # legacy name + attrs['export_path'] = attrs[export_dir] + from .core.warnings import high + high(f'"{export_dir}" is deprecated! Please use "export_path" instead."') return attrs config = make_config(reddit, migration=migration)