Skip to content

Commit 7ec6401

Browse files
nicoddemushugovk
andauthored
Change pytest deprecation warnings into errors for 6.0 release (#7362)
Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
1 parent a9799f0 commit 7ec6401

File tree

19 files changed

+122
-84
lines changed

19 files changed

+122
-84
lines changed

changelog/5584.breaking.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**PytestDeprecationWarning are now errors by default.**
2+
3+
Following our plan to remove deprecated features with as little disruption as
4+
possible, all warnings of type ``PytestDeprecationWarning`` now generate errors
5+
instead of warning messages.
6+
7+
**The affected features will be effectively removed in pytest 6.1**, so please consult the
8+
`Deprecations and Removals <https://docs.pytest.org/en/latest/deprecations.html>`__
9+
section in the docs for directions on how to update existing code.
10+
11+
In the pytest ``6.0.X`` series, it is possible to change the errors back into warnings as a
12+
stopgap measure by adding this to your ``pytest.ini`` file:
13+
14+
.. code-block:: ini
15+
16+
[pytest]
17+
filterwarnings =
18+
ignore::pytest.PytestDeprecationWarning
19+
20+
But this will stop working when pytest ``6.1`` is released.
21+
22+
**If you have concerns** about the removal of a specific feature, please add a
23+
comment to `#5584 <https://github.com/pytest-dev/pytest/issues/5584>`__.

doc/en/reference.rst

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,45 @@ When set (regardless of value), pytest will use color in terminal output.
10241024
Exceptions
10251025
----------
10261026

1027-
UsageError
1028-
~~~~~~~~~~
1029-
10301027
.. autoclass:: _pytest.config.UsageError()
1028+
:show-inheritance:
1029+
1030+
.. _`warnings ref`:
1031+
1032+
Warnings
1033+
--------
1034+
1035+
Custom warnings generated in some situations such as improper usage or deprecated features.
1036+
1037+
.. autoclass:: pytest.PytestWarning
1038+
:show-inheritance:
1039+
1040+
.. autoclass:: pytest.PytestAssertRewriteWarning
1041+
:show-inheritance:
1042+
1043+
.. autoclass:: pytest.PytestCacheWarning
1044+
:show-inheritance:
1045+
1046+
.. autoclass:: pytest.PytestCollectionWarning
1047+
:show-inheritance:
1048+
1049+
.. autoclass:: pytest.PytestConfigWarning
1050+
:show-inheritance:
1051+
1052+
.. autoclass:: pytest.PytestDeprecationWarning
1053+
:show-inheritance:
1054+
1055+
.. autoclass:: pytest.PytestExperimentalApiWarning
1056+
:show-inheritance:
1057+
1058+
.. autoclass:: pytest.PytestUnhandledCoroutineWarning
1059+
:show-inheritance:
1060+
1061+
.. autoclass:: pytest.PytestUnknownMarkWarning
1062+
:show-inheritance:
1063+
1064+
1065+
Consult the :ref:`internal-warnings` section in the documentation for more information.
10311066

10321067

10331068
.. _`ini options ref`:

doc/en/warnings.rst

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,6 @@ custom error message.
381381
Internal pytest warnings
382382
------------------------
383383

384-
385-
386384
pytest may generate its own warnings in some situations, such as improper usage or deprecated features.
387385

388386
For example, pytest will emit a warning if it encounters a class that matches :confval:`python_classes` but also
@@ -415,31 +413,4 @@ These warnings might be filtered using the same builtin mechanisms used to filte
415413
Please read our :ref:`backwards-compatibility` to learn how we proceed about deprecating and eventually removing
416414
features.
417415

418-
The following warning types are used by pytest and are part of the public API:
419-
420-
.. autoclass:: pytest.PytestWarning
421-
:show-inheritance:
422-
423-
.. autoclass:: pytest.PytestAssertRewriteWarning
424-
:show-inheritance:
425-
426-
.. autoclass:: pytest.PytestCacheWarning
427-
:show-inheritance:
428-
429-
.. autoclass:: pytest.PytestCollectionWarning
430-
:show-inheritance:
431-
432-
.. autoclass:: pytest.PytestConfigWarning
433-
:show-inheritance:
434-
435-
.. autoclass:: pytest.PytestDeprecationWarning
436-
:show-inheritance:
437-
438-
.. autoclass:: pytest.PytestExperimentalApiWarning
439-
:show-inheritance:
440-
441-
.. autoclass:: pytest.PytestUnhandledCoroutineWarning
442-
:show-inheritance:
443-
444-
.. autoclass:: pytest.PytestUnknownMarkWarning
445-
:show-inheritance:
416+
The full list of warnings is listed in :ref:`the reference documentation <warnings ref>`.

src/_pytest/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
from _pytest.config import _PluggyPlugin
4747
from _pytest.config import Config
4848
from _pytest.config.argparsing import Parser
49-
from _pytest.deprecated import FILLFUNCARGS
5049
from _pytest.deprecated import FIXTURE_POSITIONAL_ARGUMENTS
5150
from _pytest.deprecated import FUNCARGNAMES
5251
from _pytest.mark import ParameterSet
@@ -361,7 +360,8 @@ def reorder_items_atscope(
361360

362361
def fillfixtures(function: "Function") -> None:
363362
""" fill missing funcargs for a test function. """
364-
warnings.warn(FILLFUNCARGS, stacklevel=2)
363+
# Uncomment this after 6.0 release (#7361)
364+
# warnings.warn(FILLFUNCARGS, stacklevel=2)
365365
try:
366366
request = function._request
367367
except AttributeError:

src/_pytest/hookspec.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pluggy import HookspecMarker
1313

1414
from .deprecated import COLLECT_DIRECTORY_HOOK
15-
from .deprecated import WARNING_CAPTURED_HOOK
1615
from _pytest.compat import TYPE_CHECKING
1716

1817
if TYPE_CHECKING:
@@ -737,7 +736,9 @@ def pytest_terminal_summary(
737736
"""
738737

739738

740-
@hookspec(historic=True, warn_on_impl=WARNING_CAPTURED_HOOK)
739+
# Uncomment this after 6.0 release (#7361)
740+
# @hookspec(historic=True, warn_on_impl=WARNING_CAPTURED_HOOK)
741+
@hookspec(historic=True)
741742
def pytest_warning_captured(
742743
warning_message: "warnings.WarningMessage",
743744
when: "Literal['config', 'collect', 'runtest']",

src/_pytest/mark/__init__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
""" generic mechanism for marking and selecting python functions. """
22
import typing
3-
import warnings
43
from typing import AbstractSet
54
from typing import List
65
from typing import Optional
@@ -23,8 +22,6 @@
2322
from _pytest.config import hookimpl
2423
from _pytest.config import UsageError
2524
from _pytest.config.argparsing import Parser
26-
from _pytest.deprecated import MINUS_K_COLON
27-
from _pytest.deprecated import MINUS_K_DASH
2825
from _pytest.store import StoreKey
2926

3027
if TYPE_CHECKING:
@@ -181,12 +178,14 @@ def deselect_by_keyword(items: "List[Item]", config: Config) -> None:
181178

182179
if keywordexpr.startswith("-"):
183180
# To be removed in pytest 7.0.0.
184-
warnings.warn(MINUS_K_DASH, stacklevel=2)
181+
# Uncomment this after 6.0 release (#7361)
182+
# warnings.warn(MINUS_K_DASH, stacklevel=2)
185183
keywordexpr = "not " + keywordexpr[1:]
186184
selectuntil = False
187185
if keywordexpr[-1:] == ":":
188186
# To be removed in pytest 7.0.0.
189-
warnings.warn(MINUS_K_COLON, stacklevel=2)
187+
# Uncomment this after 6.0 release (#7361)
188+
# warnings.warn(MINUS_K_COLON, stacklevel=2)
190189
selectuntil = True
191190
keywordexpr = keywordexpr[:-1]
192191

src/_pytest/warning_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class PytestUnhandledCoroutineWarning(PytestWarning):
7878
class PytestUnknownMarkWarning(PytestWarning):
7979
"""Warning emitted on use of unknown markers.
8080
81-
See https://docs.pytest.org/en/stable/mark.html for details.
81+
See :ref:`mark` for details.
8282
"""
8383

8484
__module__ = "pytest"

src/_pytest/warnings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ def catch_warnings_for_item(
105105
warnings.filterwarnings("always", category=DeprecationWarning)
106106
warnings.filterwarnings("always", category=PendingDeprecationWarning)
107107

108+
warnings.filterwarnings("error", category=pytest.PytestDeprecationWarning)
109+
108110
# filters should have this precedence: mark, cmdline options, ini
109111
# filters should be applied in the inverse order of precedence
110112
for arg in inifilters:

src/pytest/collect.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import sys
2-
import warnings
32
from types import ModuleType
43
from typing import Any
54
from typing import List
65

76
import pytest
8-
from _pytest.deprecated import PYTEST_COLLECT_MODULE
97

108

119
COLLECT_FAKEMODULE_ATTRIBUTES = [
@@ -33,7 +31,8 @@ def __dir__(self) -> List[str]:
3331
def __getattr__(self, name: str) -> Any:
3432
if name not in self.__all__:
3533
raise AttributeError(name)
36-
warnings.warn(PYTEST_COLLECT_MODULE.format(name=name), stacklevel=2)
34+
# Uncomment this after 6.0 release (#7361)
35+
# warnings.warn(PYTEST_COLLECT_MODULE.format(name=name), stacklevel=2)
3736
return getattr(pytest, name)
3837

3938

testing/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ def runtest(self):
302302
pass
303303
class MyCollector(pytest.File):
304304
def collect(self):
305-
return [MyItem(name="xyz", parent=self)]
305+
return [MyItem.from_parent(name="xyz", parent=self)]
306306
def pytest_collect_file(path, parent):
307307
if path.basename.startswith("conftest"):
308-
return MyCollector(path, parent)
308+
return MyCollector.from_parent(fspath=path, parent=parent)
309309
"""
310310
)
311311
result = testdir.runpytest(c.basename + "::" + "xyz")

testing/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ def dummy_yaml_custom_test(testdir):
116116
117117
def pytest_collect_file(parent, path):
118118
if path.ext == ".yaml" and path.basename.startswith("test"):
119-
return YamlFile(path, parent)
119+
return YamlFile.from_parent(fspath=path, parent=parent)
120120
121121
class YamlFile(pytest.File):
122122
def collect(self):
123-
yield YamlItem(self.fspath.basename, self)
123+
yield YamlItem.from_parent(name=self.fspath.basename, parent=self)
124124
125125
class YamlItem(pytest.Item):
126126
def runtest(self):

testing/deprecated_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def test():
2828
)
2929

3030

31+
@pytest.mark.skip(reason="should be reintroduced in 6.1: #7361")
3132
@pytest.mark.parametrize("attribute", pytest.collect.__all__) # type: ignore
3233
# false positive due to dynamic attribute
3334
def test_pytest_collect_module_deprecated(attribute):
@@ -117,14 +118,16 @@ class MockConfig:
117118
assert w[0].filename == __file__
118119

119120

120-
def test__fillfuncargs_is_deprecated() -> None:
121+
@pytest.mark.skip(reason="should be reintroduced in 6.1: #7361")
122+
def test_fillfuncargs_is_deprecated() -> None:
121123
with pytest.warns(
122124
pytest.PytestDeprecationWarning,
123125
match="The `_fillfuncargs` function is deprecated",
124126
):
125127
pytest._fillfuncargs(mock.Mock())
126128

127129

130+
@pytest.mark.skip(reason="should be reintroduced in 6.1: #7361")
128131
def test_minus_k_dash_is_deprecated(testdir) -> None:
129132
threepass = testdir.makepyfile(
130133
test_threepass="""
@@ -137,6 +140,7 @@ def test_three(): assert 1
137140
result.stdout.fnmatch_lines(["*The `-k '-expr'` syntax*deprecated*"])
138141

139142

143+
@pytest.mark.skip(reason="should be reintroduced in 6.1: #7361")
140144
def test_minus_k_colon_is_deprecated(testdir) -> None:
141145
threepass = testdir.makepyfile(
142146
test_threepass="""
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import pytest
22

33

4-
class CustomItem(pytest.Item, pytest.File):
4+
class CustomItem(pytest.Item):
55
def runtest(self):
66
pass
77

88

9+
class CustomFile(pytest.File):
10+
def collect(self):
11+
yield CustomItem.from_parent(name="foo", parent=self)
12+
13+
914
def pytest_collect_file(path, parent):
10-
return CustomItem(path, parent)
15+
return CustomFile.from_parent(fspath=path, parent=parent)

testing/example_scripts/issue88_initial_file_multinodes/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
class MyFile(pytest.File):
55
def collect(self):
6-
return [MyItem("hello", parent=self)]
6+
return [MyItem.from_parent(name="hello", parent=self)]
77

88

99
def pytest_collect_file(path, parent):
10-
return MyFile(path, parent)
10+
return MyFile.from_parent(fspath=path, parent=parent)
1111

1212

1313
class MyItem(pytest.Item):

testing/python/collect.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ class MyModule(pytest.Module):
762762
pass
763763
def pytest_pycollect_makemodule(path, parent):
764764
if path.basename == "test_xyz.py":
765-
return MyModule(path, parent)
765+
return MyModule.from_parent(fspath=path, parent=parent)
766766
"""
767767
)
768768
testdir.makepyfile("def test_some(): pass")
@@ -836,7 +836,7 @@ class MyFunction(pytest.Function):
836836
pass
837837
def pytest_pycollect_makeitem(collector, name, obj):
838838
if name == "some":
839-
return MyFunction(name, collector)
839+
return MyFunction.from_parent(name=name, parent=collector)
840840
"""
841841
)
842842
testdir.makepyfile("def some(): pass")
@@ -873,7 +873,7 @@ def find_module(self, name, path=None):
873873
874874
def pytest_collect_file(path, parent):
875875
if path.ext == ".narf":
876-
return Module(path, parent)"""
876+
return Module.from_parent(fspath=path, parent=parent)"""
877877
)
878878
testdir.makefile(
879879
".narf",

0 commit comments

Comments
 (0)