-
-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get TimeStamper working properly if datetime module is mocked before its instantiation #364
Get TimeStamper working properly if datetime module is mocked before its instantiation #364
Conversation
…the instantiation
@@ -333,7 +333,17 @@ def _make_stamper( | |||
if fmt is None and not utc: | |||
raise ValueError("UNIX timestamps are always UTC.") | |||
|
|||
now = getattr(datetime.datetime, "utcnow" if utc else "now") | |||
now: Callable[[], datetime.datetime] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- This declaration is not necessary, but I prefer the "Φ" to be obvious.
dc04145
to
1c5c779
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it's a slight performance regression but function calls are getting cheaper over the past releases. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like mypy is not happy:
src/structlog/processors.py:372: error: Argument 1 to "strftime" of "datetime" has incompatible type "Optional[str]"; expected "str"
This looks like a false error because |
You're right, looks like a mypy bug! |
Summary
Because the reference to either
datetime.now()
ordatetime.utcnow()
is retrieved duringTimeStamper
is being created, it gets into trouble when FreezeGun or any similar mechanism that patchesdatetime
module is applied before the instantiation. This patchs proposes a modification to it so that the invocation to the "now" function will always be done with dereferences from the datetime module object, albeit the slight possible performance penalty.Checklist
docs/api.rst
by hand.versionadded
,versionchanged
, ordeprecated
directives. Find the appropriate next version in our__init__.py
file..rst
files is written using semantic newlines.