Skip to content

Commit

Permalink
Warn user about offset-naive object in last_modified property (aio-li…
Browse files Browse the repository at this point in the history
  • Loading branch information
greshilov committed Dec 6, 2020
1 parent 84bd28b commit a7072ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/5304.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Warn user about offset-naive object in last_modified property.
8 changes: 8 additions & 0 deletions aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,14 @@ def last_modified(
"%a, %d %b %Y %H:%M:%S GMT", time.gmtime(math.floor(value))
)
elif isinstance(value, datetime.datetime):
if not value.tzinfo:
warnings.warn(
RuntimeWarning(
"'last_modified' property received a offset-naive datetime. "
"Please specify a timezone for your datetime object."
)
)

self._headers[hdrs.LAST_MODIFIED] = time.strftime(
"%a, %d %b %Y %H:%M:%S GMT", value.utctimetuple()
)
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 @@ -269,6 +269,13 @@ def test_last_modified_round_consistency() -> None:
assert datetime_last_modified == float_last_modified


def test_last_modified_tz_unaware_warning() -> None:
resp = StreamResponse()

with pytest.warns(RuntimeWarning):
resp.last_modified = datetime.datetime.now()


async def test_start() -> None:
req = make_request("GET", "/")
resp = StreamResponse()
Expand Down

0 comments on commit a7072ec

Please sign in to comment.