Skip to content

Commit

Permalink
chore: deprecate TimezoneGMT class (#2302)
Browse files Browse the repository at this point in the history
* chore: deprecate TimezoneGMT class

* chore: address review comments
  • Loading branch information
CaselIT authored Aug 27, 2024
1 parent a9f6141 commit 321bda1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
1 change: 1 addition & 0 deletions docs/_newsfragments/2301.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecated the ``TimezoneGMT`` class. Use :attr:`datetime.timezone.utc` instead.
6 changes: 2 additions & 4 deletions falcon/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from __future__ import annotations

from datetime import timezone
import functools
import mimetypes
from typing import Dict, Optional
Expand All @@ -35,14 +36,11 @@
from falcon.util import http_cookies
from falcon.util import http_status_to_code
from falcon.util import structures
from falcon.util import TimezoneGMT
from falcon.util.deprecation import AttributeRemovedError
from falcon.util.deprecation import deprecated
from falcon.util.uri import encode_check_escaped as uri_encode
from falcon.util.uri import encode_value_check_escaped as uri_encode_value

GMT_TIMEZONE = TimezoneGMT()

_STREAM_LEN_REMOVED_MSG = (
'The deprecated stream_len property was removed in Falcon 3.0. '
'Please use Response.set_stream() or Response.content_length instead.'
Expand Down Expand Up @@ -503,7 +501,7 @@ def set_cookie( # noqa: C901
self._cookies[name]['expires'] = expires.strftime(fmt)
else:
# aware
gmt_expires = expires.astimezone(GMT_TIMEZONE)
gmt_expires = expires.astimezone(timezone.utc)
self._cookies[name]['expires'] = gmt_expires.strftime(fmt)

if max_age:
Expand Down
6 changes: 6 additions & 0 deletions falcon/util/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import datetime
from typing import Optional

from .deprecation import deprecated

__all__ = ('TimezoneGMT',)


Expand All @@ -20,6 +22,10 @@ class TimezoneGMT(datetime.tzinfo):

GMT_ZERO = datetime.timedelta(hours=0)

@deprecated('TimezoneGMT is deprecated, use datetime.timezone.utc instead')
def __init__(self) -> None:
super().__init__()

def utcoffset(self, dt: Optional[datetime.datetime]) -> datetime.timedelta:
"""Get the offset from UTC.
Expand Down
31 changes: 6 additions & 25 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from datetime import tzinfo
from http import cookies as http_cookies
import re

Expand All @@ -10,25 +9,10 @@
import falcon
import falcon.testing as testing
from falcon.util import http_date_to_dt
from falcon.util import TimezoneGMT

UNICODE_TEST_STRING = 'Unicode_\xc3\xa6\xc3\xb8'


class TimezoneGMTPlus1(tzinfo):
def utcoffset(self, dt):
return timedelta(hours=1)

def tzname(self, dt):
return 'GMT+1'

def dst(self, dt):
return timedelta(hours=1)


GMT_PLUS_ONE = TimezoneGMTPlus1()


def utcnow_naive():
return datetime.now(timezone.utc).replace(tzinfo=None)

Expand All @@ -49,7 +33,9 @@ def on_post(self, req, resp):
resp.unset_cookie('bad')

def on_put(self, req, resp):
e = datetime(year=2050, month=1, day=1, tzinfo=GMT_PLUS_ONE) # aware
e = datetime(
year=2050, month=1, day=1, tzinfo=timezone(timedelta(hours=1))
) # aware
resp.set_cookie('foo', 'bar', http_only=False, secure=False, expires=e)
resp.unset_cookie('bad')

Expand Down Expand Up @@ -289,7 +275,7 @@ def test_cookie_expires_aware(client):
assert not cookie.secure


def test_cookies_setable(client):
def test_cookies_setable():
resp = falcon.Response()

assert resp._cookies is None
Expand Down Expand Up @@ -321,14 +307,14 @@ def test_cookie_max_age_float_and_string(client, cookie_name):
assert not cookie.secure


def test_response_unset_cookie(client):
def test_response_unset_cookie():
resp = falcon.Response()
resp.unset_cookie('bad')
resp.set_cookie('bad', 'cookie', max_age=300)
resp.unset_cookie('bad')

morsels = list(resp._cookies.values())
len(morsels) == 1
assert len(morsels) == 1

bad_cookie = morsels[0]
assert bad_cookie['expires'] == -1
Expand All @@ -343,11 +329,6 @@ def test_response_unset_cookie(client):
assert expiration < utcnow_naive()


def test_cookie_timezone(client):
tz = TimezoneGMT()
assert tz.tzname(timedelta(0)) == 'GMT'


# =====================================================================
# Request
# =====================================================================
Expand Down
12 changes: 12 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import functools
import http
Expand All @@ -22,6 +23,7 @@
from falcon.util import misc
from falcon.util import structures
from falcon.util import uri
from falcon.util.time import TimezoneGMT

try:
import msgpack # type: ignore
Expand Down Expand Up @@ -1419,3 +1421,13 @@ def test_json_deprecation():

with pytest.raises(AttributeError):
falcon.util.some_imaginary_module


def test_TimezoneGMT():
with pytest.warns(deprecation.DeprecatedWarning):
tz = TimezoneGMT()

z = timedelta(0)
assert tz.tzname(None) == 'GMT'
assert tz.dst(None) == z
assert tz.utcoffset(None) == z

0 comments on commit 321bda1

Please sign in to comment.