Skip to content

Enhancement: support ReadOnly type for TypedDict #4423

@sobolevn

Description

@sobolevn

Summary

Right now this happens for this code sample:

from litestar import Litestar, get
from typing import TypedDict
from typing_extensions import ReadOnly

class Model(TypedDict):
    id: ReadOnly[str]

@get('/')
async def test_route() -> Model:
    return {'id': 'test'}

app = Litestar(
    route_handlers=[test_route],
)

/schema endpoint raises:

500: Parameter 'id' with type 'typing.ReadOnly[str]' could not be mapped to an Open API type. This can occur if a user-defined generic type is resolved as a parameter. If 'id' should not be documented as a parameter, annotate it using the `Dependency` function, e.g., `id: ... = Dependency(...)`.

Note that OpenAPI has readOnly: true property, see https://swagger.io/docs/specification/v3_0/data-models/data-types/#read-only-and-write-only-properties But! It is not the same ReadOnly as in Python. Our ReadOnly means that the key in TypeDict can't be changed. I think the proper solution here is to just unwrap ReadOnly[T] to T

I will send a PR.

Basic Example

No response

Drawbacks and Impact

No response

Unresolved questions

No response

Metadata

Metadata

Assignees

Labels

EnhancementThis is a new feature or requestOpenAPIThis is related to our OpenAPI schemaTypingThis relates to typing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions