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

216
Visualizações
Union of generic types that is also generic

Say I have two types (one of them generic) like this

from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]): pass
class B: pass

And a union of A and B like this

C = A|B

Or, in pre-Python-3.10/PEP 604-syntax:

C = Union[A,B]

How do I have to change the definition of C, so that C is also generic? e.g. if an object is of type C[int], it is either

  • of type A[int] (type parameter is passed down) or
  • of type B (type parameter is ignored)
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Rereading the mypy documentation I believe I have found my answer:

Type aliases can be generic. In this case they can be used in two ways: Subscripted aliases are equivalent to original types with substituted type variables, so the number of type arguments must match the number of free type variables in the generic type alias. Unsubscripted aliases are treated as original types with free variables replaced with Any

So, to answer my question:

C = A[T]|B

should do the trick. And it does!

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