Skip to content

Commit 6950c9e

Browse files
authored
fix: Relax types in matchers to use duck types (#699)
Fixes #698
1 parent 7706191 commit 6950c9e

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

responses/matchers.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from json.decoder import JSONDecodeError
55
from typing import Any
66
from typing import Callable
7-
from typing import Dict
87
from typing import List
8+
from typing import Mapping
9+
from typing import MutableMapping
910
from typing import Optional
1011
from typing import Pattern
1112
from typing import Tuple
@@ -17,7 +18,7 @@
1718
from urllib3.util.url import parse_url
1819

1920

20-
def _create_key_val_str(input_dict: Union[Dict[Any, Any], Any]) -> str:
21+
def _create_key_val_str(input_dict: Union[Mapping[Any, Any], Any]) -> str:
2122
"""
2223
Returns string of format {'key': val, 'key2': val2}
2324
Function is called recursively for nested dictionaries
@@ -57,8 +58,8 @@ def list_to_str(input_list: List[str]) -> str:
5758

5859

5960
def _filter_dict_recursively(
60-
dict1: Dict[Any, Any], dict2: Dict[Any, Any]
61-
) -> Dict[Any, Any]:
61+
dict1: Mapping[Any, Any], dict2: Mapping[Any, Any]
62+
) -> Mapping[Any, Any]:
6263
filtered_dict = {}
6364
for k, val in dict1.items():
6465
if k in dict2:
@@ -70,7 +71,7 @@ def _filter_dict_recursively(
7071

7172

7273
def urlencoded_params_matcher(
73-
params: Optional[Dict[str, str]], *, allow_blank: bool = False
74+
params: Optional[Mapping[str, str]], *, allow_blank: bool = False
7475
) -> Callable[..., Any]:
7576
"""
7677
Matches URL encoded data
@@ -100,7 +101,7 @@ def match(request: PreparedRequest) -> Tuple[bool, str]:
100101

101102

102103
def json_params_matcher(
103-
params: Optional[Union[Dict[str, Any], List[Any]]], *, strict_match: bool = True
104+
params: Optional[Union[Mapping[str, Any], List[Any]]], *, strict_match: bool = True
104105
) -> Callable[..., Any]:
105106
"""Matches JSON encoded data of request body.
106107
@@ -192,7 +193,7 @@ def match(request: PreparedRequest) -> Tuple[bool, str]:
192193

193194

194195
def query_param_matcher(
195-
params: Optional[Dict[str, Any]], *, strict_match: bool = True
196+
params: Optional[MutableMapping[str, Any]], *, strict_match: bool = True
196197
) -> Callable[..., Any]:
197198
"""Matcher to match 'params' argument in request.
198199
@@ -276,7 +277,7 @@ def match(request: PreparedRequest) -> Tuple[bool, str]:
276277
return match
277278

278279

279-
def request_kwargs_matcher(kwargs: Optional[Dict[str, Any]]) -> Callable[..., Any]:
280+
def request_kwargs_matcher(kwargs: Optional[Mapping[str, Any]]) -> Callable[..., Any]:
280281
"""
281282
Matcher to match keyword arguments provided to request
282283
@@ -308,7 +309,7 @@ def match(request: PreparedRequest) -> Tuple[bool, str]:
308309

309310

310311
def multipart_matcher(
311-
files: Dict[str, Any], data: Optional[Dict[str, str]] = None
312+
files: Mapping[str, Any], data: Optional[Mapping[str, str]] = None
312313
) -> Callable[..., Any]:
313314
"""
314315
Matcher to match 'multipart/form-data' content-type.
@@ -392,7 +393,7 @@ def match(request: PreparedRequest) -> Tuple[bool, str]:
392393

393394

394395
def header_matcher(
395-
headers: Dict[str, Union[str, Pattern[str]]], strict_match: bool = False
396+
headers: Mapping[str, Union[str, Pattern[str]]], strict_match: bool = False
396397
) -> Callable[..., Any]:
397398
"""
398399
Matcher to match 'headers' argument in request using the responses library.
@@ -408,7 +409,7 @@ def header_matcher(
408409
:return: (func) matcher
409410
"""
410411

411-
def _compare_with_regex(request_headers: Union[Dict[Any, Any], Any]) -> bool:
412+
def _compare_with_regex(request_headers: Union[Mapping[Any, Any], Any]) -> bool:
412413
if strict_match and len(request_headers) != len(headers):
413414
return False
414415

@@ -426,7 +427,7 @@ def _compare_with_regex(request_headers: Union[Dict[Any, Any], Any]) -> bool:
426427
return True
427428

428429
def match(request: PreparedRequest) -> Tuple[bool, str]:
429-
request_headers: Union[Dict[Any, Any], Any] = request.headers or {}
430+
request_headers: Union[Mapping[Any, Any], Any] = request.headers or {}
430431

431432
if not strict_match:
432433
# filter down to just the headers specified in the matcher

0 commit comments

Comments
 (0)