python / python/mypy

Signature inference for special methods

Aperta
#20,647 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

We have the following

class A:
    def __contains__(self, x: int) -> bool:
        return None  # incompatible return value type

class B:
    def __contains__(self, x: int):
        return None  # no error?

Currently, in order to type-check the return type of special methods such as __contains__ or __len__, it is necessary to explicitly type-annotate them, even though it ought to be "obvious" i.e. bool and int respectively.

Suggestion: Add a configuration like automatic-signature-special-methods that will behave as if every special methods are annotated with the correct types. Together with check-untyped-defs (or maybe a lighter configuration like check-untyped-special-methods)

Note that currently Python official documentation has this statement, regarding type annotation for special methods (https://typing.python.org/en/latest/spec/annotations.html)

(Note that the return type of init ought to be annotated with -> None. The reason for this is subtle. If init assumed a return annotation of -> None, would that mean that an argument-less, un-annotated init method should still be type-checked? Rather than leaving this ambiguous or introducing an exception to the exception, we simply say that init ought to have a return annotation; the default behavior is thus the same as for other methods.)

The advantages are

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

Nell’issue non sono indicati file sorgente né test. Inizia tracciando il modo in cui mypy inferisce le firme dei metodi speciali e come è configurato check-untyped-defs; confronta i comportamenti di automatic-signature-special-methods e check-untyped-special-methods. Il lavoro è considerato completato quando sono definiti i metodi speciali supportati e l’interazione della configurazione, con la verifica dei tipi restituiti dimostrata per gli esempi.

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

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
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.