Skip to content

Bug: data kwarg in dependency results in an exception #4230

@declaresub

Description

@declaresub

Description

The litestar documentation suggests that one can use the reserved 'data' kwarg in a dependency function. My attempt to do so results in an exception, traceback included below.

MCVE

from typing import Annotated

from litestar import Litestar, post
from litestar.di import Provide
from litestar.params import Dependency
from litestar.plugins.pydantic import PydanticInitPlugin
from pydantic import BaseModel


class Hello(BaseModel):
    hello: str


async def post_hello_world_dep(data: Hello):
    return Hello(hello=data.hello)


@post("/", dependencies={"resp": Provide(post_hello_world_dep)})
async def post_hello_world(resp: Annotated[Hello, Dependency()]) -> Hello:
    return resp


pydantic_plugin = PydanticInitPlugin(prefer_alias=True, exclude_unset=True)

app = Litestar(
    route_handlers=[post_hello_world],
    plugins=[pydantic_plugin],
)

Steps to reproduce

Create a virtual environment (I used python 3.10.6, but problem also happens with 3.13.5).
Install pydantic, litestar.
Save the MCVE code to a file named 'data_example.py' in the virtual environment.
python -m data_example.py

Traceback (most recent call last):
  File "/Users/charles/.pyenv/versions/3.10.6/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Users/charles/.pyenv/versions/3.10.6/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/private/tmp/data_example/data_example.py", line 19, in <module>
    app = Litestar(
  File "/private/tmp/data_example/lib/python3.10/site-packages/litestar/app.py", line 500, in __init__
    self.register(route_handler)
  File "/private/tmp/data_example/lib/python3.10/site-packages/litestar/app.py", line 709, in register
    route.create_handler_map()
  File "/private/tmp/data_example/lib/python3.10/site-packages/litestar/routes/http.py", line 98, in create_handler_map
    kwargs_model = route_handler.create_kwargs_model(path_parameters=self.path_parameters)
  File "/private/tmp/data_example/lib/python3.10/site-packages/litestar/handlers/base.py", line 577, in create_kwargs_model
    return KwargsModel.create_for_signature_model(
  File "/private/tmp/data_example/lib/python3.10/site-packages/litestar/_kwargs/kwargs_model.py", line 362, in create_for_signature_model
    is_data_optional=field_definitions["data"].is_optional if "data" in expected_reserved_kwargs else False,
KeyError: 'data'

Screenshots

No response

Logs


Litestar Version

2.16.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions