Skip to content

Commit

Permalink
fix(warnings): Do not warn for default handlers (#3569)
Browse files Browse the repository at this point in the history
* fix(warnings): Do not warn for default handlers
* test: add test to check (lack of) warnings

---------

Co-authored-by: Alc-Alc <alc@localhost>
  • Loading branch information
Alc-Alc and Alc-Alc authored Jun 15, 2024
1 parent f179aee commit 064c8fe
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
("py:exc", "HTTPExceptions"),
(PY_CLASS, "litestar.template.Template"),
(PY_CLASS, "litestar.middleware.compression.gzip_facade.GzipCompression"),
(PY_CLASS, "litestar.handlers.http_handlers.decorators._SubclassWarningMixin"),
(PY_CLASS, "litestar.handlers.http_handlers.decorators._subclass_warning"),
]

nitpick_ignore_regex = [
Expand Down
45 changes: 31 additions & 14 deletions litestar/handlers/http_handlers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,16 @@
MSG_SEMANTIC_ROUTE_HANDLER_WITH_HTTP = "semantic route handlers cannot define http_method"


class _SubclassWarningMixin:
def __init_subclass__(cls, **kwargs: Any) -> None:
warnings.warn(
"Semantic HTTP route handler classes are deprecated and will be replaced by "
"functional decorators in Litestar 3.0.",
category=DeprecationWarning,
stacklevel=2,
)
def _subclass_warning() -> None:
warnings.warn(
"Semantic HTTP route handler classes are deprecated and will be replaced by "
"functional decorators in Litestar 3.0.",
category=DeprecationWarning,
stacklevel=2,
)


class delete(HTTPRouteHandler, _SubclassWarningMixin):
class delete(HTTPRouteHandler):
"""DELETE Route Decorator.
Use this decorator to decorate an HTTP handler for DELETE requests.
Expand Down Expand Up @@ -227,8 +226,11 @@ def __init__(
**kwargs,
)

def __init_subclass__(cls, **kwargs: Any) -> None:
_subclass_warning()


class get(HTTPRouteHandler, _SubclassWarningMixin):
class get(HTTPRouteHandler):
"""GET Route Decorator.
Use this decorator to decorate an HTTP handler for GET requests.
Expand Down Expand Up @@ -400,8 +402,11 @@ def __init__(
**kwargs,
)

def __init_subclass__(cls, **kwargs: Any) -> None:
_subclass_warning()


class head(HTTPRouteHandler, _SubclassWarningMixin):
class head(HTTPRouteHandler):
"""HEAD Route Decorator.
Use this decorator to decorate an HTTP handler for HEAD requests.
Expand Down Expand Up @@ -577,6 +582,9 @@ def __init__(
**kwargs,
)

def __init_subclass__(cls, **kwargs: Any) -> None:
_subclass_warning()

def _validate_handler_function(self) -> None:
"""Validate the route handler function once it is set by inspecting its return annotations."""
super()._validate_handler_function()
Expand All @@ -591,7 +599,7 @@ def _validate_handler_function(self) -> None:
raise ImproperlyConfiguredException("A response to a head request should not have a body")


class patch(HTTPRouteHandler, _SubclassWarningMixin):
class patch(HTTPRouteHandler):
"""PATCH Route Decorator.
Use this decorator to decorate an HTTP handler for PATCH requests.
Expand Down Expand Up @@ -762,8 +770,11 @@ def __init__(
**kwargs,
)

def __init_subclass__(cls, **kwargs: Any) -> None:
_subclass_warning()


class post(HTTPRouteHandler, _SubclassWarningMixin):
class post(HTTPRouteHandler):
"""POST Route Decorator.
Use this decorator to decorate an HTTP handler for POST requests.
Expand Down Expand Up @@ -934,8 +945,11 @@ def __init__(
**kwargs,
)

def __init_subclass__(cls, **kwargs: Any) -> None:
_subclass_warning()

class put(HTTPRouteHandler, _SubclassWarningMixin):

class put(HTTPRouteHandler):
"""PUT Route Decorator.
Use this decorator to decorate an HTTP handler for PUT requests.
Expand Down Expand Up @@ -1105,3 +1119,6 @@ def __init__(
type_encoders=type_encoders,
**kwargs,
)

def __init_subclass__(cls, **kwargs: Any) -> None:
_subclass_warning()
15 changes: 15 additions & 0 deletions tests/unit/test_handlers/test_http_handlers/test_deprecation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from __future__ import annotations

from importlib import reload
from warnings import catch_warnings, simplefilter

import pytest

from litestar.handlers import delete, get, head, patch, post, put
Expand All @@ -11,3 +14,15 @@ def test_subclass_warns_deprecation(handler_cls: get | post | put | patch | dele

class SubClass(handler_cls): # type: ignore[valid-type, misc]
pass


def test_default_no_warns() -> None:
with catch_warnings(record=True) as warnings:
simplefilter("always")
import litestar.handlers.http_handlers.decorators

reload(litestar.handlers.http_handlers.decorators)
assert len(warnings) == 0

# revert to previous filter
simplefilter("default")

0 comments on commit 064c8fe

Please sign in to comment.