Skip to content

Commit 880a64a

Browse files
committed
fix: isolate user code from coverage.py internal code flags. #1524
1 parent 8fef6f0 commit 880a64a

File tree

9 files changed

+28
-8
lines changed

9 files changed

+28
-8
lines changed

Diff for: CHANGES.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ development at the same time, such as 4.5.x and 5.0.
2020
Unreleased
2121
----------
2222

23-
Nothing yet.
23+
- Fix: On Python 3.7, a file with type annotations but no ``from __future__
24+
import annotations`` would be missing statements in the coverage report. This
25+
is now fixed, closing `issue 1524`_.
26+
27+
.. _issue 1524: https://github.com/nedbat/coveragepy/issues/1524
2428

2529

2630
.. _changes_7-0-4:

Diff for: coverage/execfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def make_code_from_py(filename):
275275
except (OSError, NoSource) as exc:
276276
raise NoSource(f"No file to run: '{filename}'") from exc
277277

278-
return compile(source, filename, "exec")
278+
return compile(source, filename, "exec", dont_inherit=True)
279279

280280

281281
def make_code_from_pyc(filename):

Diff for: coverage/parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def __init__(
385385
else:
386386
assert filename is not None
387387
try:
388-
self.code = compile(text, filename, "exec")
388+
self.code = compile(text, filename, "exec", dont_inherit=True)
389389
except SyntaxError as synerr:
390390
raise NotPython(
391391
"Couldn't parse '%s' as Python source: '%s' at line %d" % (

Diff for: lab/genpy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def show_a_bunch():
231231
source = PythonSpinner.generate_python(maker.make_body("def"))
232232
try:
233233
print("-"*80, "\n", source, sep="")
234-
compile(source, "<string>", "exec")
234+
compile(source, "<string>", "exec", dont_inherit=True)
235235
except Exception as ex:
236236
print(f"Oops: {ex}\n{source}")
237237
if len(source) > len(longest):

Diff for: lab/parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def all_code_objects(code):
177177
def disassemble(pyparser):
178178
"""Disassemble code, for ad-hoc experimenting."""
179179

180-
code = compile(pyparser.text, "", "exec")
180+
code = compile(pyparser.text, "", "exec", dont_inherit=True)
181181
for code_obj in all_code_objects(code):
182182
if pyparser.text:
183183
srclines = pyparser.text.splitlines()

Diff for: lab/show_pyc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def show_py_file(fname):
4848
show_py_text(text, fname=fname)
4949

5050
def show_py_text(text, fname="<string>"):
51-
code = compile(text, fname, "exec")
51+
code = compile(text, fname, "exec", dont_inherit=True)
5252
show_code(code)
5353

5454
CO_FLAGS = [

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def better_set_verbosity(v):
5959
# Keep pylint happy.
6060
__version__ = __url__ = version_info = ""
6161
# Execute the code in version.py.
62-
exec(compile(version_file.read(), cov_ver_py, 'exec'))
62+
exec(compile(version_file.read(), cov_ver_py, 'exec', dont_inherit=True))
6363

6464
with open("README.rst") as readme:
6565
readme_text = readme.read()

Diff for: tests/test_cmdline.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def cmd_executes(
142142
code = textwrap.dedent(code)
143143
expected = self.model_object()
144144
globs = {n: getattr(expected, n) for n in self.MOCK_GLOBALS}
145-
code_obj = compile(code, "<code>", "exec")
145+
code_obj = compile(code, "<code>", "exec", dont_inherit=True)
146146
eval(code_obj, globs, {}) # pylint: disable=eval-used
147147

148148
# Many of our functions take a lot of arguments, and cmdline.py

Diff for: tests/test_summary.py

+16
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,22 @@ def missing(x, y):
849849
assert self.get_report(cov, output_format="total", precision=2) == "78.57\n"
850850
assert self.get_report(cov, output_format="total", precision=4) == "78.5714\n"
851851

852+
def test_bug_1524(self) -> None:
853+
self.make_file("bug1524.py", """\
854+
class Mine:
855+
@property
856+
def thing(self) -> int:
857+
return 17
858+
859+
print(Mine().thing)
860+
""")
861+
cov = coverage.Coverage()
862+
self.start_import_stop(cov, "bug1524")
863+
assert self.stdout() == "17\n"
864+
report = self.get_report(cov)
865+
report_lines = report.splitlines()
866+
assert report_lines[2] == "bug1524.py 5 0 100%"
867+
852868

853869
class ReportingReturnValueTest(CoverageTest):
854870
"""Tests of reporting functions returning values."""

0 commit comments

Comments
 (0)