Python: calls to a typing.overload'ed function link to the first @overload stub, not the implementation

Offen
#2,963 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

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

Rechercherichtung

Reproduce with the supplied pkg/deco.py and use.py snippets, then inspect the emitted calls edges for command and Prompt.ask; the expected targets are the implementation defs at lines 12 and 30, not the first @overload stubs. Start by locating the Python call-resolution path that maps a call to an indexed definition, and check how it treats multiple defs sharing a name and decorators such as @classmethod. Done means the example’s callers and impact reach the implementation, the run to real_work path is intact, and Python resolution tests cover overload plus classmethod overload cases.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

Calls to a Python function or method declared with typing.overload link to the first @overload stub, whose body is just .... The real implementation, the undecorated def that runs, gets no callers, even though its body holds all the callees.

# pkg/deco.py
import typing as t


@t.overload
def command(name: t.Callable) -> int: ...          # line 5


@t.overload
def command(name: str | None = None) -> str: ...   # line 9


def command(name=None):                            # line 12, the implementation
    return real_work(name)


def real_work(n):
    return n


class Prompt:
    @t.overload
    @classmethod
    def ask(cls, p: str) -> str: ...               # line 23

    @t.overload
    @classmethod
    def ask(cls, p: str, d: int) -> int: ...

    @classmethod
    def ask(cls, p, d=None):                       # line 30, the implementation
        return real_work(p)

# pkg/__init__.py
from .deco import command, Prompt

# use.py
import pkg
from pkg import command, Prompt


@pkg.command()
def cli():
    pass


def run():
    command("x")
    Prompt.ask("y")
call edge on main expected
@pkg.command() command at line 5 (stub) line 12
command("x") command at line 5 (stub) line 12
Prompt.ask("y") ask at line 23 (stub) line 30

All six defs are indexed as separate nodes. The stubs have no callees, and the implementations have the callees but no callers. So callers/impact on the implementation come back empty, the call path from run to real_work breaks at the stub, and the implementation looks unreferenced.

Reproduced on main at 72f398af, with CODEGRAPH_KERNEL=0 and with the native kernel. Linux/WSL2, Node 22.23.2.

Where it shows up. Libraries that type their public API with overloads are hit hardest. Checking every Python calls edge against jedi's goto-definition:

  • In click (2247b35), command in src/click/decorators.py has stubs at lines 138, 144, 153 and 163 and the implementation at 168. 345 call edges land on the line-138 stub and 2 on the implementation. Most of click's 473 disagreeing import edges are this (group, edit, progressbar, prompt, convert_type, …).
  • In rich (9d8f9a3), Confirm.ask / Prompt.ask and rich.progress.open land on their first stub the same way.

Expected. For a Python name with @overload stubs plus an implementation, link calls to the implementation (the def without @overload). If a stub is kept for its signature, the implementation should still be the callee. #2213 ("any call reaches the overload its arguments fit") covers languages whose overloads are separate implementations. A Python call always runs the one undecorated def.

Related: #2213.

Vorherrschende Sprache
C
Sterne
73.2k
Forks
4.7k
Ø Merge
3 Std. 37 Min.
Gemergte PRs (30 T.)
533

Entwicklungsumgebung

Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus colbymchenry/codegraph

Alle Issues in colbymchenry/codegraph

Ähnliche Issues

Weitere Issues zu C

Neue Issues direkt in Ihr Postfach

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