Skip to content

Commit ffd3779

Browse files
committed
docs: multi-line regex exclusion was wrong. #1863
1 parent 71f0f4c commit ffd3779

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Unreleased
4242
is false. To keep checksums set it to true. All this work is thanks to `Zack
4343
Weinberg <pull 1849_>`_.
4444

45+
- Fixed the docs for multi-line regex exclusions, closing `issue 1863`_.
46+
4547
- Fixed a potential crash in the C tracer, closing `issue 1835`_, thanks to
4648
`Jan Kühle <pull 1843_>`_.
4749

doc/config.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ Settings common to many kinds of reporting.
593593
from reporting. This setting is preferred, because it will preserve the
594594
default exclude pattern ``pragma: no cover`` instead of overwriting it.
595595

596+
See :ref:`config_report_exclude_lines` for further details.
597+
596598
.. versionadded:: 7.2.0
597599

598600

@@ -617,6 +619,10 @@ only have to match a portion of the line. For example, if you write ``...``,
617619
you'll exclude any line with three or more of any character. If you write
618620
``pass``, you'll also exclude the line ``my_pass="foo"``, and so on.
619621

622+
All of the regexes here and in :ref:`config_report_exclude_also` are combined
623+
into one regex for processing, so you cannot use global flags like ``(?s)`` in
624+
your regexes. Use the scoped flag form instead: ``(?s:...)``
625+
620626

621627
.. _config_report_fail_under:
622628

doc/excluding.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,9 @@ region will be excluded. If part of the region introduces a block, the entire
248248
block is excluded even if part of it is outside the matched region.
249249

250250
When writing regexes to match multiple lines, remember that ``"."`` won't match
251-
a newline character, but ``"\n"`` or ``"(?s:.)"`` will. Using the ``"(?s)"``
252-
flag in your regex will also make dot match a newline.
251+
a newline character, but ``"\n"`` or ``"(?s:.)"`` will. The regexes in these
252+
settings are combined, so you cannot use global flags like ``(?s)`` in
253+
your regexes. Use the scoped flag form instead: ``(?s:...)``
253254

254255
Here are some examples:
255256

@@ -263,7 +264,7 @@ Here are some examples:
263264
; 2. Comments to turn coverage on and off:
264265
no cover: start(?s:.)*?no cover: stop
265266
; 3. A pragma comment that excludes an entire file:
266-
(?s)\A.*# pragma: exclude file.*\Z
267+
\A(?s:.*# pragma: exclude file.*)\Z
267268
""",
268269
toml=r"""
269270
[tool.coverage.report]
@@ -273,7 +274,7 @@ Here are some examples:
273274
# 2. Comments to turn coverage on and off:
274275
"no cover: start(?s:.)*?no cover: stop",
275276
# 3. A pragma comment that excludes an entire file:
276-
"(?s)\\A.*# pragma: exclude file.*\\Z",
277+
"\\A(?s:.*# pragma: exclude file.*)\\Z",
277278
]
278279
""",
279280
)
@@ -291,7 +292,7 @@ Here are some examples:
291292
; 2. Comments to turn coverage on and off:
292293
no cover: start(?s:.)*?no cover: stop
293294
; 3. A pragma comment that excludes an entire file:
294-
(?s)\A.*# pragma: exclude file.*\Z
295+
\A(?s:.*# pragma: exclude file.*)\Z
295296

296297
.. code-tab:: toml
297298
:caption: pyproject.toml
@@ -303,7 +304,7 @@ Here are some examples:
303304
# 2. Comments to turn coverage on and off:
304305
"no cover: start(?s:.)*?no cover: stop",
305306
# 3. A pragma comment that excludes an entire file:
306-
"(?s)\\A.*# pragma: exclude file.*\\Z",
307+
"\\A(?s:.*# pragma: exclude file.*)\\Z",
307308
]
308309

309310
.. code-tab:: ini
@@ -316,9 +317,9 @@ Here are some examples:
316317
; 2. Comments to turn coverage on and off:
317318
no cover: start(?s:.)*?no cover: stop
318319
; 3. A pragma comment that excludes an entire file:
319-
(?s)\A.*# pragma: exclude file.*\Z
320+
\A(?s:.*# pragma: exclude file.*)\Z
320321

321-
.. [[[end]]] (checksum: 22ff0a1433f00d3b4d13544623aaf884)
322+
.. [[[end]]] (checksum: ee3ef14b5a5d73f987b924df623a4927)
322323
323324
The first regex matches a specific except line followed by a specific function
324325
call. Both lines must be present for the exclusion to take effect. Note that
@@ -336,7 +337,7 @@ as possible, and you could accidentally exclude large swaths of code.
336337
The third regex matches the entire text of a file containing the comment ``#
337338
pragma: exclude file``. This lets you exclude files from coverage measurement
338339
with an internal comment instead of naming them in a settings file. This regex
339-
uses the ``"(?s)"`` regex flag to let a dot match any character including a
340+
uses the ``"(?s:...)"`` regex flag to let a dot match any character including a
340341
newline.
341342

342343

tests/test_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@ def my_function(args, j):
920920

921921
def test_multiline_exclusion_whole_source(self) -> None:
922922
# https://github.com/nedbat/coveragepy/issues/118
923-
regex = r"(?s)\A.*# pragma: exclude file.*\Z"
923+
regex = r"\A(?s:.*# pragma: exclude file.*)\Z"
924924
parser = self.parse_text("""\
925925
import coverage
926926
# pragma: exclude file

0 commit comments

Comments
 (0)