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

438
Visualizações
How do I inherit from union'd pydantic models

Currently I have quite a few models defined and I want a single endpoint that can receive and return them.

My models are all different, e.g.

class A(BaseModel):
  foo: str

class B(BaseModel):
  bar: str

All = Union[A, B]

Considering the case where my endpoint accepts a body of the form All, and returns a new resource of that same form. I want to be able to specify that the return type All is All plus an extra id field.

i.e.

RequestAll = Union[A,B]

class ResponseAll(RequestAll):
  id = str

However, when I do this, I get an error:

__init__() takes 2 positional [but more were given]

Another approach I could take is to have a RequestA and ResponseA equivalent for every type, then union Requests and Responses respectively. However, I would prefer to avoid this given the amount of types I actually have.

Is there a neat way to address this problem?

over 4 years ago · Santiago Trujillo
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