Skip to content

Commit

Permalink
Fix deprecation warning being emitted by add_callback (#470)
Browse files Browse the repository at this point in the history
* added fix for wrong boolean value in add_callback
* added tests to catch the behaviour described in #464
  • Loading branch information
beliaev-maksim committed Jan 12, 2022
1 parent 69b7df7 commit a552398
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CHANGES
Expand Up @@ -7,6 +7,8 @@
* Removed internal `_quote` from `test_responses.py`
* Removed internal `_matches` attribute of `RequestsMock` object.
* Generated decorator wrapper now uses stdlib features instead of strings and exec
* Fix issue when Deprecation Warning was raised with default arguments
in `responses.add_callback` due to `match_querystring`. See #464

0.17.0
------
Expand All @@ -16,7 +18,7 @@
* Fixed issue when `passthru_prefixes` persisted across tests.
Now `add_passthru` is valid only within a context manager or for a single function and
cleared on exit
* Deprecate `match_querystring` argument in `Response`` and `CallbackResponse`.
* Deprecate `match_querystring` argument in `Response` and `CallbackResponse`.
Use `responses.matchers.query_param_matcher` or `responses.matchers.query_string_matcher`
* Added support for non-UTF-8 bytes in `responses.matchers.multipart_matcher`
* Added `responses.registries`. Now user can create custom registries to
Expand Down
28 changes: 19 additions & 9 deletions responses/__init__.py
Expand Up @@ -61,6 +61,15 @@
logger = logging.getLogger("responses")


class FalseBool:
# used for backwards compatibility, see
# https://github.com/getsentry/responses/issues/464
def __bool__(self):
return False

__nonzero__ = __bool__


def urlencoded_params_matcher(params):
warn(
"Function is deprecated. Use 'from responses.matchers import urlencoded_params_matcher'",
Expand Down Expand Up @@ -239,14 +248,15 @@ def _should_match_querystring(self, match_querystring_argument):
return False

if match_querystring_argument is not None:
warn(
(
"Argument 'match_querystring' is deprecated. "
"Use 'responses.matchers.query_param_matcher' or "
"'responses.matchers.query_string_matcher'"
),
DeprecationWarning,
)
if not isinstance(match_querystring_argument, FalseBool):
warn(
(
"Argument 'match_querystring' is deprecated. "
"Use 'responses.matchers.query_param_matcher' or "
"'responses.matchers.query_string_matcher'"
),
DeprecationWarning,
)
return match_querystring_argument

return bool(urlparse(self.url).query)
Expand Down Expand Up @@ -621,7 +631,7 @@ def add_callback(
method,
url,
callback,
match_querystring=False,
match_querystring=FalseBool(),
content_type="text/plain",
match=(),
):
Expand Down
6 changes: 5 additions & 1 deletion responses/__init__.pyi
Expand Up @@ -25,6 +25,7 @@ from typing_extensions import Literal
from unittest import mock as std_mock
from urllib.parse import quote as quote
from urllib3.response import HTTPHeaderDict # type: ignore # Not currently exposed in typestubs.

from .matchers import urlencoded_params_matcher, json_params_matcher


Expand Down Expand Up @@ -63,6 +64,9 @@ class CallList(Sequence[Call], Sized):
def add(self, request: PreparedRequest, response: _Body) -> None: ...
def reset(self) -> None: ...

class FalseBool:
def __bool__(self) -> bool: ...

class BaseResponse:
passthrough: bool = ...
content_type: Optional[str] = ...
Expand Down Expand Up @@ -132,7 +136,7 @@ class CallbackResponse(BaseResponse):
callback: Callable[[Any], Any],
stream: bool = ...,
content_type: Optional[str] = ...,
match_querystring: bool = ...,
match_querystring: Union[bool, FalseBool] = ...,
match: MatcherIterable = ...,
) -> None: ...
def get_response( # type: ignore [override]
Expand Down
39 changes: 38 additions & 1 deletion responses/test_responses.py
Expand Up @@ -559,11 +559,48 @@ def run():
assert_reset()


def test_callback_deprecated_argument():
def test_callback_deprecated_stream_argument():
with pytest.deprecated_call():
CallbackResponse(responses.GET, "url", lambda x: x, stream=False)


def test_callback_deprecated_match_querystring_argument():
with pytest.deprecated_call():
CallbackResponse(responses.GET, "url", lambda x: x, match_querystring=False)


def test_callback_match_querystring_default_false():
"""
Test to ensure that by default 'match_querystring' in 'add_callback' is set to False
and does not raise deprecation
see: https://github.com/getsentry/responses/issues/464 and related PR
"""
body = b"test callback"
status = 200
params = {"hello": "world", "I am": "a big test"}
headers = {"foo": "bar"}
url = "http://example.com/"

def request_callback(_request):
return status, headers, body

@responses.activate
def run():
responses.add_callback(responses.GET, url, request_callback, content_type=None)
resp = requests.get(url, params=params)
assert resp.text == "test callback"
assert resp.status_code == status
assert "foo" in resp.headers

with pytest.warns(None) as record:
run()

# check that no deprecation warning was raised
assert not record

assert_reset()


def test_callback_exception_result():
result = Exception()
url = "http://example.com/"
Expand Down

0 comments on commit a552398

Please sign in to comment.