-
-
Notifications
You must be signed in to change notification settings - Fork 948
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: privatize typing module * refactor: rename missing to unset * typing: define public typing module Other minor changes in the header types * docs: improved attributes docs by inlining them * typing: improve secure_filename typing * docs: fix linter issues * docs: make sphinx happy * docs: improve attribute docs * typing: finalize typing module * docs: workaround sphinx autoclass typing rendering bug * docs: add typing aliases to the docs also customize how generic aliases docs are rendered to avoid adding "alias of <the real type>" * chore: update .coveragerc typing => _typing * chore: update after review * fix: fix typo --------- Co-authored-by: Vytautas Liuolia <vytautas.liuolia@gmail.com>
- Loading branch information
Showing
42 changed files
with
714 additions
and
659 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,3 +85,9 @@ Other | |
|
||
.. autoclass:: falcon.ETag | ||
:members: | ||
|
||
Type Aliases | ||
------------ | ||
|
||
.. automodule:: falcon.typing | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""Customizations to the autodoc functionalities""" | ||
|
||
import sphinx.ext.autodoc as ad | ||
|
||
|
||
def setup(app): | ||
# avoid adding "alias of xyz" | ||
ad.GenericAliasMixin.update_content = ad.DataDocumenterMixinBase.update_content | ||
|
||
return {'parallel_read_safe': True} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
# Copyright 2021-2023 by Vytautas Liuolia. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Private type aliases used internally by Falcon..""" | ||
|
||
from __future__ import annotations | ||
|
||
from enum import auto | ||
from enum import Enum | ||
import http | ||
from http.cookiejar import Cookie | ||
import sys | ||
from typing import ( | ||
Any, | ||
Awaitable, | ||
Callable, | ||
Dict, | ||
Iterable, | ||
List, | ||
Literal, | ||
Mapping, | ||
Optional, | ||
Pattern, | ||
Protocol, | ||
Tuple, | ||
TYPE_CHECKING, | ||
TypeVar, | ||
Union, | ||
) | ||
|
||
# NOTE(vytas): Mypy still struggles to handle a conditional import in the EAFP | ||
# fashion, so we branch on Py version instead (which it does understand). | ||
if sys.version_info >= (3, 11): | ||
from wsgiref.types import StartResponse as StartResponse | ||
from wsgiref.types import WSGIEnvironment as WSGIEnvironment | ||
else: | ||
WSGIEnvironment = Dict[str, Any] | ||
StartResponse = Callable[[str, List[Tuple[str, str]]], Callable[[bytes], None]] | ||
|
||
if TYPE_CHECKING: | ||
from falcon.asgi import Request as AsgiRequest | ||
from falcon.asgi import Response as AsgiResponse | ||
from falcon.asgi import WebSocket | ||
from falcon.asgi_spec import AsgiEvent | ||
from falcon.asgi_spec import AsgiSendMsg | ||
from falcon.http_error import HTTPError | ||
from falcon.request import Request | ||
from falcon.response import Response | ||
|
||
|
||
class _Unset(Enum): | ||
UNSET = auto() | ||
|
||
|
||
_T = TypeVar('_T') | ||
_UNSET = _Unset.UNSET | ||
UnsetOr = Union[Literal[_Unset.UNSET], _T] | ||
|
||
Link = Dict[str, str] | ||
CookieArg = Mapping[str, Union[str, Cookie]] | ||
# Error handlers | ||
ErrorHandler = Callable[['Request', 'Response', BaseException, Dict[str, Any]], None] | ||
|
||
|
||
class AsgiErrorHandler(Protocol): | ||
async def __call__( | ||
self, | ||
req: AsgiRequest, | ||
resp: Optional[AsgiResponse], | ||
error: BaseException, | ||
params: Dict[str, Any], | ||
*, | ||
ws: Optional[WebSocket] = ..., | ||
) -> None: ... | ||
|
||
|
||
# Error serializers | ||
ErrorSerializer = Callable[['Request', 'Response', 'HTTPError'], None] | ||
|
||
# Sinks | ||
SinkPrefix = Union[str, Pattern[str]] | ||
|
||
|
||
class SinkCallable(Protocol): | ||
def __call__(self, req: Request, resp: Response, **kwargs: str) -> None: ... | ||
|
||
|
||
class AsgiSinkCallable(Protocol): | ||
async def __call__( | ||
self, req: AsgiRequest, resp: AsgiResponse, **kwargs: str | ||
) -> None: ... | ||
|
||
|
||
HeaderMapping = Mapping[str, str] | ||
HeaderIter = Iterable[Tuple[str, str]] | ||
HeaderArg = Union[HeaderMapping, HeaderIter] | ||
ResponseStatus = Union[http.HTTPStatus, str, int] | ||
StoreArg = Optional[Dict[str, Any]] | ||
Resource = object | ||
RangeSetHeader = Union[Tuple[int, int, int], Tuple[int, int, int, str]] | ||
|
||
|
||
# WSGI | ||
class ResponderMethod(Protocol): | ||
def __call__( | ||
self, | ||
resource: Resource, | ||
req: Request, | ||
resp: Response, | ||
**kwargs: Any, | ||
) -> None: ... | ||
|
||
|
||
class ResponderCallable(Protocol): | ||
def __call__(self, req: Request, resp: Response, **kwargs: Any) -> None: ... | ||
|
||
|
||
ProcessRequestMethod = Callable[['Request', 'Response'], None] | ||
ProcessResourceMethod = Callable[ | ||
['Request', 'Response', Resource, Dict[str, Any]], None | ||
] | ||
ProcessResponseMethod = Callable[['Request', 'Response', Resource, bool], None] | ||
|
||
|
||
# ASGI | ||
class AsgiResponderMethod(Protocol): | ||
async def __call__( | ||
self, | ||
resource: Resource, | ||
req: AsgiRequest, | ||
resp: AsgiResponse, | ||
**kwargs: Any, | ||
) -> None: ... | ||
|
||
|
||
class AsgiResponderCallable(Protocol): | ||
async def __call__( | ||
self, req: AsgiRequest, resp: AsgiResponse, **kwargs: Any | ||
) -> None: ... | ||
|
||
|
||
class AsgiResponderWsCallable(Protocol): | ||
async def __call__( | ||
self, req: AsgiRequest, ws: WebSocket, **kwargs: Any | ||
) -> None: ... | ||
|
||
|
||
AsgiReceive = Callable[[], Awaitable['AsgiEvent']] | ||
AsgiSend = Callable[['AsgiSendMsg'], Awaitable[None]] | ||
AsgiProcessRequestMethod = Callable[['AsgiRequest', 'AsgiResponse'], Awaitable[None]] | ||
AsgiProcessResourceMethod = Callable[ | ||
['AsgiRequest', 'AsgiResponse', Resource, Dict[str, Any]], Awaitable[None] | ||
] | ||
AsgiProcessResponseMethod = Callable[ | ||
['AsgiRequest', 'AsgiResponse', Resource, bool], Awaitable[None] | ||
] | ||
AsgiProcessRequestWsMethod = Callable[['AsgiRequest', 'WebSocket'], Awaitable[None]] | ||
AsgiProcessResourceWsMethod = Callable[ | ||
['AsgiRequest', 'WebSocket', Resource, Dict[str, Any]], Awaitable[None] | ||
] | ||
ResponseCallbacks = Union[ | ||
Tuple[Callable[[], None], Literal[False]], | ||
Tuple[Callable[[], Awaitable[None]], Literal[True]], | ||
] | ||
|
||
|
||
# Routing | ||
|
||
MethodDict = Union[ | ||
Dict[str, ResponderCallable], | ||
Dict[str, Union[AsgiResponderCallable, AsgiResponderWsCallable]], | ||
] | ||
|
||
|
||
class FindMethod(Protocol): | ||
def __call__( | ||
self, uri: str, req: Optional[Request] | ||
) -> Optional[Tuple[object, MethodDict, Dict[str, Any], Optional[str]]]: ... | ||
|
||
|
||
# Media | ||
class SerializeSync(Protocol): | ||
def __call__(self, media: Any, content_type: Optional[str] = ...) -> bytes: ... | ||
|
||
|
||
DeserializeSync = Callable[[bytes], Any] | ||
|
||
Responder = Union[ResponderMethod, AsgiResponderMethod] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.