Skip to content

Commit bb47a36

Browse files
committed
Additional improvements to response matching type stubs
For #171 Improvements: - Introduces the `AnyMatcher` type for the `requests_mock.adapter.ANY` matcher. - Adds the `request_headers` and `complete_qs` request matching parameters to `MockerCore`'s registration methods (e.g. `register_uri`, `request`, get`, etc.). - Adds the `response_list` parameter to `MockerCore`'s registration methods so that it can be specified as a positional argument as in the [documentation](https://requests-mock.readthedocs.io/en/latest/response.html#response-lists)'s examples. - Ensures that `Adapter`'s `register_uri` method supports the same request matching parameters as `MockerCore`'s `register_uri`.
1 parent 94f20de commit bb47a36

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

requests_mock/adapter.pyi

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
from requests.adapters import BaseAdapter
44
from requests_mock import _RequestObjectProxy
5-
from typing import Any, List, Optional
5+
from typing import Any, Callable, Dict, List, NewType, Optional, Pattern, Union
66

7-
ANY: object = ...
7+
AnyMatcher = NewType("AnyMatcher", object)
8+
9+
ANY: AnyMatcher = ...
810

911
class _RequestHistoryTracker:
1012
request_history: List[_RequestObjectProxy] = ...
@@ -26,5 +28,17 @@ class _Matcher(_RequestHistoryTracker):
2628

2729
class Adapter(BaseAdapter, _RequestHistoryTracker):
2830
def __init__(self, case_sensitive: bool = ...) -> None: ...
29-
def register_uri(self, method: Any, url: Any, response_list: Optional[Any] = ..., **kwargs: Any) -> Any: ...
31+
def register_uri(
32+
self,
33+
method: Union[str, AnyMatcher],
34+
url: Union[str, Pattern[str], AnyMatcher],
35+
response_list: Optional[List[Dict[str, Any]]] = ...,
36+
request_headers: Dict[str, str] = ...,
37+
complete_qs: bool = ...,
38+
status_code: int = ...,
39+
text: str = ...,
40+
headers: Optional[Dict[str, str]] = ...,
41+
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
42+
**kwargs: Any
43+
) -> Any: ...
3044
def add_matcher(self, matcher: Any) -> None: ...

requests_mock/mocker.pyi

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Stubs for requests_mock.mocker
22

3+
from requests_mock.adapter import AnyMatcher
34
from requests import Response
45
from requests_mock.request import _RequestObjectProxy
56
from typing import Any, Callable, Dict, List, Optional, Pattern, Type, TypeVar, Union
@@ -30,73 +31,100 @@ class MockerCore:
3031
def call_count(self) -> int: ...
3132
def register_uri(
3233
self,
33-
method: Union[str, Any],
34-
url: Union[str, Pattern[str], Any],
34+
method: Union[str, AnyMatcher],
35+
url: Union[str, Pattern[str], AnyMatcher],
36+
response_list: Optional[List[Dict[str, Any]]] = ...,
37+
request_headers: Dict[str, str] = ...,
38+
complete_qs: bool = ...,
3539
status_code: int = ...,
3640
text: str = ...,
3741
headers: Optional[Dict[str, str]] = ...,
3842
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
3943
**kwargs: Any) -> Response: ...
4044
def request(
4145
self,
42-
method: Union[str, Any],
43-
url: Union[str, Pattern[str], Any],
46+
method: Union[str, AnyMatcher],
47+
url: Union[str, Pattern[str], AnyMatcher],
48+
response_list: Optional[List[Dict[str, Any]]] = ...,
49+
request_headers: Dict[str, str] = ...,
50+
complete_qs: bool = ...,
4451
status_code: int = ...,
4552
text: str = ...,
4653
headers: Optional[Dict[str, str]] = ...,
4754
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
4855
**kwargs: Any) -> Response: ...
4956
def get(
5057
self,
51-
url: Union[str, Pattern[str], Any],
58+
url: Union[str, Pattern[str], AnyMatcher],
59+
response_list: Optional[List[Dict[str, Any]]] = ...,
60+
request_headers: Dict[str, str] = ...,
61+
complete_qs: bool = ...,
5262
status_code: int = ...,
5363
text: str = ...,
5464
headers: Optional[Dict[str, str]] = ...,
5565
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
5666
**kwargs: Any) -> Response: ...
5767
def head(
5868
self,
59-
url: Union[str, Pattern[str], Any],
69+
url: Union[str, Pattern[str], AnyMatcher],
70+
response_list: Optional[List[Dict[str, Any]]] = ...,
71+
request_headers: Dict[str, str] = ...,
72+
complete_qs: bool = ...,
6073
status_code: int = ...,
6174
text: str = ...,
6275
headers: Optional[Dict[str, str]] = ...,
6376
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
6477
**kwargs: Any) -> Response: ...
6578
def options(
6679
self,
67-
url: Union[str, Pattern[str], Any],
80+
url: Union[str, Pattern[str], AnyMatcher],
81+
response_list: Optional[List[Dict[str, Any]]] = ...,
82+
request_headers: Dict[str, str] = ...,
83+
complete_qs: bool = ...,
6884
status_code: int = ...,
6985
text: str = ...,
7086
headers: Optional[Dict[str, str]] = ...,
7187
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
7288
**kwargs: Any) -> Response: ...
7389
def post(
7490
self,
75-
url: Union[str, Pattern[str], Any],
91+
url: Union[str, Pattern[str], AnyMatcher],
92+
response_list: Optional[List[Dict[str, Any]]] = ...,
93+
request_headers: Dict[str, str] = ...,
94+
complete_qs: bool = ...,
7695
status_code: int = ...,
7796
text: str = ...,
7897
headers: Optional[Dict[str, str]] = ...,
7998
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
8099
**kwargs: Any) -> Response: ...
81100
def put(
82101
self,
83-
url: Union[str, Pattern[str], Any],
102+
url: Union[str, Pattern[str], AnyMatcher],
103+
response_list: Optional[List[Dict[str, Any]]] = ...,
104+
request_headers: Dict[str, str] = ...,
105+
complete_qs: bool = ...,
84106
status_code: int = ...,
85107
text: str = ...,
86108
headers: Optional[Dict[str, str]] = ...,
87109
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
88110
**kwargs: Any) -> Response: ...
89111
def patch(
90112
self,
91-
url: Union[str, Pattern[str], Any],
113+
url: Union[str, Pattern[str], AnyMatcher],
114+
response_list: Optional[List[Dict[str, Any]]] = ...,
115+
request_headers: Dict[str, str] = ...,
116+
complete_qs: bool = ...,
92117
status_code: int = ...,
93118
text: str = ...,
94119
headers: Optional[Dict[str, str]] = ...,
95120
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
96121
**kwargs: Any) -> Response: ...
97122
def delete(
98123
self,
99-
url: Union[str, Pattern[str], Any],
124+
url: Union[str, Pattern[str], AnyMatcher],
125+
response_list: Optional[List[Dict[str, Any]]] = ...,
126+
request_headers: Dict[str, str] = ...,
127+
complete_qs: bool = ...,
100128
status_code: int = ...,
101129
text: str = ...,
102130
headers: Optional[Dict[str, str]] = ...,

0 commit comments

Comments
 (0)