Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support excluding multiline regexes (fix #996) #1807

Merged
merged 14 commits into from
Jul 4, 2024

Conversation

devdanzin
Copy link
Contributor

This PR allows using multiline regexes in exclude_lines. It does so by matching regexes against the whole source code, instead of line by line as was done before.

The behavior is equivalent to adding a # pragma: no cover comment to each line matched by the multiline regex. Because of that, if a suite (e.g. if True:) is matched, all statements inside it will also be excluded, just like if there was a no-cover pragma on the suite line.

This is made possible because @nedbat figured out a O(N) algorithm for finding line numbers for the matches. With it, there's a small performance increase for excluding lines compared to the previous code.

Fixes #996.

tests/test_parser.py Outdated Show resolved Hide resolved
Comment on lines 786 to 789
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent the code strings like this:

Suggested change
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)

This should match the way other code is included in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Done.

tests/test_parser.py Outdated Show resolved Hide resolved
tests/test_parser.py Outdated Show resolved Hide resolved
tests/test_parser.py Outdated Show resolved Hide resolved
tests/test_parser.py Outdated Show resolved Hide resolved
tests/test_parser.py Outdated Show resolved Hide resolved
tests/test_parser.py Show resolved Hide resolved
tests/test_parser.py Show resolved Hide resolved
tests/test_parser.py Outdated Show resolved Hide resolved
assert parser.statements == set()

def test_multiline_exclusion_multiple_matches(self) -> None:
regex = r"print\('.*'\)"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a multi-line regex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that was just a sanity check from when I was sketching this that slipped through. I'll rewrite the test so it covers multiple multi-line matches.

tests/test_parser.py Outdated Show resolved Hide resolved
@nedbat
Copy link
Owner

nedbat commented Jul 3, 2024

We should also add a test for the #1803 example.

@nedbat
Copy link
Owner

nedbat commented Jul 3, 2024

Do you want to update the docs, or leave that to me?

@nedbat
Copy link
Owner

nedbat commented Jul 3, 2024

When this pull request is merged, we should update issues #118, #996, #1741, and #1803.

@devdanzin
Copy link
Contributor Author

Do you want to update the docs, or leave that to me?

No, I'd rather let you do your thing with the docs. But thanks for asking :)

@nedbat nedbat merged commit 1fe897d into nedbat:master Jul 4, 2024
35 checks passed
@nedbat
Copy link
Owner

nedbat commented Jul 11, 2024

This is now released as part of coverage 7.6.0.

github-merge-queue bot pushed a commit to rustymotors/server that referenced this pull request Jul 11, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [coverage](https://github.com/nedbat/coveragepy) | `==7.5.4` ->
`==7.6.0` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>nedbat/coveragepy (coverage)</summary>

###
[`v7.6.0`](https://github.com/nedbat/coveragepy/blob/HEAD/CHANGES.rst#Version-760--2024-07-11)

[Compare
Source](https://github.com/nedbat/coveragepy/compare/7.5.4...7.6.0)

- Exclusion patterns can now be multi-line, thanks to `Daniel Diniz
<pull
1807_>`*. This enables many interesting exclusion use-cases, including
those
requested in issues `118 <issue 118_>`* (entire files), `996 <issue
996_>`\_ (multiple lines only when appearing together), `1741 <issue
1741_>`\_ (remainder of a function), and `1803 <issue 1803_>`\_
(arbitrary sequence of marked lines). See the :ref:`multi_line_exclude`
    section of the docs for more details and examples.

- The JSON report now includes per-function and per-class coverage
information.
Thanks to `Daniel Diniz <pull 1809_>`\_ for getting the work started.
This
    closes `issue 1793`\_ and `issue 1532`\_.

- Fixed an incorrect calculation of "(no class)" lines in the HTML
classes
    report.

-   Python 3.13.0b3 is supported.

.. \_issue
118:[nedbat/coveragepy#118
.. \_issue
996[nedbat/coveragepy#996
.. \_issue
153[nedbat/coveragepy#1532
.. \_issue
17[nedbat/coveragepy#1741
.. \_issue
1[nedbat/coveragepy#1793
.. \_issue
[nedbat/coveragepy#1803
..
\_pull[nedbat/coveragepy#1807
..
\_pul[nedbat/coveragepy#1809

.. \_changes\_7-5-4:

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/rustymotors/server).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
renovate bot added a commit to allenporter/flux-local that referenced this pull request Jul 11, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [coverage](https://github.com/nedbat/coveragepy) | `==7.5.4` ->
`==7.6.0` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>nedbat/coveragepy (coverage)</summary>

###
[`v7.6.0`](https://github.com/nedbat/coveragepy/blob/HEAD/CHANGES.rst#Version-760--2024-07-11)

[Compare
Source](https://github.com/nedbat/coveragepy/compare/7.5.4...7.6.0)

- Exclusion patterns can now be multi-line, thanks to `Daniel Diniz
<pull
1807_>`*. This enables many interesting exclusion use-cases, including
those
requested in issues `118 <issue 118_>`* (entire files), `996 <issue
996_>`\_ (multiple lines only when appearing together), `1741 <issue
1741_>`\_ (remainder of a function), and `1803 <issue 1803_>`\_
(arbitrary sequence of marked lines). See the :ref:`multi_line_exclude`
    section of the docs for more details and examples.

- The JSON report now includes per-function and per-class coverage
information.
Thanks to `Daniel Diniz <pull 1809_>`\_ for getting the work started.
This
    closes `issue 1793`\_ and `issue 1532`\_.

- Fixed an incorrect calculation of "(no class)" lines in the HTML
classes
    report.

-   Python 3.13.0b3 is supported.

.. \_issue
118:[nedbat/coveragepy#118
.. \_issue
996[nedbat/coveragepy#996
.. \_issue
153[nedbat/coveragepy#1532
.. \_issue
17[nedbat/coveragepy#1741
.. \_issue
1[nedbat/coveragepy#1793
.. \_issue
[nedbat/coveragepy#1803
..
\_pull[nedbat/coveragepy#1807
..
\_pul[nedbat/coveragepy#1809

.. \_changes\_7-5-4:

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/allenporter/flux-local).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
jooola pushed a commit to hetznercloud/hcloud-python that referenced this pull request Jul 12, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [coverage](https://github.com/nedbat/coveragepy) | `>=7.5,<7.6` ->
`>=7.6,<7.7` |
[![age](https://developer.mend.io/api/mc/badges/age/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/pypi/coverage/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/coverage/7.5.4/7.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>nedbat/coveragepy (coverage)</summary>

###
[`v7.6.0`](https://github.com/nedbat/coveragepy/blob/HEAD/CHANGES.rst#Version-760--2024-07-11)

[Compare
Source](https://github.com/nedbat/coveragepy/compare/7.5.4...7.6.0)

- Exclusion patterns can now be multi-line, thanks to `Daniel Diniz
<pull
1807_>`*. This enables many interesting exclusion use-cases, including
those
requested in issues `118 <issue 118_>`* (entire files), `996 <issue
996_>`\_ (multiple lines only when appearing together), `1741 <issue
1741_>`\_ (remainder of a function), and `1803 <issue 1803_>`\_
(arbitrary sequence of marked lines). See the :ref:`multi_line_exclude`
    section of the docs for more details and examples.

- The JSON report now includes per-function and per-class coverage
information.
Thanks to `Daniel Diniz <pull 1809_>`\_ for getting the work started.
This
    closes `issue 1793`\_ and `issue 1532`\_.

- Fixed an incorrect calculation of "(no class)" lines in the HTML
classes
    report.

-   Python 3.13.0b3 is supported.

.. \_issue
118:[nedbat/coveragepy#118
.. \_issue
996[nedbat/coveragepy#996
.. \_issue
153[nedbat/coveragepy#1532
.. \_issue
17[nedbat/coveragepy#1741
.. \_issue
1[nedbat/coveragepy#1793
.. \_issue
[nedbat/coveragepy#1803
..
\_pull[nedbat/coveragepy#1807
..
\_pul[nedbat/coveragepy#1809

.. \_changes\_7-5-4:

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/hetznercloud/hcloud-python).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQyNS4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Excluding multiple lines with exclude_lines
2 participants