Signature inference for special methods
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
- Avoid mistakes in annotating these methods https://github.com/sagemath/sage/issues/41500
- Less code to read, less mental overhead
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es werden im Issue keine Quelldateien oder Tests genannt. Beginne damit nachzuverfolgen, wie mypy Signaturen für spezielle Methoden ableitet und wie check-untyped-defs konfiguriert ist; vergleiche das Verhalten von automatic-signature-special-methods und check-untyped-special-methods. Als abgeschlossen gilt die Definition der unterstützten speziellen Methoden und des Zusammenspiels der Konfiguration, wobei die Rückgabetypprüfung anhand der Beispiele demonstriert wird.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100