[@overload]: What is the effect of the *implementation* type-signature?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 1.8k
- Forks
- 302
- Ø Merge
- 23 Std.
- Gemergte PRs (30 T.)
- 8
Beschreibung
One thing that I've found underspecified when using @overload is what the type signature of the actual function should be.
For example, let's say I have a function like:
from typing import overload, Literal
@overload
def f(x: int, z: Literal[True]) -> str: ...
@overload
def f(x: int, z: Literal[False] = ...) -> int: ...
def f(x: int, z: bool = False) -> str | int:
if z:
return "hello"
return 1
What is the effect of the last line? In particular:
- The documentation and examples often just use no annotations for the implementation signature (
def f(x, z):). Is that the right thing to do (I sometimes getOverloaded implementation is not consistent with signature of overload 1errors if I leave out the type annotations)? - If I do specify types on the last, do I also have to add it to
@overloadlist -- in other words:
@overload
def f(x: int, z: Literal[True]) -> str: ...
@overload
def f(x: int, z: Literal[False] = ...) -> int: ...
@overload
def f(x: int, z: bool = ...) -> str | int # is this overload necessary?
def f(x: int, z: bool = False) -> str | int:
if z:
return "hello"
return 1
Both pyright and mypy seem to interpret things differently with and without it (e.g. see this play link).
I understand that @overload is a complicated feature, but I'm hoping this is a small corner we can start with.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
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
Beginne mit den @overload-Beispielen in diesem Issue und führe sie im verlinkten Pyright playground aus. Vergleiche anschließend die Ergebnisse mit mypy. Untersuche, wie die beiden Tools Implementierungssignaturen und Overload-Listen interpretieren. Die Arbeit ist abgeschlossen, wenn eine abgestimmte Erklärung der Semantik erreicht und das erwartete Verhalten sowie die Beispiele dokumentiert wurden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- documentation
- Issue-Typ
- Dokumentation
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Muss geklärt werden
- Anfängerfreundlichkeit
- 25/100