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

442
Vistas
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 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