Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda