Debian Codegraph @

src/_pytest/deprecated.py

raw
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 __future__ import annotations
13
14 from warnings import warn
15
16 from _pytest.warning_types import PytestDeprecationWarning
17 from _pytest.warning_types import PytestRemovedIn10Warning
18 from _pytest.warning_types import UnformattedWarning
19
20
21 # set of plugins which have been integrated into the core; we use this list to ignore
22 # them during registration to avoid conflicts
23 DEPRECATED_EXTERNAL_PLUGINS = {
24 "pytest_catchlog",
25 "pytest_capturelog",
26 "pytest_faulthandler",
27 "pytest_subtests",
28 }
29
30
31 # This could have been removed pytest 8, but it's harmless and common, so no rush to remove.
32 YIELD_FIXTURE = PytestDeprecationWarning(
33 "@pytest.yield_fixture is deprecated.\n"
34 "Use @pytest.fixture instead; they are the same."
35 )
36
37 CLASS_FIXTURE_INSTANCE_METHOD = PytestRemovedIn10Warning(
38 "Class-scoped fixture defined as instance method is deprecated.\n"
39 "Instance attributes set in this fixture will NOT be visible to test methods,\n"
40 "as each test gets a new instance while the fixture runs only once per class.\n"
41 "Use @classmethod decorator and set attributes on cls instead.\n"
42 "See https://docs.pytest.org/en/stable/deprecations.html#class-scoped-fixture-as-instance-method"
43 )
44
45 # This deprecation is never really meant to be removed.
46 PRIVATE = PytestDeprecationWarning("A private pytest class or function was used.")
47
48
49 HOOK_LEGACY_MARKING = UnformattedWarning(
50 PytestRemovedIn10Warning,
51 "The hook{type} {fullname} uses old-style configuration options (marks or attributes).\n"
52 "Please use the pytest.hook{type}({hook_opts}) decorator instead\n"
53 " to configure the hooks.\n"
54 " See https://docs.pytest.org/en/latest/deprecations.html"
55 "#configuring-hook-specs-impls-using-markers",
56 )
57
58 MONKEYPATCH_LEGACY_NAMESPACE_PACKAGES = PytestRemovedIn10Warning(
59 "monkeypatch.syspath_prepend() called with pkg_resources legacy namespace packages detected.\n"
60 "Legacy namespace packages (using pkg_resources.declare_namespace) are deprecated.\n"
61 "Please use native namespace packages (PEP 420) instead.\n"
62 "See https://docs.pytest.org/en/stable/deprecations.html#monkeypatch-fixup-namespace-packages"
63 )
64
65 PARAMETRIZE_NON_COLLECTION_ITERABLE = UnformattedWarning(
66 PytestRemovedIn10Warning,
67 "Passing a non-Collection iterable to parametrize is deprecated.\n"
68 "Test: {nodeid}, argvalues type: {type_name}\n"
69 "Please convert to a list or tuple.\n"
70 "See https://docs.pytest.org/en/stable/deprecations.html#parametrize-iterators",
71 )
72
73 CONSOLE_MAIN = PytestRemovedIn10Warning(
74 "pytest.console_main() is deprecated and will be removed in pytest 10.\n"
75 "It was never intended for programmatic use; use pytest.main() instead.\n"
76 "See https://docs.pytest.org/en/stable/deprecations.html#console-main"
77 )
78
79 CONFIG_INICFG = PytestRemovedIn10Warning(
80 "config.inicfg is deprecated, use config.getini() to access configuration values instead.\n"
81 "See https://docs.pytest.org/en/stable/deprecations.html#config-inicfg"
82 )
83
84 FIXTURE_GETFIXTUREVALUE_DURING_TEARDOWN = UnformattedWarning(
85 PytestRemovedIn10Warning,
86 'Calling request.getfixturevalue("{argname}") during teardown is deprecated.\n'
87 "Please request the fixture before teardown begins, either by declaring it in the fixture signature "
88 "or by calling request.getfixturevalue() before the fixture yields.\n"
89 "See https://docs.pytest.org/en/stable/deprecations.html#dynamic-fixture-request-during-teardown",
90 )
91
92 PASTEBIN = PytestRemovedIn10Warning(
93 "The --pastebin option is deprecated. "
94 "The functionality is now available in an external plugin package, pytest-pastebin.\n"
95 "See https://docs.pytest.org/en/stable/deprecations.html#the-pastebin-option"
96 )
97
98 # You want to make some `__init__` or function "private".
99 #
100 # def my_private_function(some, args):
101 # ...
102 #
103 # Do this:
104 #
105 # def my_private_function(some, args, *, _ispytest: bool = False):
106 # check_ispytest(_ispytest)
107 # ...
108 #
109 # Change all internal/allowed calls to
110 #
111 # my_private_function(some, args, _ispytest=True)
112 #
113 # All other calls will get the default _ispytest=False and trigger
114 # the warning (possibly error in the future).
115
116
117 FIXTURE_BASEID_DEPRECATED = PytestRemovedIn10Warning(
118 "Passing baseid to FixtureDef is deprecated. Pass node instead for fixture scoping."
119 )
120
121 FIXTURE_NODEID_DEPRECATED = PytestRemovedIn10Warning(
122 "Passing nodeid to _register_fixture is deprecated. "
123 "Pass node instead for fixture scoping."
124 )
125
126 FIXTUREDEF_HAS_LOCATION_DEPRECATED = PytestRemovedIn10Warning(
127 "FixtureDef.has_location is deprecated and will be removed in pytest 10. "
128 "See https://docs.pytest.org/en/stable/deprecations.html#fixturedef-has-location-deprecated"
129 )
130
131 PARSEFACTORIES_NODEID_DEPRECATED = PytestRemovedIn10Warning(
132 "Passing nodeid string to parsefactories is deprecated. "
133 "Use parsefactories(holder=obj, node=node) instead."
134 )
135
136
137 def check_ispytest(ispytest: bool) -> None:
138 if not ispytest:
139 warn(PRIVATE, stacklevel=3)