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

Excluding multiple lines with exclude_lines #996

Closed
asmeurer opened this issue Jun 4, 2020 · 3 comments · Fixed by #1807
Closed

Excluding multiple lines with exclude_lines #996

asmeurer opened this issue Jun 4, 2020 · 3 comments · Fixed by #1807
Labels
enhancement New feature or request exclude

Comments

@asmeurer
Copy link

asmeurer commented Jun 4, 2020

As far as I can tell, it isn't posible to exclude multiple lines with exclude_lines. If I am reading the code correctly, it uses the lines_matching function, which checks the regexes line-by-line

def lines_matching(self, *regexes):
"""Find the lines matching one of a list of regexes.
Returns a set of line numbers, the lines that contain a match for one
of the regexes in `regexes`. The entire line needn't match, just a
part of it.
"""
combined = join_regex(regexes)
if env.PY2:
combined = combined.decode("utf8")
regex_c = re.compile(combined)
matches = set()
for i, ltext in enumerate(self.lines, start=1):
if regex_c.search(ltext):
matches.add(i)
return matches

My use-case is I want to exclude lines like

except ValueError: 
    assume(False)

where assume is from hypothesis. Simply excluding assume(False) doesn't work because it doesn't exclude the except. I don't want to exclude other except ValueError blocks in the codebase. I know I can rewrite this as except ValueError: assume(False) on a single line, but I would prefer not to as I don't like code that does that.

What I would like to do is to write something like

[report]
exclude_lines = 
    except ValueError:\n *assume\(False\)

Or if that can't work

[report]
exclude_lines = 
    except ValueError:(?=\n *assume\(False\))

Neither of these work presently. As far as I can tell this can only be excluded by manually putting # pragma: no cover on each instance.

@asmeurer asmeurer added the enhancement New feature or request label Jun 4, 2020
@nedbat
Copy link
Owner

nedbat commented Jun 7, 2020

This is an interesting idea. I'll have to think about how it could work....

@nedbat nedbat added the exclude label Nov 15, 2021
nedbat added a commit to devdanzin/coveragepy that referenced this issue Jul 4, 2024
nedbat added a commit that referenced this issue Jul 4, 2024
* Support excluding multiline regexes, O(N) algorithm by nedbat.

* Fix test_parser.py.

* Actually fix tests and mypy check.

* Format multiline exclusion tests.

* Apply suggestions from code review by @nedbat.

Co-authored-by: Ned Batchelder <[email protected]>

* Improve and add new tests to test_parser.py.

* Skip test_multiline_exclusion_block2 if Python version < 3.10.

* Add tests for exclusion of a whole module or from a marker until the end of the file.

* tweak the regexes in the tests

* test: add one more test for the specific #996 use

* docs: explain multi-line exclusion regexes

* build: the next version will be 7.6.0

* better: no whitespace in regexes

---------

Co-authored-by: Ned Batchelder <[email protected]>
@nedbat
Copy link
Owner

nedbat commented Jul 4, 2024

As of commit 1fe897d, you can do this with the multi-line exclusion regex you showed:

[report]
exclude_also =
    except ValueError:\n *assume\(False\)

@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 issue 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 issue 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 issue 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
enhancement New feature or request exclude
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants