Skip to content

Commit

Permalink
msg for no target
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda committed Mar 21, 2021
1 parent 9e83751 commit 8d193da
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions deprecate/deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
from typing import Any, Callable, Dict, List, Optional, Tuple
from warnings import warn

#: Template warning message
#: Default template warning message
TEMPLATE_WARNING = (
"The `%(source_name)s` was deprecated since v%(deprecated_in)s in favor of `%(target_path)s`."
" It will be removed in v%(remove_in)s."
)
#: Default template warning message for no target func/method
TEMPLATE_WARNING_NO_TARGET = (
"The `%(source_name)s` was deprecated since v%(deprecated_in)s. It will be removed in v%(remove_in)s."
)

deprecation_warning = partial(warn, category=DeprecationWarning)

Expand Down Expand Up @@ -68,7 +72,7 @@ def _raise_warn(
target: Optional[Callable],
deprecated_in: str,
remove_in: str,
template_mgs: str = TEMPLATE_WARNING,
template_mgs: Optional[str] = None,
) -> None:
"""
Raise deprecation warning with in given stream,
Expand All @@ -92,8 +96,10 @@ def _raise_warn(
if target:
target_name = target.__name__
target_path = f'{target.__module__}.{target_name}'
template_mgs = TEMPLATE_WARNING if template_mgs is None else template_mgs
else:
target_name, target_path = "", ""
template_mgs = TEMPLATE_WARNING_NO_TARGET if template_mgs is None else template_mgs
source_name = source.__qualname__.split('.')[-2] if source.__name__ == "__init__" else source.__name__
source_path = f'{source.__module__}.{source_name}'
msg_args = dict(
Expand All @@ -113,7 +119,7 @@ def deprecated(
remove_in: str = "",
stream: Optional[Callable] = deprecation_warning,
num_warns: int = 1,
template_mgs: str = TEMPLATE_WARNING,
template_mgs: Optional[str] = None,
args_mapping: Optional[Dict[str, str]] = None,
args_extra: Optional[Dict[str, Any]] = None,
) -> Callable:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def depr_pow_wrong(a: int, c: float = 4) -> float:

def test_deprecated_func_warn_only() -> None:
with pytest.deprecated_call(
match='The `depr_sum_warn_only` was deprecated since v0.2 in favor of ``. It will be removed in v0.3.'
match='The `depr_sum_warn_only` was deprecated since v0.2. It will be removed in v0.3.'
):
assert depr_sum_warn_only(2) is None

Expand Down

0 comments on commit 8d193da

Please sign in to comment.