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

388
Vistas
How to pass typedDict as arguments in python for fastapi

I am working in this simple fastapi example, I would like to replace the read_books arguments with a equivalent TypedDict, like the commented line. Is it possible?

from fastapi import FastAPI, Query
from typing import Optional, TypedDict


# would like to define model for query parameter in separeted object
class GetModel(TypedDict):
    test: Optional[str]


query_param =  GetModel(test=Query('default value', max_length=10, title='titulo', description="description 123", regex="^[a-z]+"))



app = FastAPI()

@app.get("/books")
def read_books(test: Optional[str] = Query('default value', max_length=10, title='titulo', description="description 123", regex="^[a-z]+")):
#def read_books(query_param):
    """
    comentario do endpoint
    """
    results = {"books": [{"book_name": "The Great Hunt"}, {"book_name": "The Dragon Reborn"}]}

    if test:
        results.update({"test": {"name":test}})
    
    return results
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The way to do this is to implement your query parameter parsing in a separate class dependency, as described in the documentation (also, have a look at this discussion). Example below:

from fastapi import FastAPI, Query, Depends
from typing import Optional

app = FastAPI()

class MyModel:
    def __init__(
        self,
        test: Optional[str] = Query('default value', max_length=10, title='titulo', description="description 123", regex="^[a-z]+")
    ):
        self.test = test


@app.get("/books")
def read_books(m: MyModel = Depends(MyModel)):
    results = {"books": [{"book_name": "The Great Hunt"}, {"book_name": "The Dragon Reborn"}]}
    if m.test:
        results.update({"test": {"name": m.test}})
    return results
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