Skip to content

Commit 15d5e8c

Browse files
authored
Merge pull request #7210 from bluetech/minus-k-deprecations
mark: deprecate a couple undocumented -k syntaxes
2 parents 0ba6e1f + c4f9eaa commit 15d5e8c

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

changelog/7210.deprecation.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The special ``-k '-expr'`` syntax to ``-k`` is deprecated. Use ``-k 'not expr'``
2+
instead.
3+
4+
The special ``-k 'expr:'`` syntax to ``-k`` is deprecated. Please open an issue
5+
if you use this and want a replacement.

src/_pytest/deprecated.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,13 @@
7575
"The TerminalReporter.writer attribute is deprecated, use TerminalReporter._tw instead at your own risk.\n"
7676
"See https://docs.pytest.org/en/latest/deprecations.html#terminalreporter-writer for more information."
7777
)
78+
79+
80+
MINUS_K_DASH = PytestDeprecationWarning(
81+
"The `-k '-expr'` syntax to -k is deprecated.\nUse `-k 'not expr'` instead."
82+
)
83+
84+
MINUS_K_COLON = PytestDeprecationWarning(
85+
"The `-k 'expr:'` syntax to -k is deprecated.\n"
86+
"Please open an issue if you use this and want a replacement."
87+
)

src/_pytest/mark/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
""" generic mechanism for marking and selecting python functions. """
2+
import warnings
23
from typing import Optional
34

45
from .legacy import matchkeyword
@@ -13,6 +14,8 @@
1314
from _pytest.config import Config
1415
from _pytest.config import hookimpl
1516
from _pytest.config import UsageError
17+
from _pytest.deprecated import MINUS_K_COLON
18+
from _pytest.deprecated import MINUS_K_DASH
1619
from _pytest.store import StoreKey
1720

1821
__all__ = ["Mark", "MarkDecorator", "MarkGenerator", "get_empty_parameterset_mark"]
@@ -107,9 +110,13 @@ def deselect_by_keyword(items, config):
107110
return
108111

109112
if keywordexpr.startswith("-"):
113+
# To be removed in pytest 7.0.0.
114+
warnings.warn(MINUS_K_DASH, stacklevel=2)
110115
keywordexpr = "not " + keywordexpr[1:]
111116
selectuntil = False
112117
if keywordexpr[-1:] == ":":
118+
# To be removed in pytest 7.0.0.
119+
warnings.warn(MINUS_K_COLON, stacklevel=2)
113120
selectuntil = True
114121
keywordexpr = keywordexpr[:-1]
115122

testing/deprecated_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,27 @@ def test__fillfuncargs_is_deprecated() -> None:
164164
match="The `_fillfuncargs` function is deprecated",
165165
):
166166
pytest._fillfuncargs(mock.Mock())
167+
168+
169+
def test_minus_k_dash_is_deprecated(testdir) -> None:
170+
threepass = testdir.makepyfile(
171+
test_threepass="""
172+
def test_one(): assert 1
173+
def test_two(): assert 1
174+
def test_three(): assert 1
175+
"""
176+
)
177+
result = testdir.runpytest("-k=-test_two", threepass)
178+
result.stdout.fnmatch_lines(["*The `-k '-expr'` syntax*deprecated*"])
179+
180+
181+
def test_minus_k_colon_is_deprecated(testdir) -> None:
182+
threepass = testdir.makepyfile(
183+
test_threepass="""
184+
def test_one(): assert 1
185+
def test_two(): assert 1
186+
def test_three(): assert 1
187+
"""
188+
)
189+
result = testdir.runpytest("-k", "test_two:", threepass)
190+
result.stdout.fnmatch_lines(["*The `-k 'expr:'` syntax*deprecated*"])

0 commit comments

Comments
 (0)