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 properties in DTOs #3979

Open
sloria opened this issue Jan 28, 2025 · 1 comment
Open

Enhancement: Support properties in DTOs #3979

sloria opened this issue Jan 28, 2025 · 1 comment
Labels
Enhancement This is a new feature or request

Comments

@sloria
Copy link
Contributor

sloria commented Jan 28, 2025

Summary

DataclassDTO (and potentially other DTO types--I only tested with a Dataclass) don't seem to support properties.

Basic Example

# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "litestar",
#     "uvicorn",
# ]
# ///
from dataclasses import dataclass

import uvicorn
from litestar import Litestar, get
from litestar.dto import DataclassDTO, DTOConfig


@dataclass
class User:
    name: str

    @property
    def name_upper(self) -> str:
        return self.name.upper()


class UserDTO(DataclassDTO[User]):
    config = DTOConfig(
        include={"name_upper"},  # Doesn't work
    )


@get("/", dto=UserDTO)
async def get_user() -> User:
    return User(name="Alice")


app = Litestar(route_handlers=[get_user])

if __name__ == "__main__":
    uvicorn.run(app)
$ uv run repro.py
$ curl http://127.0.0.1:8000

Expected:

{"name_upper": "ALICE"}

Actual:

{}

Drawbacks and Impact

No response

Unresolved questions

No response

@sloria sloria added the Enhancement This is a new feature or request label Jan 28, 2025
@provinzkraut
Copy link
Member

Thanks for bringing this up @sloria, this should definitely be supported! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement This is a new feature or request
Projects
None yet
Development

No branches or pull requests

2 participants