[spec] overload subtyping rules are too strict.
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 35/100
Piste de recherche
Commencez par les règles de sous-typage des overloads dans la section consacrée aux callables de la spécification de typing et examinez la discussion associée dans typing/discussions/1782. Comparez les règles énoncées avec l’exemple IntScalarOurs et IntScalarTheirs, puis déterminez si la spécification nécessite une révision de principe ou une règle clarifiée de condition suffisante.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
The typing spec states that, when A and B are potentially overloaded methods
If a callable
Bis overloaded with two or more signatures, it is assignable to callableAif at least one of the overloaded signatures inBis assignable toAIf a callable
Ais overloaded with two or more signatures, callableBis assignable toAifBis assignable to all of the signatures inA
However, this definition seems to be too strict. Consider the following example:
from typing import Protocol, Self, overload
class IntScalarOurs(Protocol):
def __add__(self, other: int | Self, /) -> Self: ...
class IntScalarTheirs(Protocol):
@overload
def __add__(self, other: int, /) -> Self: ...
@overload
def __add__(self, other: Self, /) -> Self: ...
These two protocols specify the exact same runtime behavior. Yet, following the rules of the spec, IntScalarOurs is assignable to IntScalarTheirs, but IntScalarTheirs is not assignable to IntScalarOurs.
Case 1: A=IntScalarOurs, B=IntScalarTheirs.
If a callable B is overloaded with two or more signatures, it is assignable to callable A if at least one of the overloaded signatures in B is assignable to A
- Is
(self, int) -> Selfassignable to(self, int | Self) -> Self? No, becauseintis not a supertype ofint | Self(contravariance) - Is
(self, Self) -> Selfassignable to(self, int | Self) -> Self? No, becauseSelfis not a supertype ofint | Self(contravariance)
Thus, IntScalarTheirs is not assignable to IntScalarOurs.
Case 2: A=IntScalarTheirs, B=IntScalarOurs.
If a callable A is overloaded with two or more signatures, callable B is assignable to A if B is assignable to all of the signatures in A
- Is
(self, int | Self) -> Selfassignable to(self, int) -> Self? Yes. - Is
(self, int | Self) -> Selfassignable to(self, Self) -> Self? Yes.
Thus, IntScalarOurs is assignable to IntScalarTheirs.
From a pure set-theoretic POV, it seems that subtyping functions should follow a very simply rule based on the domain.
$f <: g$ if and only if $\text{dom}(f) ⊇ \text{dom}(g)$ and $f(x) <: g(x)$ for all $x∈\text{dom}(g)$
What is currently specced appears to be a lemma for a sufficient condition, but not a necessary one.
Individual type-checkers can of course use such lemmas if the general case is too difficult to check/implement (and communicate that to their users), but shouldn't the spec be biased towards the theoretically principled point of view, when applicable?
Related Discussions
- Langage dominant
- Python
- Étoiles
- 1.8k
- Forks
- 302
- Merge moyen
- 4 h 38 min
- PR mergées (30 j)
- 6
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
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.
Autres issues de python/typing
-
topic: typing spec
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
-
topic: typing spec
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
Update reference/generics.html with PEP695 type parameter syntaxPeut-être pris @BHUVANSH855 l’a pris il y a 183 jours. Ouvertetopic: documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
-
topic: documentation
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
-
Finals and Literals: Spec contradictionPeut-être pris @srittau l’a pris il y a 37 jours. Ouvertetopic: conformance tests topic: typing spec
Difficulté 3/5 1-2 jours Accessibilité débutants 72/100
Toutes les issues de python/typing
Issues similaires
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 82/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 64/100
agrc/palletjack#208 ·
-
status/needs-triage type/bug
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
PKU-YuanGroup/OpenAI4S#218 ·
Les mainteneurs répondent en général sous 1 jour
-
Test LeakagePeut-être pris @garland3 l’a pris aujourd’hui. Ouverte
Difficulté 1/5 Moins d'une heure Accessibilité débutants 90/100
sandialabs/atlas-ui-3#1030 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 84/100
aws-samples/sample-ai-persona#151 ·
Les mainteneurs répondent en général sous 1 jour