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: "type" keyword doesn't behave the same as TypeAlias for Dependency #3912

Open
1 of 4 tasks
Rubikoid opened this issue Dec 23, 2024 · 0 comments
Open
1 of 4 tasks
Labels
Bug 🐛 This is something that is not working as expected

Comments

@Rubikoid
Copy link

Rubikoid commented Dec 23, 2024

Description

Dependency's skip_validation=True parameter don't work for type keyword

URL to code causing the issue

No response

MCVE

from typing import Annotated, TypeAlias

from litestar import Litestar, get
from litestar.di import Provide
from litestar.params import Dependency


async def dep_eq() -> int:
    return 0


async def dep_type() -> int:
    return 1


async def dep_alias() -> int:
    return 2


ARG_EQ = Annotated[str, Dependency(skip_validation=True)]
type ARG_TYPE = Annotated[str, Dependency(skip_validation=True)]
ARG_ALIAS: TypeAlias = Annotated[str, Dependency(skip_validation=True)]  # noqa: UP040


@get(
    "/broken",
    dependencies={
        "arg_eq": Provide(dep_eq),
        "arg_type": Provide(dep_type),
        "arg_alias": Provide(dep_alias),
    },
)
async def broken(
    arg_eq: ARG_EQ,
    arg_type: ARG_TYPE,
    arg_alias: ARG_ALIAS,
) -> str:
    return (
        f"{arg_eq = } ({type(arg_eq) = }), "
        f"{arg_type = } ({type(arg_type) = }), "
        f"{arg_alias = } ({type(arg_alias) = })"
    )


@get(
    "/working",
    dependencies={
        "arg_eq": Provide(dep_eq),
        "arg_alias": Provide(dep_alias),
    },
)
async def working(
    arg_eq: ARG_EQ,
    arg_alias: ARG_ALIAS,
) -> str:
    return (
        f"{arg_eq = } ({type(arg_eq) = }), "  #
        f"{arg_alias = } ({type(arg_alias) = })"
    )


app = Litestar(
    debug=True,
    route_handlers=[
        broken,
        working,
    ],
)

Steps to reproduce

1. Go to `/docs`
2. Execute `/broken`
3. See error

Expected output:

arg_eq = 0 (type(arg_eq) = <class 'int'>), arg_type = 1 (type(arg_type) = <class 'int'>), arg_alias = 2 (type(arg_alias) = <class 'int'>)

Screenshots

No response

Logs

ERROR - 2024-12-23 19:36:57,329 - litestar - config - Uncaught exception (connection_type=http, path=/broken):
Traceback (most recent call last):
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/_signature/model.py", line 203, in parse_values_from_connection_kwargs
    return convert(kwargs, cls, strict=False, dec_hook=deserializer, str_keys=True).to_dict()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
msgspec.ValidationError: Expected `str`, got `int` - at `$.arg_type`

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/middleware/_internal/exceptions/middleware.py", line 159, in __call__
    await self.app(scope, receive, capture_response_started)
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/_asgi/asgi_router.py", line 100, in __call__
    await asgi_app(scope, receive, send)
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/routes/http.py", line 80, in handle
    response = await self._get_response_for_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/routes/http.py", line 132, in _get_response_for_request
    return await self._call_handler_function(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/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 "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/routes/http.py", line 183, in _get_response_data
    parsed_kwargs = route_handler.signature_model.parse_values_from_connection_kwargs(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/9pvldmaimlp28q25hdhv6h67s2p9ls9f-personal-dev-env/lib/python3.12/site-packages/litestar/_signature/model.py", line 216, in parse_values_from_connection_kwargs
    raise cls._create_exception(messages=messages, connection=connection) from e
litestar.exceptions.http_exceptions.InternalServerException: 500: Internal Server Error

Litestar Version

2.13.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
@Rubikoid Rubikoid added the Bug 🐛 This is something that is not working as expected label Dec 23, 2024
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
Development

No branches or pull requests

1 participant