Cannot anotate type of generic functions.
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Accessibilité débutants
- 32/100
Piste de recherche
Commencez par les exemples de fonctions génériques et l’issue Pyright liée, puis comparez-les aux règles actuelles de portée des variables de type. Le travail est considéré comme terminé lorsque la spécification de typing dispose d’une résolution approuvée pour l’annotation des variables de fonctions génériques et des variables d’instance de classe ; l’issue ne nomme aucun fichier ni test du dépôt.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
The current scoping rules for type variables seems to deny type annotations over generic functions.
The issue
define two functions as follows:
def fx_1(f: Callable[[bool], int]) -> int:
return f(True)
def fx_2[T](f: Callable[[bool], T]) -> T:
return f(True)
If a function alias is required, these functions can be assigned to variables. However, if type annotations are used, fx_2 cannot be annotated using compliant type checkers (i.e. pyright).
T = TypeVar("T")
fx_1_alias: Callable[[Callable[[bool], int]], int] = fx_1 # OK!
fx_2_alias: Callable[[Callable[[bool], T]], T] = fx_2 # complains that T has no meaning
Justification
- All variables should be able to be type annotated, even when those variables are functions or the annotation is not required.
defis a syntactic sugar equivalent to a variable assignation over a lambda function. As a result, it should be possible to define generic functions withoutdefstatements e.g.
fx_2_alternative: Callable[[Callable[[bool], T]], T] = lambda f: f(True)
- Generics are the first ladder of what is called a "dependent type". In the case of functions, this corresponds to a Π type where the input itself is the concrete type that will be replaced on the type var of a generic type. Here type-vars scope is over the function type itself rather than some outer entity (class, function, method).
- By these definitions of Π type, it cannot exist an unbound type variable when function/callable types are involved, since in python, the type argument is implicit and supplied when referring a TypeVar.
e.g.
T = TypeVar("T")
fx_2_alias: Callable[[Callable[[bool], T]], T] = fx_2
fx_2_alias_2: Callable[[Callable[[bool], T]], T] = fx_2
Even when using the same type var fx_2_alias and fx_2_alias_2 each var is an independent one.
A more clear syntax (like the introduced on def statements on python312) could be
fx_2_alias: Callable[X][[Callable[[bool], X]], X] = fx_2
fx_2_alias_2: Callable[X][[Callable[[bool], X]], X] = fx_2
This marks explicitly that the scope of the var is over the type itself. However, this issue does not aim to propose a new syntax, but rather aims to adjust the scoping rules of type vars.
Motivation
This issue was derived from a pyright issue where generic functions as class instance variables are denied by the checker because of the same scope issues described before.
- 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
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 75/100
-
hcocena Ouvertepolicies-accepted pre-review precheck-passed
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
Bioconductor/BiocContributions#214 · 5 commentaires ·
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 92/100
TencentCloud/Octop#1169 · 1 commentaire ·
-
[开源推荐] 在老板拷问你之前,先让 AI 灵魂拷问你 Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
521xueweihan/HelloGitHub#3778 ·
-
The version checker's trailing attribute region has no control for a less-than inside a quoted value Ouvertearea: dashboard area: tests bug perceived difficulty: 2 python
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
Nitjsefnie-Harness-Commons/daedalus#1105 · 1 commentaire ·