python / python/mypy

Overload decorator lines classified as Unanalyzed

Aperta
#21,638 5 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

@overload decorator lines are classified as Unanalyzed in --lineprecision-report. The lineprecision ratio (Precise + Imprecise) / (Lines - Empty) therefore drops with every overload added, even when all signatures are fully annotated.

To Reproduce

# mre.py
from typing import Literal, overload


class C:
    @overload
    def f(self, x: Literal["a"]) -> str: ...
    @overload
    def f(self, x: Literal["b"]) -> int: ...
    def f(self, x: Literal["a", "b"]) -> str | int:
        return "hello" if x == "a" else 42
$ mypy mre.py --lineprecision-report report/
Success: no issues found in 1 source file
$ cat report/lineprecision.txt
Name  Lines  Precise  Imprecise  Any  Empty  Unanalyzed
-------------------------------------------------------
mre      10        6          0    0      2           2

Expected Behavior

Every line is fully annotated and mypy reports no errors, so the ratio should be 8/8 = 100%. The 2 Unanalyzed lines are the two @overload decorator lines. overload is a well-typed symbol from typing; those lines should be Precise (or at minimum Empty), treating @overload as a pure annotation marker with no runtime value.

Actual Behavior

StatisticsVisitor.process_node looks up each expression in self.typemap. The NameExpr for @overload is not populated in the typemap (mypy treats it as a special form during semantic analysis), so typemap.get(node) returns Nonetype(None)record_line(TYPE_UNANALYZED).

Each overload variant therefore contributes 1 Precise line (the def) and 1 Unanalyzed line (@overload). Adding more fully-annotated overloads strictly lowers the ratio, making it impossible to hold a stable lineprecision gate on any file with a growing overload set.

There is a related # TODO in the same file: pass # TODO: Handle overloaded functions, etc. (in record_call_target_precision).

Your Environment

  • Mypy version used: 2.1.0
  • Mypy command-line flags: --lineprecision-report
  • Python version used: 3.13

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da StatisticsVisitor.process_node, dove l’issue segnala che le espressioni dei decorator vengono classificate tramite self.typemap, e verifica il TODO correlato record_call_target_precision nello stesso file. Esegui l’esempio mre.py fornito con --lineprecision-report, quindi verifica che i decorator di overload non compaiano più come Unanalyzed e che il report raggiunga 8/8 righe precise.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.