Skip to content

Commit a9bad2b

Browse files
chuenchen309claude
andcommitted
fix(datastructures): MutableScopeHeaders.getall() treats an empty-list default as "no default"
`getall(key, default)` used `if default:` to decide whether a default was supplied, so an explicit `default=[]` (a legitimate way to ask for "return an empty list if the header is missing" rather than raising) is falsy and gets treated the same as `default=None` — raising KeyError instead of returning `[]`. Fix: check `if default is not None:` instead, matching the type signature (`default: list[str] | None = None`) and the pattern already used elsewhere in this file (e.g. `MutableScopeHeaders.__getitem__`). How verified: added test_mutable_scope_headers_getall_not_found_empty_list_default, confirmed it fails with KeyError before the fix and passes after. Full test_datastructures/ suite (148 tests) passes. mypy and pre-commit (ruff, unasyncd, typos) clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4a43322 commit a9bad2b

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

litestar/datastructures/headers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def getall(self, key: str, default: list[str] | None = None) -> list[str]:
169169
if header_name.decode("latin-1").lower() == name
170170
]
171171
if not values:
172-
if default:
172+
if default is not None:
173173
return default
174174
raise KeyError
175175
return values

tests/unit/test_datastructures/test_headers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ def test_mutable_scope_headers_getall_not_found_default(mutable_headers: Mutable
156156
assert mutable_headers.getall("bar", ["default"]) == ["default"]
157157

158158

159+
def test_mutable_scope_headers_getall_not_found_empty_list_default(mutable_headers: MutableScopeHeaders) -> None:
160+
"""An explicit empty-list default should be returned as-is, not treated as "no default"."""
161+
assert mutable_headers.getall("bar", []) == []
162+
163+
159164
def test_mutable_scope_headers_extend_header_value(
160165
raw_headers: "RawHeadersList", mutable_headers: MutableScopeHeaders
161166
) -> None:

0 commit comments

Comments
 (0)