2
2
#
3
3
# grammar.py
4
4
"""
5
- The expression grammar for ``coverage_pyver_pragma``.
6
-
7
5
.. versionadded:: 0.2.0
8
6
9
- Each expression consists of one or more tags (``VERSION_TAG``, ``PLATFORM_TAG`` or ``IMPLEMENTATION_TAG``).
7
+ As with ``coverage.py``, lines are marked with comments in the form::
8
+
9
+ # pragma: no cover
10
+
11
+ With ``coverage_pyver_pragma``, the comment may be followed with an expression enclosed in parentheses::
12
+
13
+ # pragma: no cover (<=py38 and !Windows)
14
+
15
+ Each expression consists of one or more tags
16
+ (:py:data:`VERSION_TAG`, :py:data:`PLATFORM_TAG` or :py:data:`IMPLEMENTATION_TAG`).
10
17
The tags can be joined with the keywords ``AND``, ``OR`` and ``NOT``, with the exclamation mark ``!`` implying ``NOT``.
11
18
Parentheses can be used to group sub expressions.
19
+ A series of tags without keywords in between them are evaluated with ``AND``.
12
20
13
- ``VERSION_TAG``
14
- -------------------
21
+ .. py:data:: VERSION_TAG
15
22
16
23
A ``VERSION_TAG`` comprises an optional comparator (one of ``<=``, ``<``, ``>=``, ``>``),
17
24
a version specifier in the form ``pyXX``, and an optional ``+`` to indicate ``>=``.
27
34
py34+ # equivalent to >=py34
28
35
29
36
30
- ``PLATFORM_TAG``
31
- -------------------
37
+ .. py:data:: PLATFORM_TAG
32
38
33
39
A ``PLATFORM_TAG`` comprises a single word which will be compared (ignoring case)
34
40
with the output of :func:`platform.system`.
45
51
If the current platform cannot be determined all strings are treated as :py:obj:`True`.
46
52
47
53
48
- ``IMPLEMENTATION_TAG``
49
- -----------------------
54
+ .. py:data:: IMPLEMENTATION_TAG
50
55
51
56
An ``IMPLEMENTATION_TAG`` comprises a single word which will be compared (ignoring case)
52
57
with the output of :func:`platform.python_implementation`.
63
68
Examples
64
69
-----------
65
70
66
- .. parsed-literal::
71
+ ignore if the Python version is less than or equal to 3.7::
72
+
73
+ # pragma: no cover (<=py37)
74
+
75
+ ignore if running on Python 3.9::
76
+
77
+ # pragma: no cover (py39)
78
+
79
+ Ignore if the Python version is greater than 3.6 and it's not running on PyPy::
80
+
81
+ # pragma: no cover (>py36 and !PyPy)
67
82
68
- >py36 and !PyPy
69
- Windows and <py38
83
+ Ignore if the Python version is less than 3.8 and it's running on Windows::
84
+
85
+ # pragma: no cover (Windows and <py38)
86
+
87
+ Ignore when not running on macOS (Darwin)::
88
+
89
+ # pragma: no cover (!Darwin)
90
+
91
+ Ignore when not running on CPython::
92
+
93
+ # pragma: no cover (!CPython)
94
+
95
+ API Reference
96
+ ----------------
70
97
71
98
"""
72
99
#
106
133
Literal ,
107
134
OneOrMore ,
108
135
Optional ,
136
+ ParserElement ,
109
137
ParseResults ,
110
138
Word ,
111
139
infixNotation ,
@@ -200,8 +228,8 @@ class PlatformTag(str):
200
228
201
229
__slots__ = ()
202
230
203
- def __new__ (cls , token : ParseResults ): # noqa: D102
204
- return super ().__new__ (cls , str (token ["platform" ]))
231
+ def __new__ (cls , tokens : ParseResults ): # noqa: D102
232
+ return super ().__new__ (cls , str (tokens ["platform" ]))
205
233
206
234
def __repr__ (self ) -> str : # pragma: no cover
207
235
return f"<{ self .__class__ .__name__ } ({ str (self )!r} )>"
@@ -353,7 +381,7 @@ def __bool__(self):
353
381
354
382
ELEMENTS = VERSION_TAG | PLATFORM_TAG | IMPLEMENTATION_TAG
355
383
356
- GRAMMAR = OneOrMore (
384
+ GRAMMAR : ParserElement = OneOrMore (
357
385
infixNotation (
358
386
ELEMENTS ,
359
387
[
@@ -363,3 +391,8 @@ def __bool__(self):
363
391
]
364
392
)
365
393
)
394
+ """
395
+ The ``coverage_pyver_pragma`` expression grammar.
396
+
397
+ This can be used to parse an expression outside of the coverage context.
398
+ """
0 commit comments