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

Simplify (request.cookies.)getlist function #2838

Merged
merged 6 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 5 additions & 7 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 @@ -18,16 +18,14 @@ def get(self, name: str, default: Optional[Any] = None) -> Optional[Any]:
""" # noqa: E501
return super().get(name, [default])[0]

def getlist(
self, name: str, default: Optional[Any] = None
) -> Optional[Any]:
def getlist(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
Loading