Skip to content

Commit 7d93757

Browse files
committed
compat: add compat for @warnings.deprecated
We intend to start using it.
1 parent 213ae30 commit 7d93757

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/_pytest/compat.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from typing import Any
1616
from typing import Final
1717
from typing import NoReturn
18+
from typing import TYPE_CHECKING
1819

1920
import py
2021

@@ -311,3 +312,17 @@ def running_on_ci() -> bool:
311312
# Only enable CI mode if one of these env variables is defined and non-empty.
312313
env_vars = ["CI", "BUILD_NUMBER"]
313314
return any(os.environ.get(var) for var in env_vars)
315+
316+
317+
if sys.version_info >= (3, 13):
318+
from warnings import deprecated as deprecated
319+
else:
320+
if TYPE_CHECKING:
321+
from typing_extensions import deprecated as deprecated
322+
else:
323+
324+
def deprecated(msg, /, *, category=None, stacklevel=1):
325+
def decorator(func):
326+
return func
327+
328+
return decorator

testing/test_compat.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
from functools import cached_property
66
from functools import partial
77
from functools import wraps
8-
from typing import TYPE_CHECKING
8+
from typing import Literal
9+
import warnings
910

1011
from _pytest.compat import assert_never
12+
from _pytest.compat import deprecated
1113
from _pytest.compat import get_real_func
1214
from _pytest.compat import safe_getattr
1315
from _pytest.compat import safe_isclass
1416
from _pytest.outcomes import OutcomeException
1517
import pytest
1618

1719

18-
if TYPE_CHECKING:
19-
from typing import Literal
20-
21-
2220
def test_real_func_loop_limit() -> None:
2321
class Evil:
2422
def __init__(self):
@@ -192,3 +190,15 @@ def test_assert_never_literal() -> None:
192190
pass
193191
else:
194192
assert_never(x)
193+
194+
195+
def test_deprecated() -> None:
196+
# This test is mostly for coverage.
197+
198+
@deprecated("This is deprecated!")
199+
def old_way() -> str:
200+
return "human intelligence"
201+
202+
with warnings.catch_warnings():
203+
warnings.simplefilter("ignore", DeprecationWarning)
204+
assert old_way() == "human intelligence" # type: ignore[deprecated]

0 commit comments

Comments
 (0)