Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer committed Nov 22, 2021
1 parent a352f80 commit 009a7fe
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
14 changes: 10 additions & 4 deletions aiohttp/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,31 @@
Callable,
Dict,
Generator,
Generic,
Iterable,
List,
Optional,
Tuple,
TypeVar,
)

from multidict import CIMultiDict
from yarl import URL

from .typedefs import LooseCookies, _SafeApplication, _SafeRequest

_T = TypeVar("_T")

if TYPE_CHECKING: # pragma: no cover
from .web_exceptions import HTTPException
from .web_request import BaseRequest
from .web_request import BaseRequest, Request
from .web_response import StreamResponse
else:
BaseRequest = StreamResponse = None
HTTPException = None

class Request(Generic[_T]): ...


class AbstractRouter(ABC):
def __init__(self) -> None:
Expand Down Expand Up @@ -98,14 +104,14 @@ def freeze(self) -> None:
"""


class AbstractView(ABC):
class AbstractView(ABC, Generic[_T]):
"""Abstract class based view."""

def __init__(self, request: _SafeRequest) -> None:
def __init__(self, request: Request[_T]) -> None:
self._request = request

@property
def request(self) -> _SafeRequest:
def request(self) -> Request[_T]:
"""Request instance."""
return self._request

Expand Down
9 changes: 6 additions & 3 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
List,
Optional,
Type,
TypeVar,
Union,
cast,
)
Expand Down Expand Up @@ -59,6 +60,8 @@
else:
from asynctest import TestCase # type: ignore[no-redef]

_T = TypeVar("_T")

REUSE_ADDRESS = os.name == "posix" and sys.platform != "cygwin"


Expand Down Expand Up @@ -554,15 +557,15 @@ def make_mocked_request(
match_info: Any = sentinel,
version: HttpVersion = HttpVersion(1, 1),
closing: bool = False,
app: Any = None,
app: Optional[Application[_T]] = None,
writer: Any = sentinel,
protocol: Any = sentinel,
transport: Any = sentinel,
payload: Any = sentinel,
sslcontext: Optional[SSLContext] = None,
client_max_size: int = 1024 ** 2,
loop: Any = ...,
) -> _SafeRequest:
) -> Request[_T]:
"""Creates mocked web.Request testing purposes.
Useful in unit tests, when spinning full web server is overkill or
Expand Down Expand Up @@ -625,7 +628,7 @@ def make_mocked_request(
if payload is sentinel:
payload = mock.Mock()

req: _SafeRequest = Request(
req: Request[_T] = Request(
message, payload, protocol, writer, task, loop, client_max_size=client_max_size
)

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

__all__ = ("Application", "CleanupError")

_T = TypeVar("_T")
_T = TypeVar("_T", covariant=True)


if TYPE_CHECKING: # pragma: no cover
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
from .web_protocol import RequestHandler
from .web_urldispatcher import UrlMappingMatchInfo

_T = TypeVar("_T")
_T = TypeVar("_T", covariant=True)


@dataclasses.dataclass(frozen=True)
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_routedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def delete(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef:
return route(hdrs.METH_DELETE, path, handler, **kwargs)


def view(path: str, handler: Type[AbstractView], **kwargs: Any) -> RouteDef:
def view(path: str, handler: Type[AbstractView[Any]], **kwargs: Any) -> RouteDef:
return route(hdrs.METH_ANY, path, handler, **kwargs)


Expand Down
14 changes: 8 additions & 6 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Sized,
Tuple,
Type,
TypeVar,
Union,
cast,
)
Expand Down Expand Up @@ -81,6 +82,7 @@
PATH_SEP: Final[str] = re.escape("/")


_T = TypeVar("_T")
_ExpectHandler = Callable[[Request], Awaitable[None]]
_Resolve = Tuple[Optional[AbstractMatchInfo], Set[str]]

Expand Down Expand Up @@ -155,7 +157,7 @@ class AbstractRoute(abc.ABC):
def __init__(
self,
method: str,
handler: Union[Handler, Type[AbstractView]],
handler: Union[Handler, Type[AbstractView[Any]]],
*,
expect_handler: Optional[_ExpectHandler] = None,
resource: Optional[AbstractResource] = None,
Expand Down Expand Up @@ -320,7 +322,7 @@ def __init__(self, *, name: Optional[str] = None) -> None:
def add_route(
self,
method: str,
handler: Union[Type[AbstractView], Handler],
handler: Union[Type[AbstractView[Any]], Handler],
*,
expect_handler: Optional[_ExpectHandler] = None,
) -> "ResourceRoute":
Expand Down Expand Up @@ -862,7 +864,7 @@ class ResourceRoute(AbstractRoute):
def __init__(
self,
method: str,
handler: Union[Handler, Type[AbstractView]],
handler: Union[Handler, Type[AbstractView[Any]]],
resource: AbstractResource,
*,
expect_handler: Optional[_ExpectHandler] = None,
Expand Down Expand Up @@ -922,7 +924,7 @@ def __repr__(self) -> str:
return "<SystemRoute {self.status}: {self.reason}>".format(self=self)


class View(AbstractView):
class View(AbstractView[_T]):
async def _iter(self) -> StreamResponse:
if self.request.method not in hdrs.METH_ALL:
self._raise_allowed_methods()
Expand Down Expand Up @@ -1072,7 +1074,7 @@ def add_route(
self,
method: str,
path: str,
handler: Union[Handler, Type[AbstractView]],
handler: Union[Handler, Type[AbstractView[Any]]],
*,
name: Optional[str] = None,
expect_handler: Optional[_ExpectHandler] = None,
Expand Down Expand Up @@ -1169,7 +1171,7 @@ def add_delete(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRout
return self.add_route(hdrs.METH_DELETE, path, handler, **kwargs)

def add_view(
self, path: str, handler: Type[AbstractView], **kwargs: Any
self, path: str, handler: Type[AbstractView[Any]], **kwargs: Any
) -> AbstractRoute:
"""
Shortcut for add_route with ANY methods for a class-based view
Expand Down

0 comments on commit 009a7fe

Please sign in to comment.