Repository: sanic-org/sanic
Pull request #2838 (2023-10-14 19:30:14)

Simplify (request.cookies.)getlist function
Removes a default value in case no matches were found, in which case it previously defaulted to `None`. Instead, return an empty list which is more sensible in this case as the return value will then always be a list.

Presumably practically no-one uses this functionality, and Pythonic tests `if None` and `if []` are equal. In case you do need a default value, there is an easy and a more efficient alternative because the fallback value after `or` is only constructed if needed and not in any case:

```python
list_of_foo = request.cookies.getlist("foo") or ["my", "default", "value"]
```


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/sanic-org/sanic/pull/2838?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org) Report
All modified lines are covered by tests :white_check_mark:
> Comparison is base [(`a5a9658`)](https://app.codecov.io/gh/sanic-org/sanic/commit/a5a9658896984ddad484e168d4cb5c96e589fbad?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org) 88.387% compared to head [(`b692309`)](https://app.codecov.io/gh/sanic-org/sanic/pull/2838?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org) 88.429%.

<details><summary>Additional details and impacted files</summary>


```diff
@@              Coverage Diff              @@
##              main     #2838       +/-   ##
=============================================
+ Coverage   88.387%   88.429%   +0.041%     
=============================================
  Files           92        92               
  Lines         7156      7156               
  Branches      1228      1228               
=============================================
+ Hits          6325      6328        +3     
+ Misses         575       574        -1     
+ Partials       256       254        -2     
```


| [Files](https://app.codecov.io/gh/sanic-org/sanic/pull/2838?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org) | Coverage Δ | |
|---|---|---|
| [sanic/cookies/request.py](https://app.codecov.io/gh/sanic-org/sanic/pull/2838?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org#diff-c2FuaWMvY29va2llcy9yZXF1ZXN0LnB5) | `95.833% <100.000%> (ø)` | |
| [sanic/request/parameters.py](https://app.codecov.io/gh/sanic-org/sanic/pull/2838?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org#diff-c2FuaWMvcmVxdWVzdC9wYXJhbWV0ZXJzLnB5) | `100.000% <100.000%> (ø)` | |

... and [1 file with indirect coverage changes](https://app.codecov.io/gh/sanic-org/sanic/pull/2838/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/sanic-org/sanic/pull/2838?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=sanic-org).


Patch:
diff --git a/sanic/cookies/request.py b/sanic/cookies/request.py
index cb049576..ca8ae069 100644
--- a/sanic/cookies/request.py
+++ b/sanic/cookies/request.py
@@ -155,8 +155,8 @@ class CookieRequestParameters(RequestParameters):
             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:
diff --git a/sanic/request/parameters.py b/sanic/request/parameters.py
index 0ae6b087..f7b2748b 100644
--- a/sanic/request/parameters.py
+++ b/sanic/request/parameters.py
@@ -1,6 +1,6 @@
 from __future__ import annotations
 
-from typing import Any, Optional
+from typing import Any, List, Optional
 
 
 class RequestParameters(dict):
@@ -19,15 +19,15 @@ class RequestParameters(dict):
         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 []


Test patch:
diff --git a/tests/test_cookies.py b/tests/test_cookies.py
index 29e1ec25..599a24ef 100644
--- a/tests/test_cookies.py
+++ b/tests/test_cookies.py
@@ -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") == []
 
 
 # ------------------------------------------------------------ #
@@ -461,10 +459,11 @@ 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"),
+                    "five": request.cookies.getlist("five", ["fallback"]),
                 },
                 "getattr": {
                     "one": request.cookies.one,
@@ -500,7 +499,8 @@ def test_cookie_accessors(app: Sanic):
             "one": ["1"],
             "two": ["2"],
             "three": ["3"],
-            "four": ["fallback"],
+            "four": [],
+            "five": ["fallback"],
         },
         "getattr": {
             "one": "1",