Skip to content

Commit 0d64498

Browse files
committed
fix: 3.11.0b4 has 0-numbered lines. Fixes #1419
CPython added these lines in python/cpython@1bfe83a
1 parent 297b70e commit 0d64498

File tree

5 files changed

+38
-12
lines changed

5 files changed

+38
-12
lines changed

CHANGES.rst

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

23+
- Updated for a small change in Python 3.11.0 beta 4: modules now start with a
24+
line with line number 0, which is ignored. This line cannnot be executed, so
25+
coverage totals were thrown off. This line is now ignored by coverage.py,
26+
but this also means that truly empty modules (like ``__init__.py``) have no
27+
lines in them, rather than one phantom line. Fixes `issue 1419`_.
28+
2329
- Internal debugging data added to sys.modules is now an actual module, to
2430
avoid confusing code that examines everything in sys.modules. Thanks,
2531
Yilei Yang (`pull 1399`_).
2632

2733
.. _pull 1399: https://github.com/nedbat/coveragepy/pull/1399
34+
.. _issue 1419: https://github.com/nedbat/coveragepy/issues/1419
2835

2936

3037
.. _changes_6-4-1:

coverage/env.py

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class PYBEHAVIOR:
110110
# Some words are keywords in some places, identifiers in other places.
111111
soft_keywords = (PYVERSION >= (3, 10))
112112

113+
# Modules start with a line numbered zero. This means empty modules have
114+
# only a 0-number line, which is ignored, giving a truly empty module.
115+
empty_is_empty = (PYVERSION >= (3, 11, 0, 'beta', 4))
113116

114117
# Coverage.py specifics.
115118

coverage/parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def _line_numbers(self):
377377
"""
378378
if hasattr(self.code, "co_lines"):
379379
for _, _, line in self.code.co_lines():
380-
if line is not None:
380+
if line:
381381
yield line
382382
else:
383383
# Adapted from dis.py in the standard library.

tests/test_arcs.py

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ def test_what_is_the_sound_of_no_lines_clapping(self):
158158
if env.JYTHON:
159159
# Jython reports no lines for an empty file.
160160
arcz_missing=".1 1." # pragma: only jython
161+
elif env.PYBEHAVIOR.empty_is_empty:
162+
arcz_missing=".1 1."
161163
else:
162164
# Other Pythons report one line.
163165
arcz_missing=""

tests/test_lcov.py

+25-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from tests.coveragetest import CoverageTest
99

1010
import coverage
11+
from coverage import env
1112

1213

1314
class LcovTest(CoverageTest):
@@ -252,7 +253,8 @@ def test_empty_init_files(self):
252253
line. It will also note the lack of branches, and the checksum for
253254
the line.
254255
255-
Although there are no lines found, it will note one line as hit.
256+
Although there are no lines found, it will note one line as hit in
257+
old Pythons, and no lines hit in newer Pythons.
256258
"""
257259

258260
self.make_file("__init__.py", "")
@@ -261,15 +263,27 @@ def test_empty_init_files(self):
261263
self.start_import_stop(cov, "__init__")
262264
cov.lcov_report()
263265
self.assert_exists("coverage.lcov")
264-
expected_result = textwrap.dedent("""\
265-
TN:
266-
SF:__init__.py
267-
DA:1,1,1B2M2Y8AsgTpgAmY7PhCfg
268-
LF:0
269-
LH:1
270-
BRF:0
271-
BRH:0
272-
end_of_record
273-
""")
266+
# Newer Pythons have truly empty empty files.
267+
if env.PYBEHAVIOR.empty_is_empty:
268+
expected_result = textwrap.dedent("""\
269+
TN:
270+
SF:__init__.py
271+
LF:0
272+
LH:0
273+
BRF:0
274+
BRH:0
275+
end_of_record
276+
""")
277+
else:
278+
expected_result = textwrap.dedent("""\
279+
TN:
280+
SF:__init__.py
281+
DA:1,1,1B2M2Y8AsgTpgAmY7PhCfg
282+
LF:0
283+
LH:1
284+
BRF:0
285+
BRH:0
286+
end_of_record
287+
""")
274288
actual_result = self.get_lcov_report_content()
275289
assert actual_result == expected_result

0 commit comments

Comments
 (0)