Skip to content

Generic TypedDict types are not supported in response validation decorators #119

@semohr

Description

@semohr

When using TypedDict with Generic / TypeVar, the resulting type cannot be used with validation decorators such as @validate_response.

Example

I'm trying to create a simple json:api like schema and was wondering if it is supported.

A = TypeVar("A")

class Resource(TypedDict, Generic[A]):
    type: str
    id: str
    attributes: A 

class ItemAttributes(TypedDict, total=False):
    title: str

# Error
@app.get("/items", methods=["GET"])
@validate_response(Resource[ItemAttributes])
async def get_items() -> Resource[ItemAttributes]:
   ...

# Works
@app.get("/items", methods=["GET"])
async def get_items() -> Resource[ItemAttributes]:
   ...

I would have expected this works as msgspec does not seems to have issues here.

msgspec.json.schema(Resource[ItemAttributes]) # works
# Same for encode/decode

Best, S


 Traceback (most recent call last):
   File "/venv/lib/python3.12/site-packages/quart/app.py", line 1500, in full_dispatch_request
     result = await self.dispatch_request(request_context)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/venv/lib/python3.12/site-packages/quart/app.py", line 1597, in dispatch_request
     return await self.ensure_async(handler)(**request_.view_args)  # type: ignore[return-value]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/venv/lib/python3.12/site-packages/quart_schema/extension.py", line 321, in openapi
     schema = self.openapi_provider.schema()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/venv/lib/python3.12/site-packages/quart_schema/openapi.py", line 62, in schema
     path_objects, path_components = self.build_paths(rule)
                                     ^^^^^^^^^^^^^^^^^^^^^^
   File "/venv/lib/python3.12/site-packages/quart_schema/openapi.py", line 178, in build_paths
     response_object, response_components = self.build_response_object(
                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   File "/venv/lib/python3.12/site-packages/quart_schema/openapi.py", line 341, in build_response_object
     schema = model_schema(
              ^^^^^^^^^^^^^
   File "/venv/lib/python3.12/site-packages/quart_schema/conversion.py", line 217, in model_schema
     raise TypeError(f"Cannot create schema for {model_class}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions