python / python/cpython

some instructions in __annotate__ have incorrect code positions

Offen
#135,700 4 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

interpreter-core topic-typing type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

The following code shows the incorrect positions:

from dis import Bytecode

src = """\
22
333
__dataclass_fields__: ClassVar
"""

code = compile(src, "<string>", "exec")
print(code.co_consts)
annotate_code = code.co_consts[1]

for bc in Bytecode(annotate_code):
    print(bc.positions, bc.opname)

output (Python 3.14.0b3+):

(0, <code object __annotate__ at 0x7fcda674f940, file "<string>", line 1>, None)
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=0) RESUME       
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) LOAD_FAST_BORROW         # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) LOAD_SMALL_INT           # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) COMPARE_OP               # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) POP_JUMP_IF_FALSE        # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) NOT_TAKEN                # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) LOAD_COMMON_CONSTANT     # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) RAISE_VARARGS            # incorrect
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) BUILD_MAP                # incorrect
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) LOAD_SMALL_INT
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) LOAD_GLOBAL
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) CONTAINS_OP
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) POP_JUMP_IF_FALSE
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) NOT_TAKEN
Positions(lineno=3, end_lineno=3, col_offset=22, end_col_offset=30) LOAD_GLOBAL
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) COPY
Positions(lineno=3, end_lineno=3, col_offset=0, end_col_offset=30) LOAD_CONST
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) STORE_SUBSCR
Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=2) RETURN_VALUE

The statements after RESUME have the position of the first ast-node in the module. I think it would be correct if they had the same code position as RESUME. I assume they are part of a general code that is part of every __annotate__ and are not associated with a specific ast-node.

I hope this can be fixed, as it makes it difficult for projects like executing to work with these code locations.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs
  • gh-135814
  • gh-136543

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem bereitgestellten Reproducer für Python 3.14 unter Verwendung von dis.Bytecode und untersuche, wie Codepositionen für annotate zugewiesen werden. Vergleiche die Anweisungen nach RESUME mit dem im Bericht beschriebenen erwarteten Verhalten der Positionen; abgeschlossen ist die Aufgabe, wenn die allgemeinen Anweisungen nicht mehr die Position des ersten AST-Knotens übernehmen. Die verknüpften PRs gh-135814 und gh-136543 zeigen, dass diese Arbeit bereits im Gange ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.