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

Enhancement: Support generic types in DTOs #2500

Open
1 of 4 tasks
guacs opened this issue Oct 23, 2023 · 3 comments · May be fixed by #2791
Open
1 of 4 tasks

Enhancement: Support generic types in DTOs #2500

guacs opened this issue Oct 23, 2023 · 3 comments · May be fixed by #2791
Assignees
Labels
DTOs This is related to our DTO feature Enhancement This is a new feature or request

Comments

@guacs
Copy link
Member

guacs commented Oct 23, 2023

Description

DTOs does not seem to support generic types.

URL to code causing the issue

No response

MCVE

from dataclasses import dataclass
from typing import Generic, TypeVar

from litestar import Litestar
from litestar import post
from litestar.dto import DTOConfig, DataclassDTO, DTOData


T = TypeVar("T")


@dataclass
class Foo(Generic[T]):
    foo: T


FooDTO = DataclassDTO[Foo[int]]


@post("/foo", dto=FooDTO)
async def foo_handler(data: DTOData[Foo[int]]) -> Foo[int]:
    return data.create_instance()


app = Litestar([foo_handler])

Steps to reproduce

Run `litestar run`.

Screenshots

No response

Logs

Traceback (most recent call last):
  File "/home/guacs/open-source/litestar/.venv/bin/litestar", line 8, in <module>
    sys.exit(run_cli())
  File "/home/guacs/open-source/litestar/litestar/__main__.py", line 6, in run_cli
    litestar_group()
  File "/home/guacs/open-source/litestar/.venv/lib/python3.8/site-packages/click/core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
  File "/home/guacs/open-source/litestar/.venv/lib/python3.8/site-packages/rich_click/rich_command.py", line 125, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "/home/guacs/open-source/litestar/litestar/cli/_utils.py", line 242, in make_context
    self._prepare(ctx)
  File "/home/guacs/open-source/litestar/litestar/cli/_utils.py", line 224, in _prepare
    env = ctx.obj = LitestarEnv.from_env(ctx.params.get("app_path"), ctx.params.get("app_dir"))
  File "/home/guacs/open-source/litestar/litestar/cli/_utils.py", line 122, in from_env
    loaded_app = _autodiscover_app(cwd)
  File "/home/guacs/open-source/litestar/litestar/cli/_utils.py", line 332, in _autodiscover_app
    module = importlib.import_module(import_path)
  File "/home/guacs/.pyenv/versions/3.8.17/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/guacs/open-source/litestar/app.py", line 25, in <module>
    app = Litestar([foo_handler])
  File "/home/guacs/open-source/litestar/litestar/app.py", line 447, in __init__
    self.register(route_handler)
  File "/home/guacs/open-source/litestar/litestar/app.py", line 623, in register
    route_handler.on_registration(self)
  File "/home/guacs/open-source/litestar/litestar/handlers/http_handlers/base.py", line 492, in on_registration
    super().on_registration(app)
  File "/home/guacs/open-source/litestar/litestar/handlers/base.py", line 525, in on_registration
    self.resolve_data_dto()
  File "/home/guacs/open-source/litestar/litestar/handlers/base.py", line 451, in resolve_data_dto
    data_dto.create_for_field_definition(
  File "/home/guacs/open-source/litestar/litestar/dto/base_dto.py", line 176, in create_for_field_definition
    raise InvalidAnnotationException(
litestar.exceptions.dto_exceptions.InvalidAnnotationException: DTO narrowed with 'app.Foo[int]', handler type is 'litestar.dto.data_structures.DTOData[app.Foo[int]]'

Litestar Version

main

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 Litestar 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
@guacs guacs added Bug 🐛 This is something that is not working as expected Triage Required 🏥 This requires triage labels Oct 23, 2023
@peterschutt peterschutt self-assigned this Oct 24, 2023
@provinzkraut
Copy link
Member

@peterschutt You started working on this, but closed the PR again. Is this blocked by something?

@provinzkraut provinzkraut removed the Triage Required 🏥 This requires triage label Nov 28, 2023
@peterschutt
Copy link
Contributor

No, just ran into a dead-end with the approach I was taking and have been sidetracked since.

peterschutt added a commit that referenced this issue Nov 28, 2023
This PR is an attempt at handling DTOs that are narrowed with a `_GenericAlias` of a type supported by the DTO factory type.

Closes #2500
@peterschutt peterschutt linked a pull request Nov 28, 2023 that will close this issue
5 tasks
peterschutt added a commit that referenced this issue Nov 28, 2023
This PR is an attempt at handling DTOs that are narrowed with a `_GenericAlias` of a type supported by the DTO factory type.

Closes #2500
peterschutt added a commit that referenced this issue Nov 28, 2023
This PR is an attempt at handling DTOs that are narrowed with a `_GenericAlias` of a type supported by the DTO factory type.

Closes #2500
@JacobCoffee JacobCoffee added the DTOs This is related to our DTO feature label Dec 7, 2023
@interifter
Copy link

Encountered this on python 3.12 on Windows, as well

@provinzkraut provinzkraut added Enhancement This is a new feature or request and removed Bug 🐛 This is something that is not working as expected labels Feb 14, 2024
@provinzkraut provinzkraut changed the title Bug: DTOs cannot handle generic types Enhancement: Support generic types in DTOs Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DTOs This is related to our DTO feature Enhancement This is a new feature or request
Projects
Status: Working on it
5 participants