[spec] Can staticmethod calls infer type parameters for the containing class?
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
In this code:
from typing import TypeVar, Generic
T = TypeVar('T')
class Foo(Generic[T]):
@staticmethod
def create(arg: T) -> 'Foo[T]':
...
reveal_type(Foo.create(1234))
What should the type be? Should T be inferred based on the argument to the static method, resulting in Foo[int]? Or should it be Foo[Unknown] or similar?
mypy (python/mypy@fe15ee69b) picks Foo[int].
pyright used to choose Foo[int], but microsoft/pyright#7454 changed it to pick Foo[Unknown], on the grounds that this is required to comply with PEP 696's statement:
Type parameter defaults should be bound by attribute access (including call and subscript).
My example does not include any type parameter defaults, but I can see the logic of applying the same rule without one: you wouldn't want it to be a breaking change to add a default.
Still, why can't these kinds of static method calls be treated like constructor calls, where class type parameters can be inferred from arguments?
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 examiner l’exemple, les règles de liaison de PEP 696, le commit mypy fe15ee69b et l’issue pyright référencée. Déterminez le comportement convenu pour les appels à staticmethod sur les classes génériques et consignez la décision de spécification qui en résulte, notamment si les vérificateurs de types doivent inférer Foo[int] ou utiliser un type inconnu.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- devtools
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100