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.

This commit is contained in:
L. Kärkkäinen 2023-10-14 20:22:28 +01:00
parent a5a9658896
commit b692309c91
3 changed files with 11 additions and 14 deletions

View File

@ -149,13 +149,11 @@ class CookieRequestParameters(RequestParameters):
except KeyError:
return super().get(name, default)
def getlist(
self, name: str, default: Optional[Any] = None
) -> Optional[Any]:
def getlist(self, name: str) -> list[Any]:
try:
return self._get_prefixed_cookie(name)
except KeyError:
return super().getlist(name, default)
return super().getlist(name)
def _get_prefixed_cookie(self, name: str) -> Any:
getitem = super().__getitem__

View File

@ -19,15 +19,14 @@ class RequestParameters(dict):
return super().get(name, [default])[0]
def getlist(
self, name: str, default: Optional[Any] = None
) -> Optional[Any]:
self, name: str
) -> list[Any]:
"""Return the entire list
Args:
name (str): The name of the parameter
default (Optional[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) or []

View File

@ -445,10 +445,10 @@ def test_cookie_accessors(app: Sanic):
"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"),
},
"getattr": {
"one": request.cookies.one,
@ -484,7 +484,7 @@ def test_cookie_accessors(app: Sanic):
"one": ["1"],
"two": ["2"],
"three": ["3"],
"four": ["fallback"],
"four": [],
},
"getattr": {
"one": "1",