Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: multiple values for keyword arguments #3593

Closed
1 of 4 tasks
embecka opened this issue Jun 24, 2024 · 3 comments · Fixed by #3605
Closed
1 of 4 tasks

Bug: multiple values for keyword arguments #3593

embecka opened this issue Jun 24, 2024 · 3 comments · Fixed by #3605
Labels
Bug 🐛 This is something that is not working as expected

Comments

@embecka
Copy link

embecka commented Jun 24, 2024

Description

Function parameters clashes with keyword arguments in this function or in another. More places in the source code may be affected.

One possible solution is to declare kwargs argument as kwargs: dict[str, Any] instead of **kwargs: Any to separate those namespaces.

URL to code causing the issue

No response

MCVE

from litestar import Litestar, get


async def provide_connection() -> str:
    return "connection"


@get('/connection', dependencies={"connection": provide_connection})
async def get_connection(connection: str) -> str:
    return connection


async def provide_deserializer() -> int:
    return "deserializer"


@get('/deserializer', dependencies={"deserializer": provide_deserializer})
async def get_deserializer(deserializer: int) -> str:
    return deserializer


app = Litestar(route_handlers=[get_connection, get_deserializer])

Steps to reproduce

1. Request the endpoint 'http://127.0.0.1:8000/connection'
2. Request the endpoint 'http://127.0.0.1:8000/deserializer'

Screenshots

No response

Logs

Traceback (most recent call last):
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/middleware/_internal/exceptions/middleware.py", line 158, in __call__
    await self.app(scope, receive, capture_response_started)
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/_asgi/asgi_router.py", line 99, in __call__
    await asgi_app(scope, receive, send)
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 80, in handle
    response = await self._get_response_for_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 132, in _get_response_for_request
    return await self._call_handler_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 152, in _call_handler_function
    response_data, cleanup_group = await self._get_response_data(
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 191, in _get_response_data
    parsed_kwargs = route_handler.signature_model.parse_values_from_connection_kwargs(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: litestar._signature.model.SignatureModel.parse_values_from_connection_kwargs() got multiple values for keyword argument 'connection'






Traceback (most recent call last):
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/_signature/model.py", line 201, in parse_values_from_connection_kwargs
    return convert(kwargs, cls, strict=False, dec_hook=deserializer, str_keys=True).to_dict()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
msgspec.ValidationError: Expected `int`, got `str` - at `$.deserializer`

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/middleware/_internal/exceptions/middleware.py", line 158, in __call__
    await self.app(scope, receive, capture_response_started)
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/_asgi/asgi_router.py", line 99, in __call__
    await asgi_app(scope, receive, send)
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 80, in handle
    response = await self._get_response_for_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 132, in _get_response_for_request
    return await self._call_handler_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 152, in _call_handler_function
    response_data, cleanup_group = await self._get_response_data(
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/routes/http.py", line 191, in _get_response_data
    parsed_kwargs = route_handler.signature_model.parse_values_from_connection_kwargs(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/embe/anaconda3/envs/py312/lib/python3.12/site-packages/litestar/_signature/model.py", line 209, in parse_values_from_connection_kwargs
    for field_name, exc in cls._collect_errors(deserializer=deserializer, **kwargs):  # type: ignore[assignment]
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: litestar._signature.model.SignatureModel._collect_errors() got multiple values for keyword argument 'deserializer'

Litestar Version

2.9.0

Platform

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

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@embecka embecka added the Bug 🐛 This is something that is not working as expected label Jun 24, 2024
@provinzkraut
Copy link
Member

Hi @embecka! This is not a bug but a conscious design decision. It is documented here.

Litestar considers these parameters "reserved" for use by Litestar itself. To resolve possible naming issues, you can simply provide an alternative injection name for your parameters. In case of the dependencies, this would simply mean using a different name, and for other parameters, the docs have a link how to use the renaming feature

@provinzkraut provinzkraut closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2024
@provinzkraut
Copy link
Member

Sorry for the confusion @embecka, I should have read your example more carefully. Yes, this is indeed a bug and your suggestion would be the proper fix :)

@provinzkraut provinzkraut reopened this Jun 30, 2024
provinzkraut added a commit that referenced this issue Jun 30, 2024
…ignatures don't clash with model signature (#3605)

* Ensure signature model internal function signatures don't clash with model signature
Copy link

This issue has been closed in #3605. The change will be included in the upcoming patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
2 participants