From 50cb6de44839b91c63e7ff333d6f6dd391b8e1e8 Mon Sep 17 00:00:00 2001 From: SamirAk <44325916+SamirPS@users.noreply.github.com> Date: Sat, 23 Jul 2022 02:37:35 +0200 Subject: [PATCH 1/9] Update web.py --- aiohttp/web.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/aiohttp/web.py b/aiohttp/web.py index c0348c8d097..6b6a0a2f29b 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -1,5 +1,6 @@ import asyncio import logging +import pathlib import socket import sys from argparse import ArgumentParser @@ -290,7 +291,7 @@ async def _run_app( *, host: Optional[Union[str, HostSequence]] = None, port: Optional[int] = None, - path: Optional[str] = None, + path: Optional[Union[str, pathlib.Path]] = None, sock: Optional[Union[socket.socket, TypingIterable[socket.socket]]] = None, shutdown_timeout: float = 60.0, keepalive_timeout: float = 75.0, @@ -366,7 +367,7 @@ async def _run_app( ) if path is not None: - if isinstance(path, (str, bytes, bytearray, memoryview)): + if isinstance(path, (str, bytes, bytearray, memoryview, pathlib.Path)): sites.append( UnixSite( runner, @@ -464,7 +465,7 @@ def run_app( debug: bool = False, host: Optional[Union[str, HostSequence]] = None, port: Optional[int] = None, - path: Optional[str] = None, + path: Optional[Union[str, pathlib.Path]] = None, sock: Optional[Union[socket.socket, TypingIterable[socket.socket]]] = None, shutdown_timeout: float = 60.0, keepalive_timeout: float = 75.0, From d5764891961a05681c0e8f8a8137ed1811e5fe8b Mon Sep 17 00:00:00 2001 From: SamirAk <44325916+SamirPS@users.noreply.github.com> Date: Sat, 23 Jul 2022 02:38:58 +0200 Subject: [PATCH 2/9] Create 6839.issue --- CHANGES/6839.issue | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/6839.issue diff --git a/CHANGES/6839.issue b/CHANGES/6839.issue new file mode 100644 index 00000000000..8e1b9f6d1a3 --- /dev/null +++ b/CHANGES/6839.issue @@ -0,0 +1 @@ +web.run_app(path=) accept pathlib.Path From 0e8360f4af381132bce4dcfd99daa7df3600c698 Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 02:45:09 +0200 Subject: [PATCH 3/9] change name --- CHANGES/{6839.issue => 6839.feature} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename CHANGES/{6839.issue => 6839.feature} (100%) diff --git a/CHANGES/6839.issue b/CHANGES/6839.feature similarity index 100% rename from CHANGES/6839.issue rename to CHANGES/6839.feature From e4fcb74b3e319bba897c7885b441699c68c3365b Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 14:09:42 +0200 Subject: [PATCH 4/9] modify test and add contribution --- CONTRIBUTORS.txt | 1 + tests/test_run_app.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index bd7677f5f29..bda74215d50 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -267,6 +267,7 @@ Robert Lu Robert Nikolich Roman Markeloff Roman Podoliaka +Samir Akarioh Samuel Colvin Sean Hunt Sebastian Acuna diff --git a/tests/test_run_app.py b/tests/test_run_app.py index 835f2c6714f..cd91d4b4282 100644 --- a/tests/test_run_app.py +++ b/tests/test_run_app.py @@ -518,7 +518,7 @@ def test_run_app_custom_backlog_unix(patched_loop: Any) -> None: def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None: app = web.Application() - sock_path = str(tmp_path / "socket.sock") + sock_path = tmp_path / "socket.sock" printer = mock.Mock(wraps=stopper(patched_loop)) web.run_app(app, path=sock_path, print=printer, loop=patched_loop) @@ -532,7 +532,7 @@ def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None: def test_run_app_https_unix_socket(patched_loop: Any, tmp_path: Any) -> None: app = web.Application() - sock_path = str(tmp_path / "socket.sock") + sock_path = tmp_path / "socket.sock" ssl_context = ssl.create_default_context() printer = mock.Mock(wraps=stopper(patched_loop)) web.run_app( From 6391e9e51f31c7e38e1abfd5e80f129b64c1d9c1 Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 14:55:16 +0200 Subject: [PATCH 5/9] path now str and path from pathlib --- aiohttp/client_exceptions.py | 8 +++-- aiohttp/connector.py | 9 ++--- aiohttp/cookiejar.py | 11 ++++-- aiohttp/helpers.py | 8 +++-- aiohttp/http_parser.py | 3 +- aiohttp/test_utils.py | 51 ++++++++++++++++++++-------- aiohttp/web.py | 8 ++--- aiohttp/web_middlewares.py | 7 ++-- aiohttp/web_routedef.py | 57 ++++++++++++++++++++----------- aiohttp/web_runner.py | 11 ++++-- aiohttp/web_urldispatcher.py | 59 ++++++++++++++++++++++----------- tests/autobahn/test_autobahn.py | 6 ++-- tests/test_circular_imports.py | 4 +-- tests/test_client_session.py | 5 +-- 14 files changed, 169 insertions(+), 78 deletions(-) diff --git a/aiohttp/client_exceptions.py b/aiohttp/client_exceptions.py index d7f75a0dbd5..02507673aa5 100644 --- a/aiohttp/client_exceptions.py +++ b/aiohttp/client_exceptions.py @@ -1,6 +1,7 @@ """HTTP related errors.""" import asyncio +from pathlib import Path from typing import TYPE_CHECKING, Any, Optional, Tuple, Union from .http_parser import RawResponseMessage @@ -175,13 +176,16 @@ class UnixClientConnectorError(ClientConnectorError): """ def __init__( - self, path: str, connection_key: ConnectionKey, os_error: OSError + self, + path: Optional[Union[str, Path]], + connection_key: ConnectionKey, + os_error: OSError, ) -> None: self._path = path super().__init__(connection_key, os_error) @property - def path(self) -> str: + def path(self) -> Optional[Union[str, Path]]: return self._path def __str__(self) -> str: diff --git a/aiohttp/connector.py b/aiohttp/connector.py index 86399b84272..f1ee4614750 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -10,6 +10,7 @@ from contextlib import suppress from http.cookies import SimpleCookie from itertools import cycle, islice +from pathlib import Path from time import monotonic from types import TracebackType from typing import ( # noqa @@ -1263,7 +1264,7 @@ class UnixConnector(BaseConnector): def __init__( self, - path: str, + path: Optional[Union[str, Path]], force_close: bool = False, keepalive_timeout: Union[_SENTINEL, float, None] = sentinel, limit: int = 100, @@ -1278,7 +1279,7 @@ def __init__( self._path = path @property - def path(self) -> str: + def path(self) -> Optional[Union[str, Path]]: """Path to unix socket.""" return self._path @@ -1315,7 +1316,7 @@ class NamedPipeConnector(BaseConnector): def __init__( self, - path: str, + path: Optional[Union[str, Path]], force_close: bool = False, keepalive_timeout: Union[_SENTINEL, float, None] = sentinel, limit: int = 100, @@ -1336,7 +1337,7 @@ def __init__( self._path = path @property - def path(self) -> str: + def path(self) -> Optional[Union[str, Path]]: """Path to the named pipe.""" return self._path diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index 9a2bf192629..a0afaa8eb6d 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -8,6 +8,7 @@ import warnings from collections import defaultdict from http.cookies import BaseCookie, Morsel, SimpleCookie +from pathlib import Path from typing import ( # noqa DefaultDict, Dict, @@ -150,7 +151,11 @@ def _do_expiration(self) -> None: self.clear(lambda x: False) def _expire_cookie( - self, when: datetime.datetime, domain: str, path: str, name: str + self, + when: datetime.datetime, + domain: str, + path: Optional[Union[str, Path]], + name: str, ) -> None: self._next_expiration = min(self._next_expiration, when) self._expirations[(domain, path, name)] = when @@ -307,7 +312,9 @@ def _is_domain_match(domain: str, hostname: str) -> bool: return not is_ip_address(hostname) @staticmethod - def _is_path_match(req_path: str, cookie_path: str) -> bool: + def _is_path_match( + req_path: Optional[Union[str, Path]], cookie_path: Optional[Union[str, Path]] + ) -> bool: """Implements path matching adhering to RFC 6265.""" if not req_path.startswith("/"): req_path = "/" diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 8e2502ac53b..46f58f39669 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -933,7 +933,7 @@ def set_cookie( expires: Optional[str] = None, domain: Optional[str] = None, max_age: Optional[Union[int, str]] = None, - path: str = "/", + path: Optional[Union[str, Path]] = "/", secure: Optional[bool] = None, httponly: Optional[bool] = None, version: Optional[str] = None, @@ -986,7 +986,11 @@ def set_cookie( ) def del_cookie( - self, name: str, *, domain: Optional[str] = None, path: str = "/" + self, + name: str, + *, + domain: Optional[str] = None, + path: Optional[Union[str, Path]] = "/", ) -> None: """Delete cookie. diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index dd1c0850e5e..fd156aaa7c7 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -6,6 +6,7 @@ import zlib from contextlib import suppress from enum import IntEnum +from pathlib import Path from typing import ( Any, Generic, @@ -75,7 +76,7 @@ class RawRequestMessage(NamedTuple): method: str - path: str + path: Optional[Union[str, Path]] version: HttpVersion headers: CIMultiDictProxy[str] raw_headers: RawHeaders diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 82956986b9d..42e65d8ba57 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -9,6 +9,7 @@ import socket import sys from abc import ABC, abstractmethod +from pathlib import Path from types import TracebackType from typing import ( TYPE_CHECKING, @@ -148,7 +149,7 @@ async def start_server(self, **kwargs: Any) -> None: async def _make_runner(self, **kwargs: Any) -> BaseRunner: pass - def make_url(self, path: str) -> URL: + def make_url(self, path: Optional[Union[str, Path]]) -> URL: assert self._root is not None url = URL(path) if not self.skip_url_asserts: @@ -304,16 +305,20 @@ def session(self) -> ClientSession: """ return self._session - def make_url(self, path: str) -> URL: + def make_url(self, path: Optional[Union[str, Path]]) -> URL: return self._server.make_url(path) - async def _request(self, method: str, path: str, **kwargs: Any) -> ClientResponse: + async def _request( + self, method: str, path: Optional[Union[str, Path]], **kwargs: Any + ) -> ClientResponse: resp = await self._session.request(method, self.make_url(path), **kwargs) # save it to close later self._responses.append(resp) return resp - def request(self, method: str, path: str, **kwargs: Any) -> _RequestContextManager: + def request( + self, method: str, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Routes a request to tested http server. The interface is identical to aiohttp.ClientSession.request, @@ -323,35 +328,51 @@ def request(self, method: str, path: str, **kwargs: Any) -> _RequestContextManag """ return _RequestContextManager(self._request(method, path, **kwargs)) - def get(self, path: str, **kwargs: Any) -> _RequestContextManager: + def get( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP GET request.""" return _RequestContextManager(self._request(hdrs.METH_GET, path, **kwargs)) - def post(self, path: str, **kwargs: Any) -> _RequestContextManager: + def post( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP POST request.""" return _RequestContextManager(self._request(hdrs.METH_POST, path, **kwargs)) - def options(self, path: str, **kwargs: Any) -> _RequestContextManager: + def options( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP OPTIONS request.""" return _RequestContextManager(self._request(hdrs.METH_OPTIONS, path, **kwargs)) - def head(self, path: str, **kwargs: Any) -> _RequestContextManager: + def head( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP HEAD request.""" return _RequestContextManager(self._request(hdrs.METH_HEAD, path, **kwargs)) - def put(self, path: str, **kwargs: Any) -> _RequestContextManager: + def put( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP PUT request.""" return _RequestContextManager(self._request(hdrs.METH_PUT, path, **kwargs)) - def patch(self, path: str, **kwargs: Any) -> _RequestContextManager: + def patch( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP PATCH request.""" return _RequestContextManager(self._request(hdrs.METH_PATCH, path, **kwargs)) - def delete(self, path: str, **kwargs: Any) -> _RequestContextManager: + def delete( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _RequestContextManager: """Perform an HTTP PATCH request.""" return _RequestContextManager(self._request(hdrs.METH_DELETE, path, **kwargs)) - def ws_connect(self, path: str, **kwargs: Any) -> _WSRequestContextManager: + def ws_connect( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _WSRequestContextManager: """Initiate websocket connection. The api corresponds to aiohttp.ClientSession.ws_connect. @@ -359,7 +380,9 @@ def ws_connect(self, path: str, **kwargs: Any) -> _WSRequestContextManager: """ return _WSRequestContextManager(self._ws_connect(path, **kwargs)) - async def _ws_connect(self, path: str, **kwargs: Any) -> ClientWebSocketResponse: + async def _ws_connect( + self, path: Optional[Union[str, Path]], **kwargs: Any + ) -> ClientWebSocketResponse: ws = await self._session.ws_connect(self.make_url(path), **kwargs) self._websockets.append(ws) return ws @@ -555,7 +578,7 @@ def get_extra_info(key: str) -> Optional[SSLContext]: def make_mocked_request( method: str, - path: str, + path: Optional[Union[str, Path]], headers: Any = None, *, match_info: Any = sentinel, diff --git a/aiohttp/web.py b/aiohttp/web.py index 6b6a0a2f29b..e771a61591c 100644 --- a/aiohttp/web.py +++ b/aiohttp/web.py @@ -1,11 +1,11 @@ import asyncio import logging -import pathlib import socket import sys from argparse import ArgumentParser from collections.abc import Iterable from importlib import import_module +from pathlib import Path from typing import ( Any, Awaitable, @@ -291,7 +291,7 @@ async def _run_app( *, host: Optional[Union[str, HostSequence]] = None, port: Optional[int] = None, - path: Optional[Union[str, pathlib.Path]] = None, + path: Optional[Union[str, Path]] = None, sock: Optional[Union[socket.socket, TypingIterable[socket.socket]]] = None, shutdown_timeout: float = 60.0, keepalive_timeout: float = 75.0, @@ -367,7 +367,7 @@ async def _run_app( ) if path is not None: - if isinstance(path, (str, bytes, bytearray, memoryview, pathlib.Path)): + if isinstance(path, (str, bytes, bytearray, memoryview, Path)): sites.append( UnixSite( runner, @@ -465,7 +465,7 @@ def run_app( debug: bool = False, host: Optional[Union[str, HostSequence]] = None, port: Optional[int] = None, - path: Optional[Union[str, pathlib.Path]] = None, + path: Optional[Union[str, Path]] = None, sock: Optional[Union[socket.socket, TypingIterable[socket.socket]]] = None, shutdown_timeout: float = 60.0, keepalive_timeout: float = 75.0, diff --git a/aiohttp/web_middlewares.py b/aiohttp/web_middlewares.py index 45838244584..1e44c8f25cf 100644 --- a/aiohttp/web_middlewares.py +++ b/aiohttp/web_middlewares.py @@ -1,6 +1,7 @@ import re import warnings -from typing import TYPE_CHECKING, Tuple, Type, TypeVar +from pathlib import Path +from typing import TYPE_CHECKING, Optional, Tuple, Type, TypeVar, Union from .typedefs import Handler, Middleware from .web_exceptions import HTTPMove, HTTPPermanentRedirect @@ -19,7 +20,9 @@ _Func = TypeVar("_Func") -async def _check_request_resolves(request: Request, path: str) -> Tuple[bool, Request]: +async def _check_request_resolves( + request: Request, path: Optional[Union[str, Path]] +) -> Tuple[bool, Request]: alt_request = request.clone(rel_url=path) match_info = await request.app.router.resolve(alt_request) diff --git a/aiohttp/web_routedef.py b/aiohttp/web_routedef.py index e1ea3c16217..6ee005fec40 100644 --- a/aiohttp/web_routedef.py +++ b/aiohttp/web_routedef.py @@ -1,6 +1,7 @@ import abc import dataclasses import os # noqa +from pathlib import Path from typing import ( TYPE_CHECKING, Any, @@ -57,7 +58,7 @@ def register(self, router: UrlDispatcher) -> List[AbstractRoute]: @dataclasses.dataclass(frozen=True, repr=False) class RouteDef(AbstractRouteDef): method: str - path: str + path: Optional[Union[str, Path]] handler: _HandlerType kwargs: Dict[str, Any] @@ -99,20 +100,26 @@ def register(self, router: UrlDispatcher) -> List[AbstractRoute]: return list(routes.values()) -def route(method: str, path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def route( + method: str, path: Optional[Union[str, Path]], handler: _HandlerType, **kwargs: Any +) -> RouteDef: return RouteDef(method, path, handler, kwargs) -def head(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def head( + path: Optional[Union[str, Path]], handler: _HandlerType, **kwargs: Any +) -> RouteDef: return route(hdrs.METH_HEAD, path, handler, **kwargs) -def options(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def options( + path: Optional[Union[str, Path]], handler: _HandlerType, **kwargs: Any +) -> RouteDef: return route(hdrs.METH_OPTIONS, path, handler, **kwargs) def get( - path: str, + path: Optional[Union[str, Path]], handler: _HandlerType, *, name: Optional[str] = None, @@ -124,23 +131,33 @@ def get( ) -def post(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def post( + path: Optional[Union[str, Path]], handler: _HandlerType, **kwargs: Any +) -> RouteDef: return route(hdrs.METH_POST, path, handler, **kwargs) -def put(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def put( + path: Optional[Union[str, Path]], handler: _HandlerType, **kwargs: Any +) -> RouteDef: return route(hdrs.METH_PUT, path, handler, **kwargs) -def patch(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def patch( + path: Optional[Union[str, Path]], handler: _HandlerType, **kwargs: Any +) -> RouteDef: return route(hdrs.METH_PATCH, path, handler, **kwargs) -def delete(path: str, handler: _HandlerType, **kwargs: Any) -> RouteDef: +def delete( + path: Optional[Union[str, Path]], 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: Optional[Union[str, Path]], handler: Type[AbstractView], **kwargs: Any +) -> RouteDef: return route(hdrs.METH_ANY, path, handler, **kwargs) @@ -180,35 +197,37 @@ def __len__(self) -> int: def __contains__(self, item: object) -> bool: return item in self._items - def route(self, method: str, path: str, **kwargs: Any) -> _Deco: + def route( + self, method: str, path: Optional[Union[str, Path]], **kwargs: Any + ) -> _Deco: def inner(handler: _HandlerType) -> _HandlerType: self._items.append(RouteDef(method, path, handler, kwargs)) return handler return inner - def head(self, path: str, **kwargs: Any) -> _Deco: + def head(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_HEAD, path, **kwargs) - def get(self, path: str, **kwargs: Any) -> _Deco: + def get(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_GET, path, **kwargs) - def post(self, path: str, **kwargs: Any) -> _Deco: + def post(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_POST, path, **kwargs) - def put(self, path: str, **kwargs: Any) -> _Deco: + def put(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_PUT, path, **kwargs) - def patch(self, path: str, **kwargs: Any) -> _Deco: + def patch(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_PATCH, path, **kwargs) - def delete(self, path: str, **kwargs: Any) -> _Deco: + def delete(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_DELETE, path, **kwargs) - def options(self, path: str, **kwargs: Any) -> _Deco: + def options(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_OPTIONS, path, **kwargs) - def view(self, path: str, **kwargs: Any) -> _Deco: + def view(self, path: Optional[Union[str, Path]], **kwargs: Any) -> _Deco: return self.route(hdrs.METH_ANY, path, **kwargs) def static(self, prefix: str, path: PathLike, **kwargs: Any) -> None: diff --git a/aiohttp/web_runner.py b/aiohttp/web_runner.py index 6fb13b396bc..660b62f7c45 100644 --- a/aiohttp/web_runner.py +++ b/aiohttp/web_runner.py @@ -2,7 +2,8 @@ import signal import socket from abc import ABC, abstractmethod -from typing import Any, List, Optional, Set, Type +from pathlib import Path +from typing import Any, List, Optional, Set, Type, Union from yarl import URL @@ -141,7 +142,7 @@ class UnixSite(BaseSite): def __init__( self, runner: "BaseRunner", - path: str, + path: Optional[Union[str, Path]], *, shutdown_timeout: float = 60.0, ssl_context: Optional[SSLContext] = None, @@ -174,7 +175,11 @@ class NamedPipeSite(BaseSite): __slots__ = ("_path",) def __init__( - self, runner: "BaseRunner", path: str, *, shutdown_timeout: float = 60.0 + self, + runner: "BaseRunner", + path: Optional[Union[str, Path]], + *, + shutdown_timeout: float = 60.0, ) -> None: loop = asyncio.get_event_loop() if not isinstance( diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index c5efdcbfe7e..ee49c480dea 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -87,7 +87,7 @@ class _InfoDict(TypedDict, total=False): - path: str + path: Optional[Union[str, Path]] formatter: str pattern: Pattern[str] @@ -148,7 +148,7 @@ def freeze(self) -> None: pass @abc.abstractmethod - def raw_match(self, path: str) -> bool: + def raw_match(self, path: Optional[Union[str, Path]]) -> bool: """Perform a raw match against path""" @@ -361,7 +361,7 @@ async def resolve(self, request: Request) -> _Resolve: return None, allowed_methods @abc.abstractmethod - def _match(self, path: str) -> Optional[Dict[str, str]]: + def _match(self, path: Optional[Union[str, Path]]) -> Optional[Dict[str, str]]: pass # pragma: no cover def __len__(self) -> int: @@ -374,7 +374,9 @@ def __iter__(self) -> Iterator["ResourceRoute"]: class PlainResource(Resource): - def __init__(self, path: str, *, name: Optional[str] = None) -> None: + def __init__( + self, path: Optional[Union[str, Path]], *, name: Optional[str] = None + ) -> None: super().__init__(name=name) assert not path or path.startswith("/") self._path = path @@ -393,14 +395,14 @@ def add_prefix(self, prefix: str) -> None: assert len(prefix) > 1 self._path = prefix + self._path - def _match(self, path: str) -> Optional[Dict[str, str]]: + def _match(self, path: Optional[Union[str, Path]]) -> Optional[Dict[str, str]]: # string comparison is about 10 times faster than regexp matching if self._path == path: return {} else: return None - def raw_match(self, path: str) -> bool: + def raw_match(self, path: Optional[Union[str, Path]]) -> bool: return self._path == path def get_info(self) -> _InfoDict: @@ -420,7 +422,9 @@ class DynamicResource(Resource): DYN_WITH_RE = re.compile(r"\{(?P[_a-zA-Z][_a-zA-Z0-9]*):(?P.+)\}") GOOD = r"[^{}/]+" - def __init__(self, path: str, *, name: Optional[str] = None) -> None: + def __init__( + self, path: Optional[Union[str, Path]], *, name: Optional[str] = None + ) -> None: super().__init__(name=name) pattern = "" formatter = "" @@ -464,7 +468,7 @@ def add_prefix(self, prefix: str) -> None: self._pattern = re.compile(re.escape(prefix) + self._pattern.pattern) self._formatter = prefix + self._formatter - def _match(self, path: str) -> Optional[Dict[str, str]]: + def _match(self, path: Optional[Union[str, Path]]) -> Optional[Dict[str, str]]: match = self._pattern.fullmatch(path) if match is None: return None @@ -473,7 +477,7 @@ def _match(self, path: str) -> Optional[Dict[str, str]]: key: _unquote_path(value) for key, value in match.groupdict().items() } - def raw_match(self, path: str) -> bool: + def raw_match(self, path: Optional[Union[str, Path]]) -> bool: return self._formatter == path def get_info(self) -> _InfoDict: @@ -1054,7 +1058,9 @@ def register_resource(self, resource: AbstractResource) -> None: self._named_resources[name] = resource self._resources.append(resource) - def add_resource(self, path: str, *, name: Optional[str] = None) -> Resource: + def add_resource( + self, path: Optional[Union[str, Path]], *, name: Optional[str] = None + ) -> Resource: if path and not path.startswith("/"): raise ValueError("path should be started with / or be empty") # Reuse last added resource if path and name are the same @@ -1073,7 +1079,7 @@ def add_resource(self, path: str, *, name: Optional[str] = None) -> Resource: def add_route( self, method: str, - path: str, + path: Optional[Union[str, Path]], handler: Union[Handler, Type[AbstractView]], *, name: Optional[str] = None, @@ -1116,17 +1122,21 @@ def add_static( self.register_resource(resource) return resource - def add_head(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute: + def add_head( + self, path: Optional[Union[str, Path]], handler: Handler, **kwargs: Any + ) -> AbstractRoute: """Shortcut for add_route with method HEAD.""" return self.add_route(hdrs.METH_HEAD, path, handler, **kwargs) - def add_options(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute: + def add_options( + self, path: Optional[Union[str, Path]], handler: Handler, **kwargs: Any + ) -> AbstractRoute: """Shortcut for add_route with method OPTIONS.""" return self.add_route(hdrs.METH_OPTIONS, path, handler, **kwargs) def add_get( self, - path: str, + path: Optional[Union[str, Path]], handler: Handler, *, name: Optional[str] = None, @@ -1143,24 +1153,35 @@ def add_get( resource.add_route(hdrs.METH_HEAD, handler, **kwargs) return resource.add_route(hdrs.METH_GET, handler, **kwargs) - def add_post(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute: + def add_post( + self, path: Optional[Union[str, Path]], handler: Handler, **kwargs: Any + ) -> AbstractRoute: """Shortcut for add_route with method POST.""" return self.add_route(hdrs.METH_POST, path, handler, **kwargs) - def add_put(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute: + def add_put( + self, path: Optional[Union[str, Path]], handler: Handler, **kwargs: Any + ) -> AbstractRoute: """Shortcut for add_route with method PUT.""" return self.add_route(hdrs.METH_PUT, path, handler, **kwargs) - def add_patch(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute: + def add_patch( + self, path: Optional[Union[str, Path]], handler: Handler, **kwargs: Any + ) -> AbstractRoute: """Shortcut for add_route with method PATCH.""" return self.add_route(hdrs.METH_PATCH, path, handler, **kwargs) - def add_delete(self, path: str, handler: Handler, **kwargs: Any) -> AbstractRoute: + def add_delete( + self, path: Optional[Union[str, Path]], handler: Handler, **kwargs: Any + ) -> AbstractRoute: """Shortcut for add_route with method DELETE.""" return self.add_route(hdrs.METH_DELETE, path, handler, **kwargs) def add_view( - self, path: str, handler: Type[AbstractView], **kwargs: Any + self, + path: Optional[Union[str, Path]], + handler: Type[AbstractView], + **kwargs: Any, ) -> AbstractRoute: """Shortcut for add_route with ANY methods for a class-based view.""" return self.add_route(hdrs.METH_ANY, path, handler, **kwargs) diff --git a/tests/autobahn/test_autobahn.py b/tests/autobahn/test_autobahn.py index 5d72e37a17a..57f092f9874 100644 --- a/tests/autobahn/test_autobahn.py +++ b/tests/autobahn/test_autobahn.py @@ -2,7 +2,7 @@ import subprocess import sys from pathlib import Path -from typing import Any, Dict, Generator, List +from typing import Any, Dict, Generator, List, Optional, Union import pytest from pytest import TempPathFactory @@ -32,7 +32,9 @@ def build_autobahn_testsuite() -> Generator[None, None, None]: docker.image.remove(x="autobahn-testsuite") -def get_failed_tests(report_path: str, name: str) -> List[Dict[str, Any]]: +def get_failed_tests( + report_path: Optional[Union[str, Path]], name: str +) -> List[Dict[str, Any]]: path = Path(report_path) result_summary = json.loads((path / "index.json").read_text())[name] failed_messages = [] diff --git a/tests/test_circular_imports.py b/tests/test_circular_imports.py index 7181e5c6f6b..f558d63e042 100644 --- a/tests/test_circular_imports.py +++ b/tests/test_circular_imports.py @@ -15,7 +15,7 @@ from itertools import chain from pathlib import Path from types import ModuleType -from typing import Generator, List +from typing import Generator, List, Optional, Union import pytest @@ -65,7 +65,7 @@ def _discover_path_importables( "import_path", _find_all_importables(aiohttp), ) -def test_no_warnings(import_path: str) -> None: +def test_no_warnings(import_path: Optional[Union[str, Path]]) -> None: """Verify that exploding importables doesn't explode. This is seeking for any import errors including ones caused diff --git a/tests/test_client_session.py b/tests/test_client_session.py index ff90a52656c..e343b886768 100644 --- a/tests/test_client_session.py +++ b/tests/test_client_session.py @@ -6,7 +6,8 @@ import sys from http.cookies import SimpleCookie from io import BytesIO -from typing import Any, List +from pathlib import Path +from typing import Any, List, Optional, Union from unittest import mock import pytest @@ -636,7 +637,7 @@ def reset_mocks() -> None: def to_trace_urls(mock_func: mock.Mock) -> List[URL]: return [call_args[0][-1].url for call_args in mock_func.call_args_list] - def to_url(path: str) -> URL: + def to_url(path: Optional[Union[str, Path]]) -> URL: return session.make_url(path) # Standard From 5808b7c52e4a430779124404af6c0b63847e1816 Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 15:06:31 +0200 Subject: [PATCH 6/9] fix lgtm issue --- aiohttp/cookiejar.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index a0afaa8eb6d..29d08f7669e 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -2,7 +2,6 @@ import contextlib import datetime import os # noqa -import pathlib import pickle import re import warnings @@ -94,12 +93,12 @@ def __init__( self._max_time = self.MAX_32BIT_TIME def save(self, file_path: PathLike) -> None: - file_path = pathlib.Path(file_path) + file_path = Path(file_path) with file_path.open(mode="wb") as f: pickle.dump(self._cookies, f, pickle.HIGHEST_PROTOCOL) def load(self, file_path: PathLike) -> None: - file_path = pathlib.Path(file_path) + file_path = Path(file_path) with file_path.open(mode="rb") as f: self._cookies = pickle.load(f) From 830951fbc597677883f1c35f1239728a1a4a837e Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 15:15:39 +0200 Subject: [PATCH 7/9] change import pathlib to from pathlib import --- aiohttp/web_fileresponse.py | 6 +-- examples/fake_server.py | 4 +- examples/static_files.py | 4 +- setup.py | 4 +- tests/test_client_functional.py | 4 +- tests/test_client_request.py | 10 ++--- tests/test_cookiejar.py | 4 +- tests/test_multipart.py | 8 ++-- tests/test_proxy_functional.py | 4 +- tests/test_route_def.py | 4 +- tests/test_urldispatch.py | 60 +++++++++++++-------------- tests/test_web_functional.py | 14 +++---- tests/test_web_sendfile_functional.py | 34 +++++++-------- tests/test_web_urldispatcher.py | 10 ++--- tools/check_sum.py | 4 +- tools/gen.py | 4 +- 16 files changed, 89 insertions(+), 89 deletions(-) diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index d6dce27df63..5c775624084 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -1,7 +1,7 @@ import asyncio import mimetypes import os -import pathlib +from pathlib import Path from typing import ( # noqa IO, TYPE_CHECKING, @@ -47,7 +47,7 @@ class FileResponse(StreamResponse): def __init__( self, - path: Union[str, pathlib.Path], + path: Union[str, Path], chunk_size: int = 256 * 1024, status: int = 200, reason: Optional[str] = None, @@ -56,7 +56,7 @@ def __init__( super().__init__(status=status, reason=reason, headers=headers) if isinstance(path, str): - path = pathlib.Path(path) + path = Path(path) self._path = path self._chunk_size = chunk_size diff --git a/examples/fake_server.py b/examples/fake_server.py index 065d2d779eb..e1a11eb361d 100755 --- a/examples/fake_server.py +++ b/examples/fake_server.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 import asyncio -import pathlib +from pathlib import Path import socket import ssl from typing import Any, Dict, List, Union @@ -52,7 +52,7 @@ def __init__(self) -> None: ] ) self.runner = web.AppRunner(self.app) - here = pathlib.Path(__file__) + here = Path(__file__) ssl_cert = here.parent / "server.crt" ssl_key = here.parent / "server.key" self.ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) diff --git a/examples/static_files.py b/examples/static_files.py index 65f6bb9c764..7cd785f98cf 100755 --- a/examples/static_files.py +++ b/examples/static_files.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 -import pathlib +from pathlib import Path from aiohttp import web app = web.Application() -app.router.add_static("/", pathlib.Path(__file__).parent, show_index=True) +app.router.add_static("/", Path(__file__).parent, show_index=True) web.run_app(app) diff --git a/setup.py b/setup.py index b27a54d614b..35a5a63991b 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ import os -import pathlib +from pathlib import Path import sys from setuptools import Extension, setup @@ -9,7 +9,7 @@ NO_EXTENSIONS: bool = bool(os.environ.get("AIOHTTP_NO_EXTENSIONS")) -HERE = pathlib.Path(__file__).parent +HERE = Path(__file__).parent IS_GIT_REPO = (HERE / ".git").exists() diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index b77bd516788..9eb1673e18d 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -6,7 +6,7 @@ import http.cookies import io import json -import pathlib +from pathlib import Path import socket import ssl from typing import Any @@ -25,7 +25,7 @@ @pytest.fixture def here(): - return pathlib.Path(__file__).parent + return Path(__file__).parent @pytest.fixture diff --git a/tests/test_client_request.py b/tests/test_client_request.py index 6810efd7cb5..509321ac846 100644 --- a/tests/test_client_request.py +++ b/tests/test_client_request.py @@ -2,7 +2,7 @@ import asyncio import hashlib import io -import pathlib +from pathlib import Path import zlib from http.cookies import BaseCookie, Morsel, SimpleCookie from typing import Any @@ -655,7 +655,7 @@ async def test_urlencoded_formdata_charset(loop: Any, conn: Any) -> None: async def test_formdata_boundary_from_headers(loop: Any, conn: Any) -> None: boundary = "some_boundary" - file_path = pathlib.Path(__file__).parent / "aiohttp.png" + file_path = Path(__file__).parent / "aiohttp.png" with file_path.open("rb") as f: req = ClientRequest( "post", @@ -849,7 +849,7 @@ async def test_chunked_transfer_encoding(loop: Any, conn: Any) -> None: async def test_file_upload_not_chunked(loop: Any) -> None: - file_path = pathlib.Path(__file__).parent / "aiohttp.png" + file_path = Path(__file__).parent / "aiohttp.png" with file_path.open("rb") as f: req = ClientRequest("post", URL("http://python.org/"), data=f, loop=loop) assert not req.chunked @@ -874,7 +874,7 @@ async def test_precompressed_data_stays_intact(loop: Any) -> None: async def test_file_upload_not_chunked_seek(loop: Any) -> None: - file_path = pathlib.Path(__file__).parent / "aiohttp.png" + file_path = Path(__file__).parent / "aiohttp.png" with file_path.open("rb") as f: f.seek(100) req = ClientRequest("post", URL("http://python.org/"), data=f, loop=loop) @@ -883,7 +883,7 @@ async def test_file_upload_not_chunked_seek(loop: Any) -> None: async def test_file_upload_force_chunked(loop: Any) -> None: - file_path = pathlib.Path(__file__).parent / "aiohttp.png" + file_path = Path(__file__).parent / "aiohttp.png" with file_path.open("rb") as f: req = ClientRequest( "post", URL("http://python.org/"), data=f, chunked=True, loop=loop diff --git a/tests/test_cookiejar.py b/tests/test_cookiejar.py index 84e3727797e..7bb4a9e8ad1 100644 --- a/tests/test_cookiejar.py +++ b/tests/test_cookiejar.py @@ -2,7 +2,7 @@ import asyncio import datetime import itertools -import pathlib +from pathlib import Path import unittest from http.cookies import BaseCookie, Morsel, SimpleCookie from typing import Any @@ -194,7 +194,7 @@ async def test_constructor_with_expired( async def test_save_load( tmp_path: Any, loop: Any, cookies_to_send: Any, cookies_to_receive: Any ) -> None: - file_path = pathlib.Path(str(tmp_path)) / "aiohttp.test.cookie" + file_path = Path(str(tmp_path)) / "aiohttp.test.cookie" # export cookie jar jar_save = CookieJar() diff --git a/tests/test_multipart.py b/tests/test_multipart.py index dfc5c376c80..54ba5d5a910 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -2,7 +2,7 @@ import asyncio import io import json -import pathlib +from pathlib import Path import sys import zlib from typing import Any, Optional @@ -1328,7 +1328,7 @@ async def test_preserve_content_disposition_header( self, buf: Any, stream: Any ) -> None: # https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381 - with pathlib.Path(__file__).open("rb") as fobj: + with Path(__file__).open("rb") as fobj: with aiohttp.MultipartWriter("form-data", boundary=":") as writer: part = writer.append( fobj, @@ -1357,7 +1357,7 @@ async def test_set_content_disposition_override( self, buf: Any, stream: Any ) -> None: # https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381 - with pathlib.Path(__file__).open("rb") as fobj: + with Path(__file__).open("rb") as fobj: with aiohttp.MultipartWriter("form-data", boundary=":") as writer: part = writer.append( fobj, @@ -1386,7 +1386,7 @@ async def test_reset_content_disposition_header( self, buf: Any, stream: Any ) -> None: # https://github.com/aio-libs/aiohttp/pull/3475#issuecomment-451072381 - with pathlib.Path(__file__).open("rb") as fobj: + with Path(__file__).open("rb") as fobj: with aiohttp.MultipartWriter("form-data", boundary=":") as writer: part = writer.append( fobj, diff --git a/tests/test_proxy_functional.py b/tests/test_proxy_functional.py index 2927d5e547e..50c386d3667 100644 --- a/tests/test_proxy_functional.py +++ b/tests/test_proxy_functional.py @@ -2,7 +2,7 @@ import asyncio import functools import os -import pathlib +from pathlib import Path import platform from re import match as match_regex from typing import Any @@ -691,7 +691,7 @@ def _make_ssl_transport_dummy( ) -original_is_file: Any = pathlib.Path.is_file +original_is_file: Any = Path.is_file def mock_is_file(self): diff --git a/tests/test_route_def.py b/tests/test_route_def.py index c100a89f6f7..61d8b866cc7 100644 --- a/tests/test_route_def.py +++ b/tests/test_route_def.py @@ -1,5 +1,5 @@ # type: ignore -import pathlib +from pathlib import Path from typing import Any import pytest @@ -122,7 +122,7 @@ async def handler(request): def test_static(router: Any) -> None: - folder = pathlib.Path(__file__).parent + folder = Path(__file__).parent router.add_routes([web.static("/prefix", folder)]) assert len(router.resources()) == 1 # 2 routes: for HEAD and GET diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index e302892c7c9..45ba4892484 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -1,5 +1,5 @@ # type: ignore -import pathlib +from pathlib import Path import re from collections.abc import Container, Iterable, Mapping, MutableMapping, Sized from functools import partial @@ -58,7 +58,7 @@ def fill_routes(router: Any): def go(): route1 = router.add_route("GET", "/plain", make_handler()) route2 = router.add_route("GET", "/variable/{name}", make_handler()) - resource = router.add_static("/static", pathlib.Path(aiohttp.__file__).parent) + resource = router.add_static("/static", Path(aiohttp.__file__).parent) return [route1, route2] + list(resource) return go @@ -364,7 +364,7 @@ def test_route_dynamic(router: Any) -> None: def test_add_static(router: Any) -> None: resource = router.add_static( - "/st", pathlib.Path(aiohttp.__file__).parent, name="static" + "/st", Path(aiohttp.__file__).parent, name="static" ) assert router["static"] is resource url = resource.url_for(filename="/dir/a.txt") @@ -373,7 +373,7 @@ def test_add_static(router: Any) -> None: def test_add_static_append_version(router: Any) -> None: - resource = router.add_static("/st", pathlib.Path(__file__).parent, name="static") + resource = router.add_static("/st", Path(__file__).parent, name="static") url = resource.url_for(filename="/data.unknown_mime_type", append_version=True) expect_url = ( "/st/data.unknown_mime_type?" "v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D" @@ -383,7 +383,7 @@ def test_add_static_append_version(router: Any) -> None: def test_add_static_append_version_set_from_constructor(router: Any) -> None: resource = router.add_static( - "/st", pathlib.Path(__file__).parent, append_version=True, name="static" + "/st", Path(__file__).parent, append_version=True, name="static" ) url = resource.url_for(filename="/data.unknown_mime_type") expect_url = ( @@ -394,7 +394,7 @@ def test_add_static_append_version_set_from_constructor(router: Any) -> None: def test_add_static_append_version_override_constructor(router: Any) -> None: resource = router.add_static( - "/st", pathlib.Path(__file__).parent, append_version=True, name="static" + "/st", Path(__file__).parent, append_version=True, name="static" ) url = resource.url_for(filename="/data.unknown_mime_type", append_version=False) expect_url = "/st/data.unknown_mime_type" @@ -402,7 +402,7 @@ def test_add_static_append_version_override_constructor(router: Any) -> None: def test_add_static_append_version_filename_without_slash(router: Any) -> None: - resource = router.add_static("/st", pathlib.Path(__file__).parent, name="static") + resource = router.add_static("/st", Path(__file__).parent, name="static") url = resource.url_for(filename="data.unknown_mime_type", append_version=True) expect_url = ( "/st/data.unknown_mime_type?" "v=aUsn8CHEhhszc81d28QmlcBW0KQpfS2F4trgQKhOYd8%3D" @@ -411,13 +411,13 @@ def test_add_static_append_version_filename_without_slash(router: Any) -> None: def test_add_static_append_version_non_exists_file(router: Any) -> None: - resource = router.add_static("/st", pathlib.Path(__file__).parent, name="static") + resource = router.add_static("/st", Path(__file__).parent, name="static") url = resource.url_for(filename="/non_exists_file", append_version=True) assert "/st/non_exists_file" == str(url) def test_add_static_append_version_non_exists_file_without_slash(router: Any) -> None: - resource = router.add_static("/st", pathlib.Path(__file__).parent, name="static") + resource = router.add_static("/st", Path(__file__).parent, name="static") url = resource.url_for(filename="non_exists_file", append_version=True) assert "/st/non_exists_file" == str(url) @@ -425,8 +425,8 @@ def test_add_static_append_version_non_exists_file_without_slash(router: Any) -> def test_add_static_append_version_follow_symlink(router: Any, tmp_path: Any) -> None: # Tests the access to a symlink, in static folder with apeend_version symlink_path = tmp_path / "append_version_symlink" - symlink_target_path = pathlib.Path(__file__).parent - pathlib.Path(str(symlink_path)).symlink_to(str(symlink_target_path), True) + symlink_target_path = Path(__file__).parent + Path(str(symlink_path)).symlink_to(str(symlink_target_path), True) # Register global static route: resource = router.add_static( @@ -448,9 +448,9 @@ def test_add_static_append_version_not_follow_symlink( # Tests the access to a symlink, in static folder with apeend_version symlink_path = tmp_path / "append_version_symlink" - symlink_target_path = pathlib.Path(__file__).parent + symlink_target_path = Path(__file__).parent - pathlib.Path(str(symlink_path)).symlink_to(str(symlink_target_path), True) + Path(str(symlink_path)).symlink_to(str(symlink_target_path), True) # Register global static route: resource = router.add_static( @@ -464,7 +464,7 @@ def test_add_static_append_version_not_follow_symlink( def test_add_static_quoting(router: Any) -> None: resource = router.add_static( - "/пре %2Fфикс", pathlib.Path(aiohttp.__file__).parent, name="static" + "/пре %2Fфикс", Path(aiohttp.__file__).parent, name="static" ) assert router["static"] is resource url = resource.url_for(filename="/1 2/файл%2F.txt") @@ -491,7 +491,7 @@ def test_dynamic_not_match(router: Any) -> None: async def test_static_not_match(router: Any) -> None: - router.add_static("/pre", pathlib.Path(aiohttp.__file__).parent, name="name") + router.add_static("/pre", Path(aiohttp.__file__).parent, name="name") resource = router["name"] ret = await resource.resolve(make_mocked_request("GET", "/another/path")) assert (None, set()) == ret @@ -527,17 +527,17 @@ def test_contains(router: Any) -> None: def test_static_repr(router: Any) -> None: - router.add_static("/get", pathlib.Path(aiohttp.__file__).parent, name="name") + router.add_static("/get", Path(aiohttp.__file__).parent, name="name") assert Matches(r" None: - route = router.add_static("/prefix", pathlib.Path(aiohttp.__file__).parent) + route = router.add_static("/prefix", Path(aiohttp.__file__).parent) assert "/prefix" == route._prefix def test_static_remove_trailing_slash(router: Any) -> None: - route = router.add_static("/prefix/", pathlib.Path(aiohttp.__file__).parent) + route = router.add_static("/prefix/", Path(aiohttp.__file__).parent) assert "/prefix" == route._prefix @@ -802,7 +802,7 @@ def test_named_resources(router: Any) -> None: route1 = router.add_route("GET", "/plain", make_handler(), name="route1") route2 = router.add_route("GET", "/variable/{name}", make_handler(), name="route2") route3 = router.add_static( - "/static", pathlib.Path(aiohttp.__file__).parent, name="route3" + "/static", Path(aiohttp.__file__).parent, name="route3" ) names = {route1.name, route2.name, route3.name} @@ -911,7 +911,7 @@ async def test_match_info_get_info_dynamic2(router: Any) -> None: def test_static_resource_get_info(router: Any) -> None: - directory = pathlib.Path(aiohttp.__file__).parent.resolve() + directory = Path(aiohttp.__file__).parent.resolve() resource = router.add_static("/st", directory) info = resource.get_info() assert len(info) == 3 @@ -956,9 +956,9 @@ def test_resources_abc(router: Any) -> None: def test_static_route_user_home(router: Any) -> None: - here = pathlib.Path(aiohttp.__file__).parent + here = Path(aiohttp.__file__).parent try: - static_dir = pathlib.Path("~") / here.relative_to(pathlib.Path.home()) + static_dir = Path("~") / here.relative_to(Path.home()) except ValueError: pytest.skip("aiohttp folder is not placed in user's HOME") route = router.add_static("/st", str(static_dir)) @@ -966,19 +966,19 @@ def test_static_route_user_home(router: Any) -> None: def test_static_route_points_to_file(router: Any) -> None: - here = pathlib.Path(aiohttp.__file__).parent / "__init__.py" + here = Path(aiohttp.__file__).parent / "__init__.py" with pytest.raises(ValueError): router.add_static("/st", here) async def test_404_for_static_resource(router: Any) -> None: - resource = router.add_static("/st", pathlib.Path(aiohttp.__file__).parent) + resource = router.add_static("/st", Path(aiohttp.__file__).parent) ret = await resource.resolve(make_mocked_request("GET", "/unknown/path")) assert (None, set()) == ret async def test_405_for_resource_adapter(router: Any) -> None: - resource = router.add_static("/st", pathlib.Path(aiohttp.__file__).parent) + resource = router.add_static("/st", Path(aiohttp.__file__).parent) ret = await resource.resolve(make_mocked_request("POST", "/st/abc.py")) assert (None, {"HEAD", "GET"}) == ret @@ -993,14 +993,14 @@ async def test_check_allowed_method_for_found_resource(router: Any) -> None: def test_url_for_in_static_resource(router: Any) -> None: - resource = router.add_static("/static", pathlib.Path(aiohttp.__file__).parent) + resource = router.add_static("/static", Path(aiohttp.__file__).parent) assert URL("/static/file.txt") == resource.url_for(filename="file.txt") def test_url_for_in_static_resource_pathlib(router: Any) -> None: - resource = router.add_static("/static", pathlib.Path(aiohttp.__file__).parent) + resource = router.add_static("/static", Path(aiohttp.__file__).parent) assert URL("/static/file.txt") == resource.url_for( - filename=pathlib.Path("file.txt") + filename=Path("file.txt") ) @@ -1177,7 +1177,7 @@ def test_frozen_app_on_subapp(app: Any) -> None: def test_set_options_route(router: Any) -> None: - resource = router.add_static("/static", pathlib.Path(aiohttp.__file__).parent) + resource = router.add_static("/static", Path(aiohttp.__file__).parent) options = None for route in resource: if route.method == "OPTIONS": @@ -1239,7 +1239,7 @@ def test_dynamic_resource_canonical() -> None: def test_static_resource_canonical() -> None: prefix = "/prefix" - directory = str(pathlib.Path(aiohttp.__file__).parent) + directory = str(Path(aiohttp.__file__).parent) canonical = prefix res = StaticResource(prefix=prefix, directory=directory) assert res.canonical == canonical diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index c6bdadf3fd7..9bd21c55003 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -2,7 +2,7 @@ import asyncio import io import json -import pathlib +from pathlib import Path import socket import zlib from typing import Any @@ -27,7 +27,7 @@ @pytest.fixture def here(): - return pathlib.Path(__file__).parent + return Path(__file__).parent @pytest.fixture @@ -305,7 +305,7 @@ async def handler(request): async def test_post_single_file(aiohttp_client: Any) -> None: - here = pathlib.Path(__file__).parent + here = Path(__file__).parent def check_file(fs): fullname = here / fs.filename @@ -368,7 +368,7 @@ async def handler(request): async def test_post_files(aiohttp_client: Any) -> None: - here = pathlib.Path(__file__).parent + here = Path(__file__).parent def check_file(fs): fullname = here / fs.filename @@ -625,7 +625,7 @@ async def handler(request): async def test_upload_file(aiohttp_client: Any) -> None: - here = pathlib.Path(__file__).parent + here = Path(__file__).parent fname = here / "aiohttp.png" with fname.open("rb") as f: data = f.read() @@ -645,7 +645,7 @@ async def handler(request): async def test_upload_file_object(aiohttp_client: Any) -> None: - here = pathlib.Path(__file__).parent + here = Path(__file__).parent fname = here / "aiohttp.png" with fname.open("rb") as f: data = f.read() @@ -1117,7 +1117,7 @@ async def handler(request): app = web.Application() subapp = web.Application() subapp.router.add_get("/to", handler) - here = pathlib.Path(__file__).parent + here = Path(__file__).parent subapp.router.add_static("/static", here, name="name") app.add_subapp("/path", subapp) diff --git a/tests/test_web_sendfile_functional.py b/tests/test_web_sendfile_functional.py index dca622b2ccc..e68bd9d9329 100644 --- a/tests/test_web_sendfile_functional.py +++ b/tests/test_web_sendfile_functional.py @@ -1,6 +1,6 @@ # type: ignore import asyncio -import pathlib +from pathlib import Path import socket import zlib from typing import Any, Iterable @@ -50,7 +50,7 @@ def maker(*args, **kwargs): @pytest.fixture def app_with_static_route(sender: Any) -> web.Application: filename = "data.unknown_mime_type" - filepath = pathlib.Path(__file__).parent / filename + filepath = Path(__file__).parent / filename async def handler(request): return sender(filepath) @@ -75,7 +75,7 @@ async def test_static_file_ok( async def test_zero_bytes_file_ok(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "data.zero_bytes" + filepath = Path(__file__).parent / "data.zero_bytes" async def handler(request): return sender(filepath) @@ -100,7 +100,7 @@ async def handler(request): async def test_zero_bytes_file_mocked_native_sendfile( aiohttp_client: Any, loop_with_mocked_native_sendfile: Any ) -> None: - filepath = pathlib.Path(__file__).parent / "data.zero_bytes" + filepath = Path(__file__).parent / "data.zero_bytes" async def handler(request): asyncio.set_event_loop(loop_with_mocked_native_sendfile) @@ -169,7 +169,7 @@ async def test_static_file_upper_directory(aiohttp_client: Any) -> None: async def test_static_file_with_content_type(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "aiohttp.jpg" + filepath = Path(__file__).parent / "aiohttp.jpg" async def handler(request): return sender(filepath, chunk_size=16) @@ -192,7 +192,7 @@ async def handler(request): async def test_static_file_custom_content_type( aiohttp_client: Any, sender: Any ) -> None: - filepath = pathlib.Path(__file__).parent / "hello.txt.gz" + filepath = Path(__file__).parent / "hello.txt.gz" async def handler(request): resp = sender(filepath, chunk_size=16) @@ -217,7 +217,7 @@ async def handler(request): async def test_static_file_custom_content_type_compress( aiohttp_client: Any, sender: Any ): - filepath = pathlib.Path(__file__).parent / "hello.txt" + filepath = Path(__file__).parent / "hello.txt" async def handler(request): resp = sender(filepath, chunk_size=16) @@ -240,7 +240,7 @@ async def handler(request): async def test_static_file_with_content_encoding( aiohttp_client: Any, sender: Any ) -> None: - filepath = pathlib.Path(__file__).parent / "hello.txt.gz" + filepath = Path(__file__).parent / "hello.txt.gz" async def handler(request): return sender(filepath) @@ -431,7 +431,7 @@ async def test_static_file_ssl( aiohttp_client: Any, client_ssl_ctx: Any, ) -> None: - dirname = pathlib.Path(__file__).parent + dirname = Path(__file__).parent filename = "data.unknown_mime_type" app = web.Application() app.router.add_static("/static", dirname) @@ -449,7 +449,7 @@ async def test_static_file_ssl( async def test_static_file_directory_traversal_attack(aiohttp_client: Any) -> None: - dirname = pathlib.Path(__file__).parent + dirname = Path(__file__).parent relpath = "../README.rst" full_path = dirname / relpath assert full_path.is_file() @@ -471,7 +471,7 @@ async def test_static_file_directory_traversal_attack(aiohttp_client: Any) -> No def test_static_route_path_existence_check() -> None: - directory = pathlib.Path(__file__).parent + directory = Path(__file__).parent web.StaticResource("/", directory) nodirectory = directory / "nonexistent-uPNiOEAg5d" @@ -513,7 +513,7 @@ async def test_static_file_huge(aiohttp_client: Any, tmp_path: Any) -> None: async def test_static_file_range(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "sample.txt" + filepath = Path(__file__).parent / "sample.txt" filesize = filepath.stat().st_size @@ -567,7 +567,7 @@ async def handler(request): async def test_static_file_range_end_bigger_than_size(aiohttp_client: Any, sender: Any): - filepath = pathlib.Path(__file__).parent / "aiohttp.png" + filepath = Path(__file__).parent / "aiohttp.png" async def handler(request): return sender(filepath, chunk_size=16) @@ -598,7 +598,7 @@ async def handler(request): async def test_static_file_range_beyond_eof(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "aiohttp.png" + filepath = Path(__file__).parent / "aiohttp.png" async def handler(request): return sender(filepath, chunk_size=16) @@ -616,7 +616,7 @@ async def handler(request): async def test_static_file_range_tail(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "aiohttp.png" + filepath = Path(__file__).parent / "aiohttp.png" async def handler(request): return sender(filepath, chunk_size=16) @@ -647,7 +647,7 @@ async def handler(request): async def test_static_file_invalid_range(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "aiohttp.png" + filepath = Path(__file__).parent / "aiohttp.png" async def handler(request): return sender(filepath, chunk_size=16) @@ -820,7 +820,7 @@ async def test_static_file_if_range_invalid_date( async def test_static_file_compression(aiohttp_client: Any, sender: Any) -> None: - filepath = pathlib.Path(__file__).parent / "data.unknown_mime_type" + filepath = Path(__file__).parent / "data.unknown_mime_type" async def handler(request): ret = sender(filepath) diff --git a/tests/test_web_urldispatcher.py b/tests/test_web_urldispatcher.py index ce2ec3cc77f..5cf1dc16924 100644 --- a/tests/test_web_urldispatcher.py +++ b/tests/test_web_urldispatcher.py @@ -1,6 +1,6 @@ # type: ignore import asyncio -import pathlib +from pathlib import Path from typing import Any from unittest import mock from unittest.mock import MagicMock @@ -91,7 +91,7 @@ async def test_follow_symlink(tmp_path: Any, aiohttp_client: Any) -> None: fw.write(data) my_symlink_path = tmp_path / "my_symlink" - pathlib.Path(str(my_symlink_path)).symlink_to(str(my_dir_path), True) + Path(str(my_symlink_path)).symlink_to(str(my_dir_path), True) app = web.Application() @@ -127,7 +127,7 @@ async def test_access_to_the_file_with_spaces( app = web.Application() - url = "/" + str(pathlib.Path(dir_name, filename)) + url = "/" + str(Path(dir_name, filename)) app.router.add_static("/", str(tmp_path)) client = await aiohttp_client(app) @@ -221,7 +221,7 @@ async def test_unauthorized_folder_access(tmp_path: Any, aiohttp_client: Any) -> async def test_access_symlink_loop(tmp_path: Any, aiohttp_client: Any) -> None: # Tests the access to a looped symlink, which could not be resolved. my_dir_path = tmp_path / "my_symlink" - pathlib.Path(str(my_dir_path)).symlink_to(str(my_dir_path), True) + Path(str(my_dir_path)).symlink_to(str(my_dir_path), True) app = web.Application() @@ -454,7 +454,7 @@ async def test_static_absolute_url(aiohttp_client: Any, tmp_path: Any) -> None: app = web.Application() file_path = tmp_path / "file.txt" file_path.write_text("sample text", "ascii") - here = pathlib.Path(__file__).parent + here = Path(__file__).parent app.router.add_static("/static", here) client = await aiohttp_client(app) resp = await client.get("/static/" + str(file_path.resolve())) diff --git a/tools/check_sum.py b/tools/check_sum.py index 50dec4d2be5..cd7248b54b0 100755 --- a/tools/check_sum.py +++ b/tools/check_sum.py @@ -2,13 +2,13 @@ import argparse import hashlib -import pathlib +from pathlib import Path import sys PARSER = argparse.ArgumentParser( description="Helper for check file hashes in Makefile instead of bare timestamps" ) -PARSER.add_argument("dst", metavar="DST", type=pathlib.Path) +PARSER.add_argument("dst", metavar="DST", type=Path) PARSER.add_argument("-d", "--debug", action="store_true", default=False) diff --git a/tools/gen.py b/tools/gen.py index d00780aa676..5c6ef6d3ec4 100755 --- a/tools/gen.py +++ b/tools/gen.py @@ -1,12 +1,12 @@ #!/usr/bin/env python import io -import pathlib +from pathlib import Path from collections import defaultdict import multidict -ROOT = pathlib.Path.cwd() +ROOT = Path.cwd() while ROOT.parent != ROOT and not (ROOT / ".git").exists(): ROOT = ROOT.parent From 70a108db2d8e22e0361cd7784a013400b5b87ca9 Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 15:16:17 +0200 Subject: [PATCH 8/9] fmt check --- examples/fake_server.py | 2 +- examples/static_files.py | 2 +- setup.py | 2 +- tests/test_client_functional.py | 2 +- tests/test_client_request.py | 2 +- tests/test_cookiejar.py | 2 +- tests/test_multipart.py | 2 +- tests/test_proxy_functional.py | 2 +- tests/test_urldispatch.py | 14 ++++---------- tests/test_web_functional.py | 2 +- tests/test_web_sendfile_functional.py | 2 +- tools/check_sum.py | 2 +- tools/gen.py | 2 +- 13 files changed, 16 insertions(+), 22 deletions(-) diff --git a/examples/fake_server.py b/examples/fake_server.py index e1a11eb361d..dc4a54c29e7 100755 --- a/examples/fake_server.py +++ b/examples/fake_server.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 import asyncio -from pathlib import Path import socket import ssl +from pathlib import Path from typing import Any, Dict, List, Union from aiohttp import ClientSession, TCPConnector, resolver, test_utils, web diff --git a/examples/static_files.py b/examples/static_files.py index 7cd785f98cf..93221e09ba2 100755 --- a/examples/static_files.py +++ b/examples/static_files.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -from pathlib import Path +from pathlib import Path from aiohttp import web diff --git a/setup.py b/setup.py index 35a5a63991b..cd8afcf55e3 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ import os -from pathlib import Path import sys +from pathlib import Path from setuptools import Extension, setup diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index 9eb1673e18d..a42ee997625 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -6,9 +6,9 @@ import http.cookies import io import json -from pathlib import Path import socket import ssl +from pathlib import Path from typing import Any from unittest import mock diff --git a/tests/test_client_request.py b/tests/test_client_request.py index 509321ac846..74d7c0cc773 100644 --- a/tests/test_client_request.py +++ b/tests/test_client_request.py @@ -2,9 +2,9 @@ import asyncio import hashlib import io -from pathlib import Path import zlib from http.cookies import BaseCookie, Morsel, SimpleCookie +from pathlib import Path from typing import Any from unittest import mock diff --git a/tests/test_cookiejar.py b/tests/test_cookiejar.py index 7bb4a9e8ad1..d5d654040e4 100644 --- a/tests/test_cookiejar.py +++ b/tests/test_cookiejar.py @@ -2,9 +2,9 @@ import asyncio import datetime import itertools -from pathlib import Path import unittest from http.cookies import BaseCookie, Morsel, SimpleCookie +from pathlib import Path from typing import Any from unittest import mock diff --git a/tests/test_multipart.py b/tests/test_multipart.py index 54ba5d5a910..63b0fc76a93 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -2,9 +2,9 @@ import asyncio import io import json -from pathlib import Path import sys import zlib +from pathlib import Path from typing import Any, Optional from unittest import mock diff --git a/tests/test_proxy_functional.py b/tests/test_proxy_functional.py index 50c386d3667..73239d36e9b 100644 --- a/tests/test_proxy_functional.py +++ b/tests/test_proxy_functional.py @@ -2,8 +2,8 @@ import asyncio import functools import os -from pathlib import Path import platform +from pathlib import Path from re import match as match_regex from typing import Any from unittest import mock diff --git a/tests/test_urldispatch.py b/tests/test_urldispatch.py index 45ba4892484..bd1ea50ce0c 100644 --- a/tests/test_urldispatch.py +++ b/tests/test_urldispatch.py @@ -1,8 +1,8 @@ # type: ignore -from pathlib import Path import re from collections.abc import Container, Iterable, Mapping, MutableMapping, Sized from functools import partial +from pathlib import Path from typing import Any from urllib.parse import unquote @@ -363,9 +363,7 @@ def test_route_dynamic(router: Any) -> None: def test_add_static(router: Any) -> None: - resource = router.add_static( - "/st", Path(aiohttp.__file__).parent, name="static" - ) + resource = router.add_static("/st", Path(aiohttp.__file__).parent, name="static") assert router["static"] is resource url = resource.url_for(filename="/dir/a.txt") assert "/st/dir/a.txt" == str(url) @@ -801,9 +799,7 @@ def test_named_resources_abc(router: Any) -> None: def test_named_resources(router: Any) -> None: route1 = router.add_route("GET", "/plain", make_handler(), name="route1") route2 = router.add_route("GET", "/variable/{name}", make_handler(), name="route2") - route3 = router.add_static( - "/static", Path(aiohttp.__file__).parent, name="route3" - ) + route3 = router.add_static("/static", Path(aiohttp.__file__).parent, name="route3") names = {route1.name, route2.name, route3.name} assert 3 == len(router.named_resources()) @@ -999,9 +995,7 @@ def test_url_for_in_static_resource(router: Any) -> None: def test_url_for_in_static_resource_pathlib(router: Any) -> None: resource = router.add_static("/static", Path(aiohttp.__file__).parent) - assert URL("/static/file.txt") == resource.url_for( - filename=Path("file.txt") - ) + assert URL("/static/file.txt") == resource.url_for(filename=Path("file.txt")) def test_url_for_in_resource_route(router: Any) -> None: diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 9bd21c55003..0f4f7f3f2d9 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -2,9 +2,9 @@ import asyncio import io import json -from pathlib import Path import socket import zlib +from pathlib import Path from typing import Any from unittest import mock diff --git a/tests/test_web_sendfile_functional.py b/tests/test_web_sendfile_functional.py index e68bd9d9329..ea8950f29c6 100644 --- a/tests/test_web_sendfile_functional.py +++ b/tests/test_web_sendfile_functional.py @@ -1,8 +1,8 @@ # type: ignore import asyncio -from pathlib import Path import socket import zlib +from pathlib import Path from typing import Any, Iterable import pytest diff --git a/tools/check_sum.py b/tools/check_sum.py index cd7248b54b0..9acba1b21a5 100755 --- a/tools/check_sum.py +++ b/tools/check_sum.py @@ -2,8 +2,8 @@ import argparse import hashlib -from pathlib import Path import sys +from pathlib import Path PARSER = argparse.ArgumentParser( description="Helper for check file hashes in Makefile instead of bare timestamps" diff --git a/tools/gen.py b/tools/gen.py index 5c6ef6d3ec4..0e62a9dfb40 100755 --- a/tools/gen.py +++ b/tools/gen.py @@ -1,8 +1,8 @@ #!/usr/bin/env python import io -from pathlib import Path from collections import defaultdict +from pathlib import Path import multidict From d56651af884fb5632d7b551698980290e077b04e Mon Sep 17 00:00:00 2001 From: SamirPS Date: Sat, 23 Jul 2022 18:17:40 +0200 Subject: [PATCH 9/9] Revert "modify test and add contribution" This reverts commit e4fcb74b3e319bba897c7885b441699c68c3365b. --- CONTRIBUTORS.txt | 1 - tests/test_run_app.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index bda74215d50..bd7677f5f29 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -267,7 +267,6 @@ Robert Lu Robert Nikolich Roman Markeloff Roman Podoliaka -Samir Akarioh Samuel Colvin Sean Hunt Sebastian Acuna diff --git a/tests/test_run_app.py b/tests/test_run_app.py index cd91d4b4282..835f2c6714f 100644 --- a/tests/test_run_app.py +++ b/tests/test_run_app.py @@ -518,7 +518,7 @@ def test_run_app_custom_backlog_unix(patched_loop: Any) -> None: def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None: app = web.Application() - sock_path = tmp_path / "socket.sock" + sock_path = str(tmp_path / "socket.sock") printer = mock.Mock(wraps=stopper(patched_loop)) web.run_app(app, path=sock_path, print=printer, loop=patched_loop) @@ -532,7 +532,7 @@ def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None: def test_run_app_https_unix_socket(patched_loop: Any, tmp_path: Any) -> None: app = web.Application() - sock_path = tmp_path / "socket.sock" + sock_path = str(tmp_path / "socket.sock") ssl_context = ssl.create_default_context() printer = mock.Mock(wraps=stopper(patched_loop)) web.run_app(