From aa79a778ae83bb05a93263213d6403a6fc47ad5d Mon Sep 17 00:00:00 2001 From: Joel Andrews Date: Sat, 29 May 2021 18:27:26 -0700 Subject: [PATCH 1/2] Explicitly indicate start of keyword-only args See PEP 3102 (https://www.python.org/dev/peps/pep-3102/). --- requests_mock/adapter.pyi | 1 + requests_mock/mocker.pyi | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/requests_mock/adapter.pyi b/requests_mock/adapter.pyi index da48e2f..1ab3544 100644 --- a/requests_mock/adapter.pyi +++ b/requests_mock/adapter.pyi @@ -33,6 +33,7 @@ class Adapter(BaseAdapter, _RequestHistoryTracker): 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 = ..., diff --git a/requests_mock/mocker.pyi b/requests_mock/mocker.pyi index 12abb56..27880c0 100644 --- a/requests_mock/mocker.pyi +++ b/requests_mock/mocker.pyi @@ -34,6 +34,7 @@ class MockerCore: 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 = ..., @@ -46,6 +47,7 @@ class MockerCore: 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 = ..., @@ -57,6 +59,7 @@ class MockerCore: 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 = ..., @@ -68,6 +71,7 @@ class MockerCore: 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 = ..., @@ -79,6 +83,7 @@ class MockerCore: 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 = ..., @@ -90,6 +95,7 @@ class MockerCore: 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 = ..., @@ -101,6 +107,7 @@ class MockerCore: 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 = ..., @@ -112,6 +119,7 @@ class MockerCore: 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 = ..., @@ -123,6 +131,7 @@ class MockerCore: 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 = ..., From 4ffdc248697dd927d515dbfaecbe915bccdf35d1 Mon Sep 17 00:00:00 2001 From: Joel Andrews Date: Sat, 29 May 2021 18:53:15 -0700 Subject: [PATCH 2/2] Improve response-related param type hints Makes improvements to the type hints of response parameters in both `MockerCore` and `Adapter`: - Added explicit parameters for `reason`, `cookies`, `json`, `content`, `body`, `raw` and `exc` as per the documentation at https://requests-mock.readthedocs.io/en/latest/response.html#registering-responses - Updated the `text` parameter to support static and dynamic values as per https://requests-mock.readthedocs.io/en/latest/response.html#dynamic-response - Ensured that the "new" `json`, `content` and `body` parameters support both static and dynamic values, just like the `text` parameter --- requests_mock/adapter.pyi | 19 +++++-- requests_mock/mocker.pyi | 109 +++++++++++++++++++++++++++++++------- 2 files changed, 105 insertions(+), 23 deletions(-) diff --git a/requests_mock/adapter.pyi b/requests_mock/adapter.pyi index 1ab3544..8570cd7 100644 --- a/requests_mock/adapter.pyi +++ b/requests_mock/adapter.pyi @@ -1,8 +1,14 @@ # Stubs for requests_mock.adapter +from http.cookiejar import CookieJar +from io import IOBase +from typing import Any, Callable, Dict, List, NewType, Optional, Pattern, Union + from requests.adapters import BaseAdapter +from requests.packages.urllib3.response import HTTPResponse + +from requests_mock.response import _Context from requests_mock import _RequestObjectProxy -from typing import Any, Callable, Dict, List, NewType, Optional, Pattern, Union AnyMatcher = NewType("AnyMatcher", object) @@ -37,8 +43,15 @@ class Adapter(BaseAdapter, _RequestHistoryTracker): request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any ) -> Any: ... diff --git a/requests_mock/mocker.pyi b/requests_mock/mocker.pyi index 27880c0..31f6464 100644 --- a/requests_mock/mocker.pyi +++ b/requests_mock/mocker.pyi @@ -1,9 +1,15 @@ # Stubs for requests_mock.mocker -from requests_mock.adapter import AnyMatcher +from http.cookiejar import CookieJar +from io import IOBase +from typing import Any, Callable, Dict, List, Optional, Pattern, Type, TypeVar, Union + from requests import Response +from requests.packages.urllib3.response import HTTPResponse + +from requests_mock.adapter import AnyMatcher from requests_mock.request import _RequestObjectProxy -from typing import Any, Callable, Dict, List, Optional, Pattern, Type, TypeVar, Union +from requests_mock.response import _Context DELETE: str GET: str @@ -38,8 +44,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def request( @@ -51,8 +64,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def get( @@ -63,8 +83,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def head( @@ -75,8 +102,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def options( @@ -87,8 +121,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def post( @@ -99,8 +140,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def put( @@ -111,8 +159,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def patch( @@ -123,8 +178,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ... def delete( @@ -135,8 +197,15 @@ class MockerCore: request_headers: Dict[str, str] = ..., complete_qs: bool = ..., status_code: int = ..., - text: str = ..., - headers: Optional[Dict[str, str]] = ..., + reason: str = ..., + headers: Dict[str, str] = ..., + cookies: Union[CookieJar, Dict[str, str]] = ..., + json: Union[Any, Callable[[_RequestObjectProxy, _Context], Any]] = ..., + text: Union[str, Callable[[_RequestObjectProxy, _Context], str]] = ..., + content: Union[bytes, Callable[[_RequestObjectProxy, _Context], bytes]] = ..., + body: Union[IOBase, Callable[[_RequestObjectProxy, _Context], IOBase]] = ..., + raw: HTTPResponse = ..., + exc: Exception = ..., additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ..., **kwargs: Any) -> Response: ...