Skip to content

Commit

Permalink
Raise TypeError when setting StreamResponse.last_modified to an u…
Browse files Browse the repository at this point in the history
…nsupported type (#10146)

<!-- Thank you for your contribution! -->

## What do these changes do?

closes #10143

## Are there changes in behavior for the user?

Currently this would silently fail and discard the new value.

<img width="685" alt="Screenshot 2024-12-08 at 12 32 49 PM"
src="https://github.com/user-attachments/assets/b0cf00c3-cd2e-4be6-af94-d993491bd296">

(cherry picked from commit a818e51)
  • Loading branch information
bdraco authored and patchback[bot] committed Dec 12, 2024
1 parent 5d9d830 commit f8cf09a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/10146.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Setting :attr:`aiohttp.web.StreamResponse.last_modified` to an unsupported type will now raise :exc:`TypeError` instead of silently failing -- by :user:`bdraco`.
3 changes: 3 additions & 0 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,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__}" # type: ignore[unreachable]
raise TypeError(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 @@ -255,6 +255,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(TypeError, match="Unsupported type for last_modified: object"):
resp.last_modified = object() # type: ignore[assignment]


@pytest.mark.parametrize(
["header_val", "expected"],
[
Expand Down

0 comments on commit f8cf09a

Please sign in to comment.