Skip to content

Commit

Permalink
Raise ValueError when setting StreamResponse.last_modified to an unsu…
Browse files Browse the repository at this point in the history
…pported type

Currently this would silently fail and discard the new
value.

fixes #10143
  • Loading branch information
bdraco committed Dec 8, 2024
1 parent 6200513 commit be95343
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ def last_modified(
)
elif isinstance(value, str):
self._headers[hdrs.LAST_MODIFIED] = value
else:
msg = f"Unsupported type for last_modified: {type(value).__name__}"
raise ValueError(msg)

@property
def etag(self) -> Optional[ETag]:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ def test_last_modified_reset() -> None:
assert resp.last_modified is None


def test_last_modified_invalid_type() -> None:
resp = web.StreamResponse()

with pytest.raises(ValueError, match="Unsupported type for last_modified: object"):
resp.last_modified = object() # type: ignore[assignment]


@pytest.mark.parametrize(
"header_val",
(
Expand Down

0 comments on commit be95343

Please sign in to comment.