python / python/mypy

hinting method decorators with protocols fails to remove the self attribute when binding

Offen
#16,200 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

When hinting a decorator used on methods using Callable, the unbound method and bound method types differ by the first/self parameter as I expect. When I replace the Callable with a Protocol providing .__call__() instead, the unbound and bound cases are the same.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=1046c1e56c21fd17cda1ecab5e9e131b

import typing


def d(f: typing.Callable[[C, int], str]) -> typing.Callable[[C, int], str]:
    def inner(self, x: int) -> str:
        return f(self, x)
    
    return inner

class C:
    @d
    def m(self, x: int) -> str:
        return ""

reveal_type(C.m)
reveal_type(C().m)


class P(typing.Protocol):
    def __call__(protocol_self, self: CP, x: int) -> str:
        ...

def dp(f: P) -> P:
    def inner(self, x: int) -> str:
        return f(self, x)
    
    return inner

class CP:
    @dp
    def m(self, x: int) -> str:
        return ""

reveal_type(CP.m)
reveal_type(CP().m)

Expected Behavior

I don't know the details around 'modifying' protocols such as would be required here but I would expect that unbound and bound would not be the same.

Actual Behavior

Unbound and bound methods decorated with a decorator hinted with protocols have the same type.

main.py:15: note: Revealed type is "def (__main__.C, builtins.int) -> builtins.str"
main.py:16: note: Revealed type is "def (builtins.int) -> builtins.str"
main.py:34: note: Revealed type is "__main__.P"
main.py:35: note: Revealed type is "__main__.P"
Success: no issues found in 1 source file

Your Environment

  • Mypy version used: 1.5.1, master
  • Mypy command-line flags: mypy-play defaults
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11

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 verknüpften mypy-play-Reproducer und der reveal_type-Ausgabe in main.py und vergleiche dabei die Callable- und Protocol-Fälle. Verfolge die Behandlung der Methodenbindung für Protocol-call-Typen und füge einen Regressionstest hinzu, der ungebundene und gebundene Methoden abdeckt. Als erledigt gilt die Aufgabe, wenn bei der gebundenen Protocol-dekorierten Methode self weggelassen wird, entsprechend dem Verhalten von Callable.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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