Skip to content

Commit 80a479d

Browse files
committed
fix: bug #1902
1 parent 8c2ea9a commit 80a479d

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

CHANGES.rst

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ upgrading your version of coverage.py.
2323
Unreleased
2424
----------
2525

26-
Nothing yet.
26+
- Fixed an obscure and mysterious problem on PyPy 3.10 seemingly involving
27+
mocks, imports, and trace functions: `issue 1902`_. To be honest, I don't
28+
understand the problem or the solution, but ``git bisect`` helped find it,
29+
and now it's fixed.
30+
31+
.. _issue 1902: https://github.com/nedbat/coveragepy/issues/1902
2732

2833

2934
.. start-releases

coverage/pytracer.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
Tracer,
2929
)
3030

31+
32+
# I don't understand why, but if we use `cast(set[TLineNo], ...)` inside
33+
# the _trace() function, we get some strange behavior on PyPy 3.10.
34+
# Assigning these names here and using them below fixes the problem.
35+
# See https://github.com/nedbat/coveragepy/issues/1902
36+
set_TLineNo = set[TLineNo]
37+
set_TArc = set[TArc]
38+
39+
3140
# We need the YIELD_VALUE opcode below, in a comparison-friendly form.
3241
# PYVERSIONS: RESUME is new in Python3.11
3342
RESUME = dis.opmap.get("RESUME")
@@ -253,9 +262,9 @@ def _trace(
253262
flineno: TLineNo = frame.f_lineno
254263

255264
if self.trace_arcs:
256-
cast(set[TArc], self.cur_file_data).add((self.last_line, flineno))
265+
cast(set_TArc, self.cur_file_data).add((self.last_line, flineno))
257266
else:
258-
cast(set[TLineNo], self.cur_file_data).add(flineno)
267+
cast(set_TLineNo, self.cur_file_data).add(flineno)
259268
self.last_line = flineno
260269

261270
elif event == "return":
@@ -286,7 +295,7 @@ def _trace(
286295
real_return = True
287296
if real_return:
288297
first = frame.f_code.co_firstlineno
289-
cast(set[TArc], self.cur_file_data).add((self.last_line, -first))
298+
cast(set_TArc, self.cur_file_data).add((self.last_line, -first))
290299

291300
# Leaving this function, pop the filename stack.
292301
self.cur_file_data, self.cur_file_name, self.last_line, self.started_context = (

0 commit comments

Comments
 (0)