python / python/mypy

Overload decorator lines classified as Unanalyzed

Offen
#21,638 5 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
54

Beschreibung

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

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 StatisticsVisitor.process_node, wo das Issue berichtet, dass Decorator-Ausdrücke über self.typemap klassifiziert werden, und prüfe das zugehörige record_call_target_precision TODO in derselben Datei. Führe das bereitgestellte mre.py-Beispiel mit --lineprecision-report aus und überprüfe anschließend, dass die Overload-Decorator nicht mehr als Unanalyzed erscheinen und der Bericht 8/8 präzise Zeilen erreicht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
68/100

Neue Issues direkt in Ihr Postfach

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