| 1 |
"""Deprecation messages and bits of code used elsewhere in the codebase that |
| 2 |
is planned to be removed in the next pytest release. |
| 3 |
|
| 4 |
Keeping it in a central location makes it easy to track what is deprecated and should |
| 5 |
be removed when the time comes. |
| 6 |
|
| 7 |
All constants defined in this module should be either instances of |
| 8 |
:class:`PytestWarning`, or :class:`UnformattedWarning` |
| 9 |
in case of warnings which need to format their messages. |
| 10 |
""" |
| 11 |
|
| 12 |
from warnings import warn |
| 13 |
|
| 14 |
from _pytest.warning_types import PytestDeprecationWarning |
| 15 |
from _pytest.warning_types import PytestRemovedIn9Warning |
| 16 |
from _pytest.warning_types import UnformattedWarning |
| 17 |
|
| 18 |
|
| 19 |
# set of plugins which have been integrated into the core; we use this list to ignore |
| 20 |
# them during registration to avoid conflicts |
| 21 |
DEPRECATED_EXTERNAL_PLUGINS = { |
| 22 |
"pytest_catchlog", |
| 23 |
"pytest_capturelog", |
| 24 |
"pytest_faulthandler", |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
# This can be* removed pytest 8, but it's harmless and common, so no rush to remove. |
| 29 |
# * If you're in the future: "could have been". |
| 30 |
YIELD_FIXTURE = PytestDeprecationWarning( |
| 31 |
"@pytest.yield_fixture is deprecated.\n" |
| 32 |
"Use @pytest.fixture instead; they are the same." |
| 33 |
) |
| 34 |
|
| 35 |
# This deprecation is never really meant to be removed. |
| 36 |
PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.") |
| 37 |
|
| 38 |
|
| 39 |
HOOK_LEGACY_PATH_ARG = UnformattedWarning( |
| 40 |
PytestRemovedIn9Warning, |
| 41 |
"The ({pylib_path_arg}: py.path.local) argument is deprecated, please use ({pathlib_path_arg}: pathlib.Path)\n" |
| 42 |
"see https://docs.pytest.org/en/latest/deprecations.html" |
| 43 |
"#py-path-local-arguments-for-hooks-replaced-with-pathlib-path", |
| 44 |
) |
| 45 |
|
| 46 |
NODE_CTOR_FSPATH_ARG = UnformattedWarning( |
| 47 |
PytestRemovedIn9Warning, |
| 48 |
"The (fspath: py.path.local) argument to {node_type_name} is deprecated. " |
| 49 |
"Please use the (path: pathlib.Path) argument instead.\n" |
| 50 |
"See https://docs.pytest.org/en/latest/deprecations.html" |
| 51 |
"#fspath-argument-for-node-constructors-replaced-with-pathlib-path", |
| 52 |
) |
| 53 |
|
| 54 |
HOOK_LEGACY_MARKING = UnformattedWarning( |
| 55 |
PytestDeprecationWarning, |
| 56 |
"The hook{type} {fullname} uses old-style configuration options (marks or attributes).\n" |
| 57 |
"Please use the pytest.hook{type}({hook_opts}) decorator instead\n" |
| 58 |
" to configure the hooks.\n" |
| 59 |
" See https://docs.pytest.org/en/latest/deprecations.html" |
| 60 |
"#configuring-hook-specs-impls-using-markers", |
| 61 |
) |
| 62 |
|
| 63 |
MARKED_FIXTURE = PytestRemovedIn9Warning( |
| 64 |
"Marks applied to fixtures have no effect\n" |
| 65 |
"See docs: https://docs.pytest.org/en/stable/deprecations.html#applying-a-mark-to-a-fixture-function" |
| 66 |
) |
| 67 |
|
| 68 |
# You want to make some `__init__` or function "private". |
| 69 |
# |
| 70 |
# def my_private_function(some, args): |
| 71 |
# ... |
| 72 |
# |
| 73 |
# Do this: |
| 74 |
# |
| 75 |
# def my_private_function(some, args, *, _ispytest: bool = False): |
| 76 |
# check_ispytest(_ispytest) |
| 77 |
# ... |
| 78 |
# |
| 79 |
# Change all internal/allowed calls to |
| 80 |
# |
| 81 |
# my_private_function(some, args, _ispytest=True) |
| 82 |
# |
| 83 |
# All other calls will get the default _ispytest=False and trigger |
| 84 |
# the warning (possibly error in the future). |
| 85 |
|
| 86 |
|
| 87 |
def check_ispytest(ispytest: bool) -> None: |
| 88 |
if not ispytest: |
| 89 |
warn(PRIVATE, stacklevel=3) |