From 30479765cb311c8ab807fb923ba193a149a84f0c Mon Sep 17 00:00:00 2001 From: Stephen Sadowski Date: Sat, 10 Apr 2021 17:04:33 -0500 Subject: [PATCH] Fix request.args.pop removes parameters inconsistently (#2110) * Fix https://github.com/sanic-org/sanic/issues/2106 * style * also apply fix to request.query_args * add test Co-authored-by: Arthur Goldberg Co-authored-by: artcg --- sanic/request.py | 18 ++++++++++++------ tests/test_requests.py | 11 +++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index 6ba809ca..95c79e53 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -378,9 +378,12 @@ class Request: :type errors: str :return: RequestParameters """ - if not self.parsed_args[ - (keep_blank_values, strict_parsing, encoding, errors) - ]: + if ( + keep_blank_values, + strict_parsing, + encoding, + errors, + ) not in self.parsed_args: if self.query_string: self.parsed_args[ (keep_blank_values, strict_parsing, encoding, errors) @@ -434,9 +437,12 @@ class Request: :type errors: str :return: list """ - if not self.parsed_not_grouped_args[ - (keep_blank_values, strict_parsing, encoding, errors) - ]: + if ( + keep_blank_values, + strict_parsing, + encoding, + errors, + ) not in self.parsed_not_grouped_args: if self.query_string: self.parsed_not_grouped_args[ (keep_blank_values, strict_parsing, encoding, errors) diff --git a/tests/test_requests.py b/tests/test_requests.py index 655a4079..665f936d 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -291,6 +291,17 @@ def test_query_string(app): assert request.args.getlist("test1") == ["1"] assert request.args.get("test3", default="My value") == "My value" +def test_popped_stays_popped(app): + @app.route("/") + async def handler(request): + return text("OK") + + request, response = app.test_client.get( + "/", params=[("test1", "1")] + ) + + assert request.args.pop("test1") == ["1"] + assert "test1" not in request.args @pytest.mark.asyncio async def test_query_string_asgi(app):