Skip to content

Commit 53bf188

Browse files
authored
Merge pull request #12523 from lovetheguitar/fix/follow_up_to_pr_#12500
Improve documentation & other kinks of marker keyword expression PR #12500
2 parents 2b7eadf + 36b384a commit 53bf188

File tree

5 files changed

+13
-8
lines changed

5 files changed

+13
-8
lines changed

doc/en/example/markers.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ keyword arguments, e.g. to run only tests marked with ``device`` and the specifi
8080

8181
.. code-block:: pytest
8282
83-
$ pytest -v -m 'device(serial="123")'
83+
$ pytest -v -m "device(serial='123')"
8484
=========================== test session starts ============================
8585
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y -- $PYTHON_PREFIX/bin/python
8686
cachedir: .pytest_cache

doc/en/how-to/usage.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ with the ``phase`` keyword argument set to ``1``:
8888

8989
.. code-block:: bash
9090
91-
pytest -m slow(phase=1)
91+
pytest -m "slow(phase=1)"
9292
9393
For more information see :ref:`marks <mark>`.
9494

src/_pytest/mark/expression.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
expression: expr? EOF
66
expr: and_expr ('or' and_expr)*
77
and_expr: not_expr ('and' not_expr)*
8-
not_expr: 'not' not_expr | '(' expr ')' | ident ( '(' name '=' value ( ', ' name '=' value )* ')')*
8+
not_expr: 'not' not_expr | '(' expr ')' | ident kwargs?
99
1010
ident: (\w|:|\+|-|\.|\[|\]|\\|/)+
11+
kwargs: ('(' name '=' value ( ', ' name '=' value )* ')')
12+
name: a valid ident, but not a reserved keyword
13+
value: (unescaped) string literal | (-)?[0-9]+ | 'False' | 'True' | 'None'
1114
1215
The semantics are:
1316
1417
- Empty expression evaluates to False.
15-
- ident evaluates to True of False according to a provided matcher function.
18+
- ident evaluates to True or False according to a provided matcher function.
1619
- or/and/not evaluate according to the usual boolean semantics.
20+
- ident with parentheses and keyword arguments evaluates to True or False according to a provided matcher function.
1721
"""
1822

1923
from __future__ import annotations
@@ -48,7 +52,7 @@ class TokenType(enum.Enum):
4852
IDENT = "identifier"
4953
EOF = "end of input"
5054
EQUAL = "="
51-
STRING = "str"
55+
STRING = "string literal"
5256
COMMA = ","
5357

5458

testing/test_mark.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def test_two():
235235

236236
@pytest.mark.parametrize(
237237
("expr", "expected_passed"),
238-
[ # TODO: improve/sort out
238+
[
239239
("car(color='red')", ["test_one"]),
240240
("car(color='red') or car(color='blue')", ["test_one", "test_two"]),
241241
("car and not car(temp=5)", ["test_one", "test_three"]),

testing/test_mark_expression.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,10 @@ def test_invalid_idents(ident: str) -> None:
228228
r'escaping with "\\" not supported in marker expression',
229229
),
230230
("mark(empty_list=[])", r'unexpected character/s "\[\]"'),
231+
("'str'", "expected not OR left parenthesis OR identifier; got string literal"),
231232
),
232233
)
233-
def test_invalid_kwarg_name_or_value( # TODO: move to `test_syntax_errors` ?
234+
def test_invalid_kwarg_name_or_value(
234235
expr: str, expected_error_msg: str, mark_matcher: MarkMatcher
235236
) -> None:
236237
with pytest.raises(ParseError, match=expected_error_msg):
@@ -289,7 +290,7 @@ def test_keyword_expressions_with_numbers(
289290
("builtin_matchers_mark(z=1)", False),
290291
),
291292
)
292-
def test_builtin_matchers_keyword_expressions( # TODO: naming when decided
293+
def test_builtin_matchers_keyword_expressions(
293294
expr: str, expected: bool, mark_matcher: MarkMatcher
294295
) -> None:
295296
assert evaluate(expr, mark_matcher) is expected

0 commit comments

Comments
 (0)