Spec/Conformance: subtyping callables with non-constant parameter mapping.
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 35/100
- Type d'issue
- Documentation
- Clarté
- Plutôt claire
- Activité
- Calme
- Stack technique
- python
- Domaine
- documentation, testing-qa
Piste de recherche
Commencez par les règles d’assignabilité de callable indiquées dans l’issue et reproduisez l’exemple avec mypy, pyright, ty et pyrefly. L’issue ne nomme aucun fichier du dépôt ni aucun chemin de tests de conformité ; l’achèvement dépend de la décision de clarifier le mappage des paramètres constants dans la spécification ou d’ajouter ce cas aux tests de conformité.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Consider the example below, which errors with all tested type checkers (mypy/pyright/ty/pyrefly):
from typing import Protocol
class Interval: ...
class Make(Protocol):
def __call__(self, /, lower: float, upper: float) -> Interval: ...
def make_impl(
string_or_lower: str | float | None = None,
/,
lower: float | None = None,
upper: float | None = None,
) -> Interval: ...
def test() -> None:
_f: Make = make_impl # ❌️ type checkers error here.
I believe from a pure type theory POV, this assignment should be legal, because all legal arguments to Make are also legal arguments to make_impl. The spec phrases it in the same spirit:
A callable type B is assignable to a callable type A if the return type of B is assignable to the return type of A and the input signature of B accepts all possible combinations of arguments that the input signature of A accepts. All of the specific assignability rules described below derive from this general rule.
And I couldn't find anything else in https://typing.python.org/en/latest/spec/callables.html#assignability-rules-for-callables that would disallow this assignment.
It seems the type-checkers try to match the KEYWORD_OR_POSITIONAL parameters by name, which is incorrect. Make has 3 legal call signatures:
Make(float, float)Make(float, upper=float)Make(lower=float, upper=float)
and all these 3 call signatures are supported by make_impl, but the parameter mapping is not constant:
make_impl(float, float)->{lower:string_or_lower, upper:lower}make_impl(float, upper=float)->{lower:string_or_lower, upper:upper}make_impl(lower=float, upper=float)->{lower:lower, upper:upper}
So either the spec should demand a constant parameter mapping, or this example should be added to the conformance tests.
- Langage dominant
- Python
- Étoiles
- 1.8k
- Forks
- 302
- Merge moyen
- 23 h
- PR mergées (30 j)
- 8
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.
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
-
topic: 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
-
topic: conformance tests topic: typing spec
Difficulté 3/5 1-2 jours Accessibilité débutants 72/100
Toutes les issues de python/typing
Issues similaires
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
stephrobert/dsoxlab#238 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
sublimehq/package_control#1780 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
nwg-piotr/nwg-displays#145 ·