[@overload]: What is the effect of the *implementation* type-signature?
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 1.8k
- Forks
- 302
- Merge moyen
- 23 h
- PR mergées (30 j)
- 8
Description
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.
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par les exemples de @overload dans cette issue et exécutez-les dans le Pyright playground indiqué, puis comparez les résultats avec mypy. Examinez comment les deux outils interprètent les signatures d’implémentation et les listes d’overloads. Le travail sera terminé lorsqu’une explication concertée de la sémantique aura été établie et que le comportement attendu ainsi que les exemples auront été documentés.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- documentation
- Type d'issue
- Documentation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100