Skip to content

Commit 54466ad

Browse files
emerybergerclaude
andcommitted
Fix annotation detection for Python < 3.11
The instr.positions attribute was added in Python 3.11. For older versions, use instr.starts_line to track current line number. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bb80611 commit 54466ad

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/slipcover/slipcover.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,25 @@ def _get_annotation_only_lines(co: types.CodeType) -> frozenset:
6262

6363
# Collect opcodes per line
6464
ops_by_line: dict = {}
65+
current_line = None
6566
for instr in dis.get_instructions(co):
66-
if instr.positions and instr.positions.lineno:
67-
line = instr.positions.lineno
68-
if line not in ops_by_line:
69-
ops_by_line[line] = set()
70-
ops_by_line[line].add(instr.opname)
67+
# Python 3.11+ has instr.positions.lineno for every instruction
68+
# Python < 3.11 has instr.starts_line only for first instruction on each line
69+
if sys.version_info >= (3, 11):
70+
if instr.positions and instr.positions.lineno:
71+
line = instr.positions.lineno
72+
else:
73+
continue
74+
else:
75+
if instr.starts_line is not None:
76+
current_line = instr.starts_line
77+
line = current_line
78+
if line is None:
79+
continue
80+
81+
if line not in ops_by_line:
82+
ops_by_line[line] = set()
83+
ops_by_line[line].add(instr.opname)
7184

7285
# Find lines where ALL ops are annotation-only ops
7386
annotation_lines = set()

0 commit comments

Comments
 (0)