Skip to content

Commit f4b2b4d

Browse files
mokashangnedbat
andauthored
fix: exclude ... bodies after multi-line return-type annotations (#2185) (#2196)
The default exclusion regex for ``...`` function bodies expected the def signature to end with ``)``. When a formatter (e.g. Ruff) splits a long return-type annotation across multiple lines, the last line of the signature ends with ``]`` instead — for example:: def f() -> dict[ str, str ]: ... In that shape coverage.py reported the function body as a missing line instead of excluding it. Allow either ``)`` or ``]`` (and let them chain, e.g. ``])``) before the optional ``-> return-type`` and the trailing ``: ...`` so the exclusion applies regardless of how the signature is wrapped. This also keeps working: standalone ``...``, single-line ``def ...: ...`` declarations, and existing multi-line ``def`` signatures that close on a line by themselves. Adds a regression test exercising single-line, split-return, split- param-and-return, and split-param-with-inner-``Tuple[..., ...]`` shapes. Co-authored-by: Ned Batchelder <[email protected]>
1 parent 1980ed0 commit f4b2b4d

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎CHANGES.rst‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26-
Nothing yet.
26+
- Fix: the default ``...`` exclusion rule now also matches function bodies
27+
whose closing return-type bracket is on its own line (for example, after a
28+
long ``-> dict[ ... ]`` annotation that a formatter has split over multiple
29+
lines). Closes `issue 2185`_.
30+
31+
.. _issue 2185: https://github.com/coveragepy/coveragepy/issues/2185
2732

2833

2934
.. start-releases
@@ -58,6 +63,7 @@ Version 7.14.2 — 2026-06-20
5863
.. _issue 2197: https://github.com/coveragepy/coveragepy/issues/2197
5964
.. _no-13t: https://py-free-threading.github.io/ci/#building-free-threaded-wheels-with-cibuildwheel
6065

66+
6167
.. _changes_7-14-1:
6268

6369
Version 7.14.1 — 2026-05-26

‎coverage/config.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def getregexlist(self, section: str, option: str) -> list[str]:
149149
# The default line exclusion regexes.
150150
DEFAULT_EXCLUDE = [
151151
r"#\s*(pragma|PRAGMA)[:\s]?\s*(no|NO)\s*(cover|COVER)",
152-
r"^\s*(((async )?def .*?)?\)(\s*->.*?)?:\s*)?\.\.\.\s*(#|$)",
152+
r"^\s*(((async )?def .*?)?[\])]+(\s*->.*?)?:\s*)?\.\.\.\s*(#|$)",
153153
r"if (typing\.)?TYPE_CHECKING:",
154154
]
155155

‎tests/test_coverage.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,36 @@ def f28(): print("(well): ... #2 false positive!")
15031503
lines=[1, 3, 5, 7, 9, 11, 19, 24, 25],
15041504
)
15051505

1506+
def test_default_ellipsis_multiline_return_type(self) -> None:
1507+
# Issue #2185: the default `...` exclusion should still apply when the
1508+
# return type annotation is split over multiple lines (a common shape
1509+
# produced by formatters like Ruff when the annotation is long).
1510+
# The trailing block also pins down what should NOT be excluded by
1511+
# the widened pattern: a `...`-valued annotated assignment, and a
1512+
# def whose return type contains `]` but has a real body.
1513+
self.check_coverage(
1514+
"""\
1515+
a = 1
1516+
def short() -> dict[str, str]: ...
1517+
def split_return() -> dict[
1518+
str, str
1519+
]: ...
1520+
def split_param_and_return(
1521+
arg: int,
1522+
) -> dict[
1523+
str, str
1524+
]: ...
1525+
def split_param_with_inner_ellipsis(arg: tuple[
1526+
int, ...
1527+
]) -> int: ...
1528+
# Not stubs, and so not excluded:
1529+
x: list[int] = ...
1530+
def with_body() -> dict[str, str]: return {}
1531+
z = 17
1532+
""",
1533+
lines=[1, 15, 16, 17],
1534+
)
1535+
15061536
def test_two_excludes(self) -> None:
15071537
self.check_coverage(
15081538
"""\

0 commit comments

Comments
 (0)