Skip to content

Commit

Permalink
refactor: privatize local datetime aliases in falcon.util.misc
Browse files Browse the repository at this point in the history
  • Loading branch information
vytas7 committed Nov 5, 2023
1 parent f4ebc1c commit 601b4a5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

# General information about the project.
project = 'Falcon'
copyright = '{year} Falcon Contributors'.format(year=datetime.utcnow().year)
copyright = '{year} Falcon Contributors'.format(year=datetime.now().year)

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
20 changes: 15 additions & 5 deletions falcon/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,21 @@
_UNSAFE_CHARS = re.compile(r'[^a-zA-Z0-9.-]')

# PERF(kgriffs): Avoid superfluous namespace lookups
strptime: Callable[[str, str], datetime.datetime] = datetime.datetime.strptime
utcnow: Callable[[], datetime.datetime] = functools.partial(
_strptime: Callable[[str, str], datetime.datetime] = datetime.datetime.strptime
_utcnow: Callable[[], datetime.datetime] = functools.partial(
datetime.datetime.now, datetime.timezone.utc
)

# The above aliases were not underscored prior to Falcon 3.1.2.
strptime: Callable[[str, str], datetime.datetime] = deprecated(
'This was a private alias local to this module; '
'please reference datetime.strptime() directly.'
)(datetime.datetime.strptime)
utcnow: Callable[[], datetime.datetime] = deprecated(
'This was a private alias local to this module; '
'please reference datetime.utcnow() directly.'
)(datetime.datetime.utcnow)


# NOTE(kgriffs,vytas): This is tested in the PyPy gate but we do not want devs
# to have to install PyPy to check coverage on their workstations, so we use
Expand Down Expand Up @@ -134,7 +144,7 @@ def http_now() -> str:
e.g., 'Tue, 15 Nov 1994 12:45:26 GMT'.
"""

return dt_to_http(utcnow())
return dt_to_http(_utcnow())

Check warning on line 147 in falcon/util/misc.py

View check run for this annotation

Codecov / codecov/patch

falcon/util/misc.py#L147

Added line #L147 was not covered by tests


def dt_to_http(dt: datetime.datetime) -> str:
Expand Down Expand Up @@ -178,7 +188,7 @@ def http_date_to_dt(http_date: str, obs_date: bool = False) -> datetime.datetime
# over it, and setting up exception handling blocks each
# time around the loop, in the case that we don't actually
# need to check for multiple formats.
return strptime(http_date, '%a, %d %b %Y %H:%M:%S %Z')
return _strptime(http_date, '%a, %d %b %Y %H:%M:%S %Z')

Check warning on line 191 in falcon/util/misc.py

View check run for this annotation

Codecov / codecov/patch

falcon/util/misc.py#L191

Added line #L191 was not covered by tests

time_formats = (
'%a, %d %b %Y %H:%M:%S %Z',
Expand All @@ -190,7 +200,7 @@ def http_date_to_dt(http_date: str, obs_date: bool = False) -> datetime.datetime
# Loop through the formats and return the first that matches
for time_format in time_formats:
try:
return strptime(http_date, time_format)
return _strptime(http_date, time_format)

Check warning on line 203 in falcon/util/misc.py

View check run for this annotation

Codecov / codecov/patch

falcon/util/misc.py#L203

Added line #L203 was not covered by tests
except ValueError:
continue

Expand Down
4 changes: 2 additions & 2 deletions tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import falcon
from falcon import testing
from falcon.util.deprecation import DeprecatedWarning
from falcon.util.misc import utcnow
from falcon.util.misc import _utcnow

from _util import create_app # NOQA

Expand All @@ -33,7 +33,7 @@ def __init__(self, last_modified=None):
if last_modified is not None:
self.last_modified = last_modified
else:
self.last_modified = utcnow()
self.last_modified = _utcnow()

def _overwrite_headers(self, req, resp):
resp.content_type = 'x-falcon/peregrine'
Expand Down
8 changes: 4 additions & 4 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import falcon
import falcon.errors
import falcon.testing as testing
from falcon.util.misc import utcnow
from falcon.util.misc import _utcnow

from _util import create_app # NOQA

Expand All @@ -36,15 +36,15 @@ def process_request(self, req, resp):
class RequestTimeMiddleware:
def process_request(self, req, resp):
global context
context['start_time'] = utcnow()
context['start_time'] = _utcnow()

def process_resource(self, req, resp, resource, params):
global context
context['mid_time'] = utcnow()
context['mid_time'] = _utcnow()

def process_response(self, req, resp, resource, req_succeeded):
global context
context['end_time'] = utcnow()
context['end_time'] = _utcnow()
context['req_succeeded'] = req_succeeded

async def process_request_async(self, req, resp):
Expand Down

0 comments on commit 601b4a5

Please sign in to comment.