Skip to content

Commit

Permalink
Merge branch 'main' into dto-values-for-nested-data
Browse files Browse the repository at this point in the history
  • Loading branch information
provinzkraut authored Oct 17, 2024
2 parents 164bf11 + 682e4c7 commit 363645f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
11 changes: 4 additions & 7 deletions docs/examples/data_transfer_objects/factory/patch_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ class PatchDTO(DataclassDTO[Person]):
config = DTOConfig(exclude={"id"}, partial=True)


database = {
UUID("f32ff2ce-e32f-4537-9dc0-26e7599f1380"): Person(
id=UUID("f32ff2ce-e32f-4537-9dc0-26e7599f1380"), name="Peter", age=40
)
}
peter_uuid = UUID("f32ff2ce-e32f-4537-9dc0-26e7599f1380")
database = {peter_uuid: Person(id=peter_uuid, name="Peter", age=40)}


@patch("/person/{person_id:uuid}", dto=PatchDTO, return_dto=None, sync_to_thread=False)
def update_person(person_id: UUID, data: DTOData[Person]) -> Person:
"""Create a person."""
return data.update_instance(database.get(person_id))
"""Partially update a person."""
return data.update_instance(database[person_id])


app = Litestar(route_handlers=[update_person])
Expand Down
8 changes: 4 additions & 4 deletions docs/usage/dto/1-abstract-dto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,17 @@ attributes in the client payload, which requires some special handling internall

.. literalinclude:: /examples/data_transfer_objects/factory/patch_requests.py
:language: python
:emphasize-lines: 7,21,32,34
:emphasize-lines: 7,20,27,28,30
:linenos:

The ``PatchDTO`` class is defined for the Person class. The ``config`` attribute of ``PatchDTO`` is set to exclude the
id field, preventing clients from setting it when updating a person, and the ``partial`` attribute is set to ``True``,
The ``PatchDTO`` class is defined for the ``Person`` class. The ``config`` attribute of ``PatchDTO`` is set to exclude the
``id`` field, preventing clients from setting it when updating a person, and the ``partial`` attribute is set to ``True``,
which allows the DTO to accept a subset of the model attributes.

Inside the handler, the :meth:`DTOData.update_instance <litestar.dto.data_structures.DTOData.update_instance>` method is called
to update the instance of ``Person`` before returning it.

In our request, we set only the ``name`` property of the ``Person``, from ``"Peter"`` to ``"Peter Pan"`` and received
In our request, we update only the ``name`` property of the ``Person``, from ``"Peter"`` to ``"Peter Pan"`` and receive
the full object - with the modified name - back in the response.

Implicit Private Fields
Expand Down
4 changes: 4 additions & 0 deletions litestar/contrib/sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def __getattr__(attr_name: str) -> object:
)

value = globals()[attr_name] = getattr(exceptions, attr_name)

else: # pragma: no cover
raise RuntimeError(f"Unhandled module attribute: {attr_name!r}")

warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.{attr_name}",
version="2.12",
Expand Down
3 changes: 3 additions & 0 deletions litestar/contrib/sqlalchemy/repository/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def __getattr__(attr_name: str) -> object:
wrap_sqlalchemy_exception, # type: ignore[import-not-found] # pyright: ignore[reportMissingImport]
)

else: # pragma: no cover
raise RuntimeError(f"Unhandled module attribute: {attr_name!r}")

value = globals()[attr_name] = locals()[attr_name]
warn_deprecation(
deprecated_name=f"litestar.contrib.sqlalchemy.repository.{attr_name}",
Expand Down
6 changes: 3 additions & 3 deletions litestar/middleware/logging.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Iterable
from typing import TYPE_CHECKING, Any, Collection, Iterable

from litestar.constants import (
HTTP_RESPONSE_BODY,
Expand Down Expand Up @@ -273,7 +273,7 @@ class LoggingMiddlewareConfig:
"""Log message to prepend when logging a request."""
response_log_message: str = field(default="HTTP Response")
"""Log message to prepend when logging a response."""
request_log_fields: Iterable[RequestExtractorField] = field(
request_log_fields: Collection[RequestExtractorField] = field(
default=(
"path",
"method",
Expand All @@ -292,7 +292,7 @@ class LoggingMiddlewareConfig:
Thus, re-arranging the log-message is as simple as changing the iterable.
- To turn off logging of requests, use and empty iterable.
"""
response_log_fields: Iterable[ResponseExtractorField] = field(
response_log_fields: Collection[ResponseExtractorField] = field(
default=(
"status_code",
"cookies",
Expand Down
2 changes: 1 addition & 1 deletion litestar/testing/websocket_test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def receive() -> WebSocketReceiveMessage:
async def send(message: WebSocketSendMessage) -> None:
if message["type"] == "websocket.accept":
headers = message.get("headers", [])
if headers:
if headers: # type: ignore[truthy-iterable]
headers_list = list(self.scope["headers"])
headers_list.extend(headers)
self.scope["headers"] = headers_list
Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,17 @@ xfail_strict = true
[tool.mypy]
packages = ["litestar", "tests"]
plugins = ["pydantic.mypy"]
enable_error_code = [
"truthy-iterable",
"unused-awaitable",
"ignore-without-code",
"possibly-undefined",
"redundant-self",
]
python_version = "3.8"

disallow_any_generics = false
disallow_untyped_decorators = true
enable_error_code = "ignore-without-code"
implicit_reexport = false
show_error_codes = true
strict = true
Expand All @@ -256,6 +262,7 @@ warn_return_any = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true
local_partial_types = true

[[tool.mypy.overrides]]
ignore_errors = true
Expand Down

0 comments on commit 363645f

Please sign in to comment.