From b74886dbcc266762095bf9cdab303d3b175ac981 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Fri, 29 Dec 2023 15:43:49 +0100 Subject: [PATCH] Improvements to type stubs --- requests_mock/__init__.pyi | 12 +- requests_mock/adapter.pyi | 55 +++--- requests_mock/mocker.pyi | 387 ++++++++++++++++++------------------- requests_mock/request.pyi | 5 +- requests_mock/response.pyi | 12 +- 5 files changed, 232 insertions(+), 239 deletions(-) diff --git a/requests_mock/__init__.pyi b/requests_mock/__init__.pyi index 9d8dd00..7e3725b 100644 --- a/requests_mock/__init__.pyi +++ b/requests_mock/__init__.pyi @@ -1,18 +1,18 @@ # Stubs for requests_mock from requests_mock.adapter import ( - ANY as ANY, - Adapter as Adapter, - Callback as Callback, + ANY as ANY, + Adapter as Adapter, + Callback as Callback, AdditionalMatcher as AdditionalMatcher, ) from requests_mock.exceptions import ( - MockException as MockException, + MockException as MockException, NoMockAddress as NoMockAddress, ) from requests_mock.mocker import ( - DELETE as DELETE, - GET as GET, + DELETE as DELETE, + GET as GET, HEAD as HEAD, Mocker as Mocker, MockerCore as MockerCore, diff --git a/requests_mock/adapter.pyi b/requests_mock/adapter.pyi index dbeba49..6cd5bc2 100644 --- a/requests_mock/adapter.pyi +++ b/requests_mock/adapter.pyi @@ -2,7 +2,7 @@ from http.cookiejar import CookieJar from io import IOBase -from typing import Any, Callable, Dict, List, NewType, Optional, Pattern, Type, TypeVar, Union +from typing import Any, Callable, NewType, Pattern, TypeVar from requests import Response from requests.adapters import BaseAdapter @@ -15,16 +15,16 @@ AnyMatcher = NewType("AnyMatcher", object) ANY: AnyMatcher = ... -T = TypeVar('T') +T = TypeVar("T") Callback = Callable[[Request, Context], T] -Matcher = Callable[[Request], Optional[Response]] +Matcher = Callable[[Request], Response | None] AdditionalMatcher = Callable[[Request], bool] class _RequestHistoryTracker: - request_history: List[Request] = ... + request_history: list[Request] = ... def __init__(self) -> None: ... @property - def last_request(self) -> Optional[Request]: ... + def last_request(self) -> Request | None: ... @property def called(self) -> bool: ... @property @@ -36,38 +36,39 @@ class _RunRealHTTP(Exception): ... class _Matcher(_RequestHistoryTracker): def __init__( - self, - method: Any, - url: Any, - responses: Any, - complete_qs: Any, - request_headers: Any, - additional_matcher: AdditionalMatcher, - real_http: Any, - case_sensitive: Any + self, + method: Any, + url: Any, + responses: Any, + complete_qs: Any, + request_headers: Any, + additional_matcher: AdditionalMatcher, + real_http: Any, + case_sensitive: Any, ) -> None: ... - def __call__(self, request: Request) -> Optional[Response]: ... - + def __call__(self, request: Request) -> Response | None: ... + class Adapter(BaseAdapter, _RequestHistoryTracker): def __init__(self, case_sensitive: bool = ...) -> None: ... def register_uri( self, - method: Union[str, AnyMatcher], - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., + method: str | AnyMatcher, + url: str | Pattern[str], + AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., *, - request_headers: Dict[str, str] = ..., + request_headers: dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., + exc: Exception | type[Exception] = ..., additional_matcher: AdditionalMatcher = ..., **kwargs: Any ) -> _Matcher: ... diff --git a/requests_mock/mocker.pyi b/requests_mock/mocker.pyi index 891e7d6..a6be7ff 100644 --- a/requests_mock/mocker.pyi +++ b/requests_mock/mocker.pyi @@ -3,7 +3,7 @@ from json import JSONEncoder from http.cookiejar import CookieJar from io import IOBase -from typing import Any, Callable, Dict, List, Optional, Pattern, Type, TypeVar, Union +from typing import Any, Callable, Pattern, TypeVar, overload from requests import Response, Session from urllib3.response import HTTPResponse @@ -24,11 +24,11 @@ class MockerCore: def __init__(self, **kwargs: Any) -> None: ... def start(self) -> None: ... def stop(self) -> None: ... - def add_matcher(self, matcher: Callable[[Request], Optional[Response]]) -> None: ... + def add_matcher(self, matcher: Callable[[Request], Response | None]) -> None: ... @property - def request_history(self) -> List[Request]: ... + def request_history(self) -> list[Request]: ... @property - def last_request(self) -> Optional[Request]: ... + def last_request(self) -> Request | None: ... @property def called(self) -> bool: ... @property @@ -37,227 +37,222 @@ class MockerCore: def call_count(self) -> int: ... def reset(self) -> None: ... def reset_mock(self) -> None: ... - def register_uri( - self, - method: Union[str, AnyMatcher], - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + method: str | AnyMatcher, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def request( - self, - method: Union[str, AnyMatcher], - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + method: str | AnyMatcher, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def get( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def head( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def options( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def post( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def put( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def patch( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... - def delete( - self, - url: Union[str, Pattern[str], AnyMatcher], - response_list: Optional[List[Dict[str, Any]]] = ..., - *, - request_headers: Dict[str, str] = ..., - complete_qs: bool = ..., - status_code: int = ..., - reason: str = ..., - headers: Dict[str, str] = ..., - cookies: Union[CookieJar, Dict[str, str]] = ..., - json: Union[Any, Callback[Any]] = ..., - text: Union[str, Callback[str]] = ..., - content: Union[bytes, Callback[bytes]] = ..., - body: Union[IOBase, Callback[IOBase]] = ..., - raw: HTTPResponse = ..., - exc: Union[Exception, Type[Exception]] = ..., - additional_matcher: AdditionalMatcher = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., - **kwargs: Any, + self, + url: str | Pattern[str] | AnyMatcher, + response_list: list[dict[str, Any]] | None = ..., + *, + request_headers: dict[str, str] = ..., + complete_qs: bool = ..., + status_code: int = ..., + reason: str = ..., + headers: dict[str, str] = ..., + cookies: CookieJar | dict[str, str] = ..., + json: Any | Callback[Any] = ..., + text: str | Callback[str] = ..., + content: bytes | Callback[bytes] = ..., + body: IOBase | Callback[IOBase] = ..., + raw: HTTPResponse = ..., + exc: Exception | type[Exception] = ..., + additional_matcher: AdditionalMatcher = ..., + json_encoder: type[JSONEncoder] | None = ..., + **kwargs: Any, ) -> _Matcher: ... -_T = TypeVar('_T') +_T = TypeVar("_T") +_CallableT = TypeVar("_CallableT", bound=Callable) class Mocker(MockerCore): TEST_PREFIX: str = ... real_http: bool = ... def __init__( - self, - kw: str = ..., - case_sensitive: bool = ..., - adapter: Any = ..., - session: Optional[Session] = ..., - real_http: bool = ..., - json_encoder: Optional[Type[JSONEncoder]] = ..., + self, + kw: str = ..., + case_sensitive: bool = ..., + adapter: Any = ..., + session: Session | None = ..., + real_http: bool = ..., + json_encoder: type[JSONEncoder] | None = ..., ) -> None: ... def __enter__(self) -> Any: ... def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ... - def __call__(self, obj: Any) -> Any: ... + @overload + def __call__(self, obj: type[_T]) -> type[_T]: ... + @overload + def __call__(self, obj: _CallableT) -> _CallableT: ... def copy(self) -> Mocker: ... def decorate_callable(self, func: Callable[..., _T]) -> Callable[..., _T]: ... - def decorate_class(self, klass: Type[_T]) -> Type[_T]: ... + def decorate_class(self, klass: type[_T]) -> type[_T]: ... mock = Mocker diff --git a/requests_mock/request.pyi b/requests_mock/request.pyi index 5e2fb30..03afcb6 100644 --- a/requests_mock/request.pyi +++ b/requests_mock/request.pyi @@ -1,6 +1,6 @@ # Stubs for requests_mock.request -from typing import Any, Dict, List +from typing import Any class _RequestObjectProxy: def __init__(self, request: Any, **kwargs: Any) -> None: ... @@ -18,7 +18,7 @@ class _RequestObjectProxy: @property def query(self) -> str: ... @property - def qs(self) -> Dict[str, List[str]]: ... + def qs(self) -> dict[str, list[str]]: ... @property def timeout(self) -> int: ... @property @@ -36,6 +36,5 @@ class _RequestObjectProxy: def json(self, **kwargs: Any) -> Any: ... @property def matcher(self) -> Any: ... - Request = _RequestObjectProxy diff --git a/requests_mock/response.pyi b/requests_mock/response.pyi index e7c8977..fcc5d20 100644 --- a/requests_mock/response.pyi +++ b/requests_mock/response.pyi @@ -1,6 +1,6 @@ # Stubs for requests_mock.response -from typing import Any, Dict +from typing import Any import six @@ -20,16 +20,14 @@ class _IOReader(six.BytesIO): def create_response(request: Any, **kwargs: Any) -> Response: ... class _Context: - headers: Dict[str,str] = ... + headers: dict[str, str] = ... status_code: int = ... reason: str = ... cookies: Any = ... - def __init__(self, - headers: Dict[str, str], - status_code: int, - reason: str, - cookies: Any) -> None: ... + def __init__( + self, headers: dict[str, str], status_code: int, reason: str, cookies: Any + ) -> None: ... class _MatcherResponse: def __init__(self, **kwargs: Any) -> None: ...