-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Version 1.0.0 #2384
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
Version 1.0.0 #2384
Changes from all commits
7b62e9d
4d436f6
d1d388f
707cb38
fa739e1
3d09c8b
6edecb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "0.38.1" | ||
| __version__ = "1.0.0" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
|
|
||
| import sys | ||
| import typing | ||
| import warnings | ||
|
|
||
| if sys.version_info >= (3, 10): # pragma: no cover | ||
| from typing import ParamSpec | ||
|
|
@@ -11,7 +10,6 @@ | |
|
|
||
| from starlette.datastructures import State, URLPath | ||
| from starlette.middleware import Middleware, _MiddlewareClass | ||
| from starlette.middleware.base import BaseHTTPMiddleware | ||
| from starlette.middleware.errors import ServerErrorMiddleware | ||
| from starlette.middleware.exceptions import ExceptionMiddleware | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to remove ExceptionMiddleware from here. And maybe delete that file as well.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where should we register the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I guess we need to get rid of is this part: https://github.com/encode/starlette/blob/966f0fc85764bd478d7c582fc8e2e5f91dbebae6/starlette/middleware/exceptions.py#L62
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah! Just to confirm: you mean that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep!
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot come up with a solution for this, FYI.
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adriangb how do you want to proceed here? We talked in PyCon US a bit, but I don't recall how we left this. This is the only blocker for 1.0. The only thing is that I don't want to add any breaking change.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to do this without any breaking change we can just leave But conceptually I think the thing is that:
To do that we'd have to replace
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving the pure ASGI realm, I have a minimalistic and effective design. https://github.com/abersheeran/baize/blob/master/baize/asgi/shortcut.py#L59
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very nice :) |
||
| from starlette.requests import Request | ||
|
|
@@ -43,15 +41,8 @@ class Starlette: | |
| Exception handler callables should be of the form | ||
| `handler(request, exc) -> response` and may be either standard functions, or | ||
| async functions. | ||
| * **on_startup** - A list of callables to run on application startup. | ||
| Startup handler callables do not take any arguments, and may be either | ||
| standard functions, or async functions. | ||
| * **on_shutdown** - A list of callables to run on application shutdown. | ||
| Shutdown handler callables do not take any arguments, and may be either | ||
| standard functions, or async functions. | ||
| * **lifespan** - A lifespan context function, which can be used to perform | ||
| startup and shutdown tasks. This is a newer style that replaces the | ||
| `on_startup` and `on_shutdown` handlers. Use one or the other, not both. | ||
| startup and shutdown tasks. | ||
| """ | ||
|
|
||
| def __init__( | ||
|
|
@@ -60,21 +51,11 @@ def __init__( | |
| routes: typing.Sequence[BaseRoute] | None = None, | ||
| middleware: typing.Sequence[Middleware] | None = None, | ||
| exception_handlers: typing.Mapping[typing.Any, ExceptionHandler] | None = None, | ||
| on_startup: typing.Sequence[typing.Callable[[], typing.Any]] | None = None, | ||
| on_shutdown: typing.Sequence[typing.Callable[[], typing.Any]] | None = None, | ||
| lifespan: Lifespan[AppType] | None = None, | ||
| ) -> None: | ||
| # The lifespan context function is a newer style that replaces | ||
| # on_startup / on_shutdown handlers. Use one or the other, not both. | ||
| assert lifespan is None or ( | ||
| on_startup is None and on_shutdown is None | ||
| ), "Use either 'lifespan' or 'on_startup'/'on_shutdown', not both." | ||
|
|
||
| self.debug = debug | ||
| self.state = State() | ||
| self.router = Router( | ||
| routes, on_startup=on_startup, on_shutdown=on_shutdown, lifespan=lifespan | ||
| ) | ||
| self.router = Router(routes, lifespan=lifespan) | ||
| self.exception_handlers = ( | ||
| {} if exception_handlers is None else dict(exception_handlers) | ||
| ) | ||
|
|
@@ -122,9 +103,6 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | |
| self.middleware_stack = self.build_middleware_stack() | ||
| await self.middleware_stack(scope, receive, send) | ||
|
|
||
| def on_event(self, event_type: str) -> typing.Callable: # type: ignore[type-arg] | ||
| return self.router.on_event(event_type) # pragma: nocover | ||
|
|
||
| def mount(self, path: str, app: ASGIApp, name: str | None = None) -> None: | ||
| self.router.mount(path, app=app, name=name) # pragma: no cover | ||
|
|
||
|
|
@@ -148,13 +126,6 @@ def add_exception_handler( | |
| ) -> None: # pragma: no cover | ||
| self.exception_handlers[exc_class_or_status_code] = handler | ||
|
|
||
| def add_event_handler( | ||
| self, | ||
| event_type: str, | ||
| func: typing.Callable, # type: ignore[type-arg] | ||
| ) -> None: # pragma: no cover | ||
| self.router.add_event_handler(event_type, func) | ||
|
|
||
| def add_route( | ||
| self, | ||
| path: str, | ||
|
|
@@ -174,93 +145,3 @@ def add_websocket_route( | |
| name: str | None = None, | ||
| ) -> None: # pragma: no cover | ||
| self.router.add_websocket_route(path, route, name=name) | ||
|
|
||
| def exception_handler( | ||
| self, exc_class_or_status_code: int | type[Exception] | ||
| ) -> typing.Callable: # type: ignore[type-arg] | ||
| warnings.warn( | ||
| "The `exception_handler` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501 | ||
| "Refer to https://www.starlette.io/exceptions/ for the recommended approach.", # noqa: E501 | ||
| DeprecationWarning, | ||
| ) | ||
|
|
||
| def decorator(func: typing.Callable) -> typing.Callable: # type: ignore[type-arg] # noqa: E501 | ||
| self.add_exception_handler(exc_class_or_status_code, func) | ||
| return func | ||
|
|
||
| return decorator | ||
|
|
||
| def route( | ||
| self, | ||
| path: str, | ||
| methods: list[str] | None = None, | ||
| name: str | None = None, | ||
| include_in_schema: bool = True, | ||
| ) -> typing.Callable: # type: ignore[type-arg] | ||
| """ | ||
| We no longer document this decorator style API, and its usage is discouraged. | ||
| Instead you should use the following approach: | ||
|
|
||
| >>> routes = [Route(path, endpoint=...), ...] | ||
| >>> app = Starlette(routes=routes) | ||
| """ | ||
| warnings.warn( | ||
| "The `route` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501 | ||
| "Refer to https://www.starlette.io/routing/ for the recommended approach.", # noqa: E501 | ||
| DeprecationWarning, | ||
| ) | ||
|
|
||
| def decorator(func: typing.Callable) -> typing.Callable: # type: ignore[type-arg] # noqa: E501 | ||
| self.router.add_route( | ||
| path, | ||
| func, | ||
| methods=methods, | ||
| name=name, | ||
| include_in_schema=include_in_schema, | ||
| ) | ||
| return func | ||
|
|
||
| return decorator | ||
|
|
||
| def websocket_route(self, path: str, name: str | None = None) -> typing.Callable: # type: ignore[type-arg] | ||
| """ | ||
| We no longer document this decorator style API, and its usage is discouraged. | ||
| Instead you should use the following approach: | ||
|
|
||
| >>> routes = [WebSocketRoute(path, endpoint=...), ...] | ||
| >>> app = Starlette(routes=routes) | ||
| """ | ||
| warnings.warn( | ||
| "The `websocket_route` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501 | ||
| "Refer to https://www.starlette.io/routing/#websocket-routing for the recommended approach.", # noqa: E501 | ||
| DeprecationWarning, | ||
| ) | ||
|
|
||
| def decorator(func: typing.Callable) -> typing.Callable: # type: ignore[type-arg] # noqa: E501 | ||
| self.router.add_websocket_route(path, func, name=name) | ||
| return func | ||
|
|
||
| return decorator | ||
|
|
||
| def middleware(self, middleware_type: str) -> typing.Callable: # type: ignore[type-arg] # noqa: E501 | ||
| """ | ||
| We no longer document this decorator style API, and its usage is discouraged. | ||
| Instead you should use the following approach: | ||
|
|
||
| >>> middleware = [Middleware(...), ...] | ||
| >>> app = Starlette(middleware=middleware) | ||
| """ | ||
| warnings.warn( | ||
| "The `middleware` decorator is deprecated, and will be removed in version 1.0.0. " # noqa: E501 | ||
| "Refer to https://www.starlette.io/middleware/#using-middleware for recommended approach.", # noqa: E501 | ||
| DeprecationWarning, | ||
| ) | ||
| assert ( | ||
| middleware_type == "http" | ||
| ), 'Currently only middleware("http") is supported.' | ||
|
|
||
| def decorator(func: typing.Callable) -> typing.Callable: # type: ignore[type-arg] # noqa: E501 | ||
| self.add_middleware(BaseHTTPMiddleware, dispatch=func) | ||
| return func | ||
|
|
||
| return decorator | ||
Uh oh!
There was an error while loading. Please reload this page.