Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

responses.add_callback triggers DeprecationWarning beyond user's control #464

Closed
mblayman opened this issue Jan 11, 2022 · 10 comments · Fixed by #470
Closed

responses.add_callback triggers DeprecationWarning beyond user's control #464

mblayman opened this issue Jan 11, 2022 · 10 comments · Fixed by #470
Assignees
Labels

Comments

@mblayman
Copy link

Steps to Reproduce

My company uses responses in combination with pytest with deprecation warnings treated as failures. The dependabot upgrade from 0.16.0 to 0.17.0 failed.

This test (which I've scrubbed to avoid sharing irrelevant and company sensitive details) triggers a DeprecationWarning upon execution.

    @responses.activate
    def test_something(self, caplog):
        # setup elided
    
        def request_callback(request):
            """Used to validate that the request correctly sets the status to deleted"""
            payload = json.loads(request.body)
            assert payload['StatusType'] == SOME_CONSTANT_TO_CHECK
            return 200, {}, json.dumps(SOME_RESPONSE_DATA)
    
       responses.add_callback(
            responses.POST, '/a/url/path', callback=request_callback
       )

Here is the warning:

E           DeprecationWarning: Argument 'match_querystring' is deprecated.
Use 'responses.matchers.query_param_matcher' or 'responses.matchers.query_string_matcher'

Expected Result

This test makes no use of match_querystring. I would not expect to see a deprecation warning.

Actual Result

A deprecation warning triggered.

I believe the reason is because add_callback sets match_querystring to False by default (see https://github.com/getsentry/responses/blob/0.17.0/responses/__init__.py#L719).

_should_match_querystring, the method where the deprecation warning is triggered, expects the more exact check of if match_querystring_argument is not None: (see https://github.com/getsentry/responses/blob/0.17.0/responses/__init__.py#L333)

My guess is that _should_match_querystring should be able to check the False value too to prevent disruption to the add_callback API.

Thanks for the help. Please let me know if I can do more to help or if more information is needed.

@beliaev-maksim
Copy link
Collaborator

beliaev-maksim commented Jan 11, 2022

@markstory
actually, this looks as architectural mistake that add_callback was setting False opposite to Response object
Changing default to None can cause issues when users have URL path with query but do not want to match query string.
Suppressing deprecation also looks strange. Solution for the user is to update tests and set manually match_querystring=None

or we can go a hard way and in next version release 1.0.0 with drop of multiple python versions and removing all deprecated parts. Then we can set default to ignore all query strings until it is explicitly defined via matcher

raphaelm added a commit to pretix/pretix that referenced this issue Jan 12, 2022
beliaev-maksim added a commit to beliaev-maksim/responses that referenced this issue Jan 12, 2022
added tests to catch the behaviour described in getsentry#464
@beliaev-maksim beliaev-maksim mentioned this issue Jan 12, 2022
@beliaev-maksim
Copy link
Collaborator

@mblayman. @jankrepl

can you please check if https://github.com/beliaev-maksim/responses/tree/fix_464 fixes the issue for you

@bblommers
Copy link
Contributor

@beliaev-maksim I'm still seeing the warning on this branch, using pip install https://github.com/beliaev-maksim/responses/archive/refs/heads/fix_464.zip

@beliaev-maksim
Copy link
Collaborator

@bblommers
And you don't have explicit definition of the argument in add_callback ?

Can you please provide minimal reproducible?

@bblommers
Copy link
Contributor

@bblommers And you don't have explicit definition of the argument in add_callback ?

Ah, apologies - we do explicitly add that argument, so the warning is to be expected.

I cannot reproduce the warning after removing the argument!

@beliaev-maksim
Copy link
Collaborator

@bblommers
correct,
if you need to match query strings, please switch to matchers.
This provides explicit comparison of requests

@jankrepl
Copy link

@mblayman. @jankrepl

can you please check if https://github.com/beliaev-maksim/responses/tree/fix_464 fixes the issue for you

Seems to be working:) Thanks for the quick help:)

@beliaev-maksim
Copy link
Collaborator

@jankrepl
thanks for checking!

beliaev-maksim added a commit to beliaev-maksim/responses that referenced this issue Jan 12, 2022
added tests to catch the behaviour described in getsentry#464
markstory pushed a commit that referenced this issue Jan 12, 2022
* added fix for wrong boolean value in add_callback
* added tests to catch the behaviour described in #464
@mblayman
Copy link
Author

Thank you for fixing this quickly!

@beliaev-maksim
Copy link
Collaborator

@mblayman
always glad to help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants