Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

147
Vistas
Inheriting Generic classes with restricted TypeVar

Consider a simple pair of generic classes:

T = TypeVar("T", str, int)

class Base(Generic[T]):
    def __init__(self, v: T):
        self.v: T = v

    @property
    def value(self) -> T:
        return self.v

class Child(Base[T]):
    def __init__(self, v: T):
        super().__init__(v)

x = Child(123)
reveal_type(x.value)

While using T = TypeVar("T") works as expected. The restricted TypeVar as shown yields the following errors:

error: Argument 1 to "__init__" of "Base" has incompatible type "str"; expected "T"
error: Argument 1 to "__init__" of "Base" has incompatible type "int"; expected "T"
note: Revealed type is 'builtins.int*'

Note that reveal_type still works.

Another difference, is that the restricted TypeVar requires the type annotation for self.v assignment whereas the unrestricted does not.

In the full use-case, I actually have Callable[[Any], T], but the issue is the same.

This is with mypy 0.910 and Python 3.9.7.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

This seems to happen anytime you call super() and pass the a generic restricted TypeVar argument to any function not just init. On those cases, I believe we can ask mypy to leave this poor thing as it is, by including # type: ignore in those lines:

   ...
   super().__init__(v) # type: ignore
   ...

It is not so bad as it seems because type checking will still be done on other lines, differently of what would happen if you remove the : T from the parameter on parent function or on the child function (any of those would suffice to suppress complains, but would also stop checking inside those functions as well).

over 4 years ago · Santiago Trujillo Denunciar

0

Bounding T to an Union[int,str] should do the job:

T = TypeVar("T", bound=Union[str, int])


class Base(Generic[T]):

    def __init__(self, v: T):
        self.v: T = v

    @property
    def value(self) -> T:
        return self.v


class Child(Base[T]):

    def __init__(self, v: T):
        super().__init__(v)


x = Child(123)
reveal_type(x.value)
y = Child('a')
reveal_type(y.value)
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda