coverage of multiline generator/set/dict comprehensions is wrong when run with pytest's assertion rewriting
See original GitHub issueOriginally 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:
- Created 7 years ago
- Comments:26 (24 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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