python / python/mypy

`get_function_signature_hook` is not invoked on function decorators

Offen
#21,904 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-plugins
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

It seems to me there is no way to intercept and change the signature of a decorator during the invocation.

I see that get_function_hook has been called for foo.deco (eventually allowing me to change the return type of the decorator) but get_function_signature_hook is missing

To Reproduce

very basic example:

foo.py

from collections.abc import Callable
def deco(fun: Callable[..., None]) -> None: ...
@deco
def fun() -> None: ...

plug.py

from mypy.plugin import *
from mypy.types import *
from collections.abc import Callable

class CustomPlugin(Plugin):
    def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
        print(f'get_function_hook - {fullname=}')
        return None

    def get_function_signature_hook(self, fullname: str) -> Callable[[FunctionSigContext], FunctionLike] | None:
        print(f'get_function_signature_hook - {fullname=}')
        return None

def plugin(_version: str, /) -> type[Plugin]:
    return CustomPlugin

If I run mypy with the plugin and capture the stdout I get (avoiding the duplicates):

get_function_hook - fullname='enum.auto'
get_function_hook - fullname='foo.deco'
get_function_hook - fullname='<list>'
get_function_hook - fullname='typing_extensions.disjoint_base'
get_function_hook - fullname='typing_extensions.final'
get_function_hook - fullname='typing.final'
get_function_hook - fullname='typing.runtime_checkable'
get_function_hook - fullname='typing.type_check_only'
get_function_hook - fullname='warnings.deprecated'
get_function_signature_hook - fullname='enum.auto'
get_function_signature_hook - fullname='warnings.deprecate

Expected Behavior

get_function_signature_hook is called also for foo.deco

Actual Behavior

it's missing

Your Environment

mypy 2.3.1 (compiled: yes)
Python 3.14.0

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

Führe mypy für die foo.py-Reproduktion mit dem Plugin in plug.py aus und vergleiche die Aufrufe von get_function_hook und get_function_signature_hook für foo.deco. Verfolge den Plugin-Dispatch-Pfad für den Aufruf des Decorators und ergänze oder aktualisiere die Testabdeckung so, dass der Signature-Hook für foo.deco aufgerufen wird; fertig ist es, wenn der erwartete Callback erscheint, ohne das bestehende Hook-Verhalten zu beeinträchtigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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