Referring to class attributes of a generic type
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
I apologize for posting my question here as an issue; I am doing this out of desperation since my original post on StackOverflow went unanswered for three weeks. Below is a near-verbatim copy from StackOverflow.
I have an abstract base class which defines several class attributes which are types themselves (i.e., nested types); the attributes are overridden in concrete derived classes inheriting from the abstract class. The concrete derived classes are used with several other classes which are type-annotated as generics, where the type parameter has a type constraint that it must be derived from the abstract class. I want these other classes (generics) to refer to the class attributes (nested types) of the concrete classes. The following example should demonstrate the idea:
import typing
class Base:
A: typing.Type[Base]
class One(Base):
class A(Base): # overrides Base.A
pass
class Two(Base):
class A(One): # overrides Base.A
pass
T = typing.TypeVar('T', bound=Base)
class Foo(typing.Generic[T]): # the generic
def foo(self, a: typing.Type[T.A]) -> T: # <-- here, see T.A
...
f: Foo[Two] = Foo()
f.foo(Two.A) # line 22 is here
MyPy v0.701 reports an error on line 22:
Argument 1 to "foo" of "Foo" has incompatible type "Type[A]"; expected "Type[Two]"
MyPy seems to ignore the attribute reference at T.A. How do I make it understand that I am trying to refer to the type available via the attribute .A? The attribute is guaranteed to be available because the type variable T is constrained to Base, which has A. Note that extending the set of generic types is not an option; I could elaborate on this but the explanation would be too specific to my application.
UPDATE: MyPy v0.711 with --new-semantic-analyzer yields a more comprehensible error message:
Name 'T.A' is not defined
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
El issue no menciona archivos ni tests del repositorio; comienza con el ejemplo mínimo de Python y sigue cómo mypy gestiona los límites de TypeVar, las anotaciones genéricas y las referencias de tipo de atributos de clase. Se considera terminado cuando el comportamiento de tipado solicitado tiene una implementación o limitación definida y está cubierto por un test de regresión.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- compilers
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100