core.warnings: handle stacklevel properly

add more warnings about deprecated config arguments
This commit is contained in:
Dima Gerasimov 2020-09-27 18:00:19 +02:00 committed by karlicoss
parent 109edd9da3
commit abbaa47aaf
4 changed files with 18 additions and 12 deletions

View file

@ -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

View file

@ -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)

View file

@ -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)

View file

@ -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)