python / python/mypy

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

Aperta
#16,200 3 commenti 0 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

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

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 con il reproducer mypy-play collegato e l'output di reveal_type in main.py, confrontando i casi Callable e Protocol. Traccia la gestione del binding dei metodi per i tipi Protocol call e aggiungi un test di regressione che copra i metodi non associati e associati. Il lavoro è completato quando il metodo associato decorato con Protocol omette self, come nel comportamento di Callable.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.