Skip to content

Commit

Permalink
Improvements (#470)
Browse files Browse the repository at this point in the history
* Add Python 3.13 support
* Add status code to ResponseContainer
* Add response container status code
  • Loading branch information
tarsil authored Jan 9, 2025
1 parent 3367fa2 commit c729d3c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12"]
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]

services:
postgres:
Expand Down
5 changes: 4 additions & 1 deletion docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ hide:

### Added

- `name` to StaticFiles config allowing to reverse lookup internally.
- `name` parameter to StaticFiles config allowing to reverse lookup internally.
- Support for Python 3.13
- Support for `redirect_slashes` in the Include.
- `status_code` to ResponseContainer to be parameter detectable.

### Changed

Expand Down
1 change: 1 addition & 0 deletions esmerald/datastructures/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class ResponseContainer(BaseModel, ABC, Generic[R]):
background: Optional[Union[BackgroundTask, BackgroundTasks]] = None
headers: Dict[str, Any] = {}
cookies: List[Cookie] = []
status_code: Optional[int] = None

@abstractmethod
def to_response(
Expand Down
12 changes: 12 additions & 0 deletions esmerald/datastructures/redirect.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import TYPE_CHECKING, Any, Dict, Type, Union

from lilya import status
from lilya.responses import RedirectResponse # noqa
from typing_extensions import Annotated, Doc

Expand All @@ -22,6 +23,17 @@ class Redirect(ResponseContainer[RedirectResponse]):
"""
),
]
status_code: Annotated[
int,
Doc(
"""
The status code of the response.
This should be in format of `int`.
Example: `301`.
"""
),
] = status.HTTP_307_TEMPORARY_REDIRECT

def to_response(
self,
Expand Down
2 changes: 1 addition & 1 deletion esmerald/routing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ async def response_content(
response: Response = data.to_response(
app=app,
headers=_headers,
status_code=self.status_code,
status_code=data.status_code or self.status_code,
media_type=media_type,
)
for cookie in _cookies:
Expand Down
17 changes: 16 additions & 1 deletion esmerald/routing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2026,7 +2026,11 @@ def __init__(
self.status_code = status_code

self.dependencies: Dependencies = dependencies or {}
self.description = description or inspect.cleandoc(self.handler.__doc__ or "")

if not description:
description = inspect.cleandoc(self.handler.__doc__ or "") if self.handler else ""

self.description = description
self.permissions = list(permissions) if permissions else [] # type: ignore
self.interceptors: Sequence[Interceptor] = []
self.middleware = list(middleware) if middleware else []
Expand Down Expand Up @@ -2504,6 +2508,7 @@ class Include(LilyaInclude):
"deprecated",
"security",
"tags",
"redirect_slashes",
)

def __init__(
Expand Down Expand Up @@ -2811,6 +2816,15 @@ async def another(request: Request) -> str:
"""
),
] = None,
redirect_slashes: Annotated[
Optional[bool],
Doc(
"""
Boolean flag indicating if the redirect slashes are enabled for the
routes or not.
"""
),
] = True,
) -> None:
self.path = path
if not path:
Expand Down Expand Up @@ -2873,6 +2887,7 @@ async def another(request: Request) -> str:
exception_handlers=exception_handlers,
deprecated=deprecated,
include_in_schema=include_in_schema,
redirect_slashes=redirect_slashes,
)

def resolve_app_parent(self, app: Optional[Any]) -> Optional[Any]:
Expand Down
2 changes: 2 additions & 0 deletions esmerald/transformers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ def get_field_definition_from_param(param: "Parameter") -> Tuple[Any, Any]:
the Any type. This is necessary because the signature model will be
generated before the actual type is resolved.
"""
annotation: Union[Any, FieldInfo]

if param.optional:
annotation = should_skip_json_schema(param)
annotation = Any if isinstance(param.annotation, str) else param.annotation
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ classifiers = [
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"Topic :: Internet :: WWW/HTTP :: HTTP Servers",
"Topic :: Internet :: WWW/HTTP",
]
Expand All @@ -41,7 +42,7 @@ dependencies = [
"email-validator >=2.2.0,<3.0.0",
"itsdangerous>=2.1.2,<3.0.0",
"jinja2>=3.1.2,<4.0.0",
"lilya>=0.11.11",
"lilya>=0.12.0",
"loguru>=0.7.0,<0.8.0",
"pydantic>=2.10,<3.0.0",
"pydantic-settings>=2.0.0,<3.0.0",
Expand Down Expand Up @@ -108,7 +109,7 @@ testing = [
"flask>=1.1.2,<4.0.0",
"freezegun>=1.2.2,<2.0.0",
"mongoz>=0.6.0",
"mypy==1.11.2",
"mypy==1.14.1",
"pytest>=7.1.3,<9.0.0",
"pytest-cov>=4.1.0,<7.0.0",
"pytest-asyncio>=0.20.0",
Expand All @@ -127,7 +128,7 @@ docs = [
"a2wsgi>=1.9.0",
"griffe-typingdoc>=0.2.2",
"httpx>=0.25.0",
"lilya>=0.11.1",
"lilya>=0.12.0",
"mkautodoc>=0.2.0",
"mkdocs>=1.6.0",
"mkdocs-material>=9.5.25",
Expand Down

0 comments on commit c729d3c

Please sign in to comment.