TypeForm: Recognize type variables in a consistent way
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 20.6k
- Forks
- 3.3k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Prerequisite: This issue applies after PR https://github.com/python/mypy/pull/19596 is merged.
Currently mypy sometimes allows a type variable to be assigned to a TypeForm variable and sometimes it does not. This is a problem because the behavior is inconsistent.
Specifically, mypy allows a type variable to be assigned to a TypeForm variable iff it is in a quoted type expression:
[case testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation]
...
E = TypeVar('E')
class Box(Generic[E]):
def foo(self, e: E) -> None:
list_of_typx: List[TypeForm] = [E] # E: "E" is a type variable and only valid in type context
typx1: TypeForm = E # E: "E" is a type variable and only valid in type context
typx2: TypeForm = 'E'
When a type variable is NOT quoted, mypy will not allow it to be assigned:
[case testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable]
SomeTypeVar = TypeVar('SomeTypeVar')
typx: TypeForm
...
# NOTE: Unbound TypeVar isn't currently accepted as a TypeForm. Is that OK?
typx = SomeTypeVar # E: Incompatible types in assignment (expression has type "TypeVar", variable has type "TypeForm[Any]")
I'd argue that TypeForm should always accept a type variable because it is a valid type expression and TypeForms are intended to accept all kinds of type expressions:
def cast[T](type_expr: TypeForm[T], value_expr: object) -> T: ...
cast(SomeTypeVar, some_expression) # should not be an error
In summary:
- mypy should allow a type variable to be assigned to a TypeForm variable, regardless of whether it is quoted or not. Update above-referenced tests to reflect this new behavior.
Guide de contribution
Ouvrir le guide de contribution
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
Examinez d’abord le PR prerequisite #19596, puis localisez les tests nommés testTypeVarTypeFormsAreOnlyRecognizedInStringAnnotation et testEveryKindOfTypeExpressionIsAssignableToATypeFormVariable. Vérifiez comment les variables de type entre guillemets et sans guillemets sont traitées pour les affectations de TypeForm. Le travail est terminé lorsque les deux cas autorisent les variables de type de manière cohérente et que les tests reflètent le nouveau comportement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 42/100