question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

coverage of multiline generator/set/dict comprehensions is wrong when run with pytest's assertion rewriting

See original GitHub issue

Originally reported by Andy Freeland (Bitbucket: rouge8, GitHub: rouge8)


Code/config to reproduce available as a gist. Fails on Python 2.7 but not Python 3.5.

Essentially, given this test:

def test_foo():
    # covered!
    assert {i for i in range(10)} == {i for i in range(10)}

    # "didn't finish the set comprehension"
    assert {i for i in range(10)} == {
        i for i in range(10)
    }

    # covered!
    assert True

When run under pytest with assertion rewriting (the default), the multiline set comprehension is reported as partially covered, even though the comprehension on oneline is fully covered. I think this is a bug in coverage, not pytest’s assertion rewriting, because this code passes:

import ast
from _pytest.assertion.rewrite import rewrite_asserts

oneline = """assert {i for i in range(10)} == {i for i in range(10)}"""
multiline = """assert {i for i in range(10)} == {
    i for i in range(10)
}"""

# Parse the expressions
oneline_tree = ast.parse(oneline)
multiline_tree = ast.parse(multiline)

# Dump the pre-assertion rewrite ASTs
multiline_dump_prerewrite = ast.dump(multiline_tree)
oneline_dump_prerewrite = ast.dump(oneline_tree)

# The ASTs should be the same
assert multiline_dump_prerewrite == oneline_dump_prerewrite

# Rewrite the asserts
rewrite_asserts(oneline_tree)
rewrite_asserts(multiline_tree)

# Dump the rewritten ASTs
oneline_dump_rewrite = ast.dump(oneline_tree)
multiline_dump_rewrite = ast.dump(multiline_tree)

# The ASTs should be the same
assert oneline_dump_rewrite == multiline_dump_rewrite

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:26 (24 by maintainers)

github_iconTop GitHub Comments

1reaction
nicoddemuscommented, Jun 3, 2019

We are about to revert the unroll functionality for all() as we have found a bunch of corner cases. We should release 4.6.2 later today.

0reactions
rouge8commented, Jun 3, 2019

This now shows up in even more cases with all() in the latest pytest, likely due to https://github.com/pytest-dev/pytest/pull/5360

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overusing list comprehensions and generator expressions in ...
Note: In this article, I'll be using the term “comprehension” to refer to all forms of comprehensions (list, set, dict) as well as...
Read more >
Changelog — pytest documentation
running pytest tests now imports the conftest twice, once as tests/real/conftest.py and once as ... #9521: Add test coverage to assertion rewrite path....
Read more >
Python Practice Problems: Parsing CSV Files
In this tutorial, you'll prepare for future interviews by working through a set of Python practice problems that involve CSV files. You'll work...
Read more >
2.6 PDF - Pylint Documentation
When Pylint is first run on a fresh piece of code, a common complaint ... Loop variables in list comprehensions and generator expressions....
Read more >
Changelog — Hypothesis 6.60.0 documentation
3 - 2021-05-23¶. This patch adds a .hypothesis property to invalid test functions, bringing them inline with valid tests and fixing a bug...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found