Skip to content

Commit

Permalink
Simplify (request.cookies.)getlist function (#2838)
Browse files Browse the repository at this point in the history
* Remove the default argument to all uses of `getlist` in Sanic in case none are found. Previously it defaulted to returning `None`. Instead return an empty list which is sensible and avoids ambiguity with return types.

* Keep default param

* squash

* format

---------

Co-authored-by: L. Kärkkäinen <[email protected]>
Co-authored-by: Adam Hopkins <[email protected]>
  • Loading branch information
3 people authored Jun 23, 2024
1 parent e3d59c1 commit 39d4ea4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions sanic/cookies/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def get(self, name: str, default: Optional[Any] = None) -> Optional[Any]:
return super().get(name, default)

def getlist(
self, name: str, default: Optional[Any] = None
) -> Optional[Any]:
self, name: str, default: Optional[List[Any]] = None
) -> List[Any]:
try:
return self._get_prefixed_cookie(name)
except KeyError:
Expand Down
12 changes: 6 additions & 6 deletions sanic/request/parameters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Any, Optional
from typing import Any, List, Optional


class RequestParameters(dict):
Expand All @@ -19,15 +19,15 @@ def get(self, name: str, default: Optional[Any] = None) -> Optional[Any]:
return super().get(name, [default])[0]

def getlist(
self, name: str, default: Optional[Any] = None
) -> Optional[Any]:
self, name: str, default: Optional[List[Any]] = None
) -> List[Any]:
"""Return the entire list
Args:
name (str): The name of the parameter
default (Optional[Any], optional): The default value. Defaults to None.
default (Optional[List[Any]], optional): The default value. Defaults to None.
Returns:
Optional[Any]: The entire list
list[Any]: The entire list of values or [] if not found
""" # noqa: E501
return super().get(name, default)
return super().get(name, default) or []
16 changes: 8 additions & 8 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def test_request_cookies():
assert c.getlist("foo") == ["one", "two"]
assert c.getlist("abc") == ["xyz"]
assert c.getlist("") == ["bare", "bare2"]
assert (
c.getlist("bare") is None
) # [] might be sensible but we got None for now
assert c.getlist("bare") == []


# ------------------------------------------------------------ #
Expand Down Expand Up @@ -461,10 +459,11 @@ async def handler(request: Request):
"four": request.cookies.get("four", "fallback"),
},
"getlist": {
"one": request.cookies.getlist("one", ["fallback"]),
"two": request.cookies.getlist("two", ["fallback"]),
"three": request.cookies.getlist("three", ["fallback"]),
"four": request.cookies.getlist("four", ["fallback"]),
"one": request.cookies.getlist("one"),
"two": request.cookies.getlist("two"),
"three": request.cookies.getlist("three"),
"four": request.cookies.getlist("four"),
"five": request.cookies.getlist("five", ["fallback"]),
},
"getattr": {
"one": request.cookies.one,
Expand Down Expand Up @@ -500,7 +499,8 @@ async def handler(request: Request):
"one": ["1"],
"two": ["2"],
"three": ["3"],
"four": ["fallback"],
"four": [],
"five": ["fallback"],
},
"getattr": {
"one": "1",
Expand Down

0 comments on commit 39d4ea4

Please sign in to comment.