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

feat: Deprecate subclassing route handler decorators #3439

Merged
merged 4 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions litestar/handlers/http_handlers/decorators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from typing import TYPE_CHECKING

from litestar.enums import HttpMethod, MediaType
Expand Down Expand Up @@ -45,7 +46,16 @@
MSG_SEMANTIC_ROUTE_HANDLER_WITH_HTTP = "semantic route handlers cannot define http_method"


class delete(HTTPRouteHandler):
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,
)


class delete(HTTPRouteHandler, _SubclassWarningMixin):
"""DELETE Route Decorator.

Use this decorator to decorate an HTTP handler for DELETE requests.
Expand Down Expand Up @@ -217,7 +227,7 @@ def __init__(
)


class get(HTTPRouteHandler):
class get(HTTPRouteHandler, _SubclassWarningMixin):
"""GET Route Decorator.

Use this decorator to decorate an HTTP handler for GET requests.
Expand Down Expand Up @@ -390,7 +400,7 @@ def __init__(
)


class head(HTTPRouteHandler):
class head(HTTPRouteHandler, _SubclassWarningMixin):
"""HEAD Route Decorator.

Use this decorator to decorate an HTTP handler for HEAD requests.
Expand Down Expand Up @@ -580,7 +590,7 @@ def _validate_handler_function(self) -> None:
raise ImproperlyConfiguredException("A response to a head request should not have a body")


class patch(HTTPRouteHandler):
class patch(HTTPRouteHandler, _SubclassWarningMixin):
"""PATCH Route Decorator.

Use this decorator to decorate an HTTP handler for PATCH requests.
Expand Down Expand Up @@ -752,7 +762,7 @@ def __init__(
)


class post(HTTPRouteHandler):
class post(HTTPRouteHandler, _SubclassWarningMixin):
"""POST Route Decorator.

Use this decorator to decorate an HTTP handler for POST requests.
Expand Down Expand Up @@ -924,7 +934,7 @@ def __init__(
)


class put(HTTPRouteHandler):
class put(HTTPRouteHandler, _SubclassWarningMixin):
"""PUT Route Decorator.

Use this decorator to decorate an HTTP handler for PUT requests.
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_handlers/test_http_handlers/test_deprecation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations

import pytest

from litestar.handlers import delete, get, head, patch, post, put


@pytest.mark.parametrize("handler_cls", [get, post, put, patch, delete, head])
def test_subclass_warns_deprecation(handler_cls: get | post | put | patch | delete | head) -> None:
with pytest.warns(DeprecationWarning):

class SubClass(handler_cls):
pass
Loading