python / python/mypy

Protocol with nested class

Aperta
#15,835 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug question topic-protocols
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug Report

Hey,

I don't know if this a bug report, feature request or simply a question about how to do it right 😄

For proper typing I need to overload the type annotation based on the argument types. One type involved is a Protocol. Everything works as expected as long as the Protocol consists of properties or method. But as soon as I'm looking for a class within the Protocol, mypy cannot find an overload variant. (See below)

(A clear and concise description of what the bug is.)

To Reproduce

This works:

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

This fails:

https://mypy-play.net/?mypy=latest&python=3.11&gist=0e424fbd22d314764a0d13ac9f8fe0b5

main.py:40: error: No overload variant of "create" of "Client" matches argument type "Content"  [call-overload]
main.py:40: note: Possible overload variants:
main.py:40: note:     def [_SupportsConfig <: SupportsConfig] create(self, content: _SupportsConfig) -> _SupportsConfig
main.py:40: note:     def [_SupportsConfig <: SupportsConfig] create(self, content: _SupportsConfig, result_class: None) -> _SupportsConfig
main.py:40: note:     def [_SupportsConfig <: SupportsConfig, _T] create(self, content: _SupportsConfig, result_class: type[_T]) -> _T

So, do I have to achieve my goal?

fin swimmer

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 confrontando le riproduzioni funzionanti e non funzionanti di mypy-play collegate nell’issue, usando come riferimento gli esempi Python 3.11 riportati e la diagnostica di overload. Traccia il modo in cui mypy gestisce un Protocol contenente properties o methods rispetto a una nested class, quindi stabilisci se il comportamento è un bug o un pattern non supportato e documenta il risultato.

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

Valutazione

Stack tecnologico
python
Ambito
tooling
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.