2019-12-30 15:27:32 -05:00
|
|
|
import logging
|
2021-01-14 14:44:33 +01:00
|
|
|
from typing import Optional, Union
|
2019-12-30 15:27:32 -05:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-02-19 21:18:45 +01:00
|
|
|
# A constant to use for any configuration that should be deprecated
|
|
|
|
# (to check, whether this config has actually been assigned a proper value or
|
|
|
|
# not).
|
|
|
|
DEPRECATED_VALUE = -1
|
|
|
|
|
2019-12-30 15:27:32 -05:00
|
|
|
|
2021-01-14 14:44:33 +01:00
|
|
|
def deprecation_warning(
|
|
|
|
old: str,
|
|
|
|
new: Optional[str] = None,
|
2021-08-03 18:30:02 -04:00
|
|
|
*,
|
|
|
|
help: Optional[str] = None,
|
2021-01-14 14:44:33 +01:00
|
|
|
error: Optional[Union[bool, Exception]] = None) -> None:
|
|
|
|
"""Warns (via the `logger` object) or throws a deprecation warning/error.
|
2020-01-21 08:06:50 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
old (str): A description of the "thing" that is to be deprecated.
|
|
|
|
new (Optional[str]): A description of the new "thing" that replaces it.
|
2021-08-03 18:30:02 -04:00
|
|
|
help (Optional[str]): An optional help text to tell the user, what to
|
|
|
|
do instead of using `old`.
|
2021-01-14 14:44:33 +01:00
|
|
|
error (Optional[Union[bool, Exception]]): Whether or which exception to
|
|
|
|
throw. If True, throw ValueError. If False, just warn.
|
|
|
|
If Exception, throw that Exception.
|
2020-01-21 08:06:50 +01:00
|
|
|
"""
|
2020-02-11 00:22:07 +01:00
|
|
|
msg = "`{}` has been deprecated.{}".format(
|
2021-08-03 18:30:02 -04:00
|
|
|
old, (" Use `{}` instead.".format(new) if new else f" {help}"
|
|
|
|
if help else ""))
|
2020-02-11 00:22:07 +01:00
|
|
|
|
|
|
|
if error is True:
|
|
|
|
raise ValueError(msg)
|
|
|
|
elif error and issubclass(error, Exception):
|
|
|
|
raise error(msg)
|
|
|
|
else:
|
|
|
|
logger.warning("DeprecationWarning: " + msg +
|
|
|
|
" This will raise an error in the future!")
|