Skip to content

Commit

Permalink
Improve response-related param type hints
Browse files Browse the repository at this point in the history
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
  • Loading branch information
OldSneerJaw committed May 30, 2021
1 parent aa79a77 commit 4ffdc24
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 23 deletions.
19 changes: 16 additions & 3 deletions requests_mock/adapter.pyi
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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: ...
Expand Down
109 changes: 89 additions & 20 deletions requests_mock/mocker.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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: ...

Expand Down

0 comments on commit 4ffdc24

Please sign in to comment.