Skip to content

Commit

Permalink
Additional improvements to response matching type stubs
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
OldSneerJaw committed May 11, 2021
1 parent 94f20de commit bb47a36
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
20 changes: 17 additions & 3 deletions requests_mock/adapter.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from requests.adapters import BaseAdapter
from requests_mock import _RequestObjectProxy
from typing import Any, List, Optional
from typing import Any, Callable, Dict, List, NewType, Optional, Pattern, Union

ANY: object = ...
AnyMatcher = NewType("AnyMatcher", object)

ANY: AnyMatcher = ...

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

class Adapter(BaseAdapter, _RequestHistoryTracker):
def __init__(self, case_sensitive: bool = ...) -> None: ...
def register_uri(self, method: Any, url: Any, response_list: Optional[Any] = ..., **kwargs: Any) -> Any: ...
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 = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any
) -> Any: ...
def add_matcher(self, matcher: Any) -> None: ...
50 changes: 39 additions & 11 deletions requests_mock/mocker.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Stubs for requests_mock.mocker

from requests_mock.adapter import AnyMatcher
from requests import Response
from requests_mock.request import _RequestObjectProxy
from typing import Any, Callable, Dict, List, Optional, Pattern, Type, TypeVar, Union
Expand Down Expand Up @@ -30,73 +31,100 @@ class MockerCore:
def call_count(self) -> int: ...
def register_uri(
self,
method: Union[str, Any],
url: Union[str, Pattern[str], Any],
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 = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def request(
self,
method: Union[str, Any],
url: Union[str, Pattern[str], Any],
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 = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def get(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def head(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def options(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def post(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def put(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def patch(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
additional_matcher: Optional[Callable[[_RequestObjectProxy], bool]] = ...,
**kwargs: Any) -> Response: ...
def delete(
self,
url: Union[str, Pattern[str], Any],
url: Union[str, Pattern[str], AnyMatcher],
response_list: Optional[List[Dict[str, Any]]] = ...,
request_headers: Dict[str, str] = ...,
complete_qs: bool = ...,
status_code: int = ...,
text: str = ...,
headers: Optional[Dict[str, str]] = ...,
Expand Down

0 comments on commit bb47a36

Please sign in to comment.