Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add internal API to warn of deprecation and future removal #2012

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/pyhf/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from warnings import warn

__all__ = [
"FailedMinimization",
Expand Down Expand Up @@ -175,3 +176,15 @@ def __init__(self, result):
result, 'message', "Unknown failure. See fit result for more details."
)
super().__init__(message)


# Deprecated APIs
def _deprecated_api_warning(
deprecated_api, new_api, deprecated_release, remove_release
):
warn(
f"{deprecated_api} is deprecated in favor of {new_api} as of pyhf v{deprecated_release} and will be removed in pyhf v{remove_release}."
+ f" Please use {new_api}.",
DeprecationWarning,
stacklevel=3, # Raise to user level
)
18 changes: 18 additions & 0 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
import pytest
from skhep_testdata import data_path
Expand Down Expand Up @@ -176,3 +178,19 @@ def test_sbottom_regionC_1600_850_60(get_json_from_tarfile):
rtol=1e-5,
)
)


def test_deprecated_api_warning():
with warnings.catch_warnings(record=True) as _warning:
# Cause all warnings to always be triggered
warnings.simplefilter("always")

pyhf.exceptions._deprecated_api_warning(
"deprecated_api", "new_api", "0.9.9", "1.0.0"
)
assert len(_warning) == 1
assert issubclass(_warning[-1].category, DeprecationWarning)
assert (
"deprecated_api is deprecated in favor of new_api as of pyhf v0.9.9 and will be removed in pyhf v1.0.0."
in str(_warning[-1].message)
)