-
Notifications
You must be signed in to change notification settings - Fork 269
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
Handle rest_framework.throttling.SimpleRateThrottle #459
base: master
Are you sure you want to change the base?
Conversation
@staticdev, @boxed , Any opinion about this PR? |
@@ -170,7 +170,7 @@ def get_current_time(): | |||
return freeze_factories[-1]() | |||
|
|||
|
|||
def fake_time(): | |||
def fake_time(*args, **kwargs): |
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.
Why *args, **kwargs here? That seems very wrong to me.
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.
Hello @boxed ,
Thanks for your review. May I humbly ask you to elaborate?
I think I made an effort to explain the reproduction of the bug, both in the description and in the test.
If I'm off track, can you possibly guide me to a solution that you think is more appropriate?
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.
@JulianMaurin this is breaking the API typing on api.pyi
, so users that have mypy will have problems. I also think you can add typed arguments to satisfy your needs with default arguments=None
so it will also not break current applications and maintain the types well defined.
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 thought this version does not break the application, in addition, fake_time
function is not defined in api.pyi
, but maybe I did not understand something.
Would you prefer this version (with a docstring ofc):
def fake_time(*args, **kwargs): | |
def fake_time(_ : Any): |
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.
@JulianMaurin putting an argument of type Any
makes the method still untyped. Also as @boxed adding *args, **kwargs
here does not make sense to me also, since to fix the bug you one need to be able to call time it with self
.
You can check in the definition of builtin time.time
there is NO argument whatsoever: https://github.com/python/cpython/blob/v3.9.6/Modules/timemodule.c#L86
Have to tried, instead of adding arguments, wrapping fake_time with @classmethod? Maybe it will fix this usage. If not I am sure some kind of wrapper like this will be needed.
@@ -170,7 +170,7 @@ def get_current_time(): | |||
return freeze_factories[-1]() | |||
|
|||
|
|||
def fake_time(): | |||
def fake_time(*args, **kwargs): |
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.
@JulianMaurin this is breaking the API typing on api.pyi
, so users that have mypy will have problems. I also think you can add typed arguments to satisfy your needs with default arguments=None
so it will also not break current applications and maintain the types well defined.
@@ -130,6 +131,14 @@ def test_fake_strftime_function(): | |||
assert fake_strftime_function() == '2012' | |||
|
|||
|
|||
@freeze_time("2022-01-01") | |||
def test_fake_time_function_as_class_attribute(): |
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.
It is great that you added tests, but it is also important to explain on README
the new optional usage in English so more people can easily reuse your solution =)
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.
You can also add some description of this change on CHANGELOG
.
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.
Hey @staticdev ,
Thank you for taking the time to develop.
I will add an entry to the changelog if we continue this PR, thanks for the suggestion 🙇
However I don't see what should be added to the README, can you be more precise?
It would be nice to fix this bug, but if you think this is not the right way at all, tell me directly and I will close the PR and we will all save time.
@@ -170,7 +170,7 @@ def get_current_time(): | |||
return freeze_factories[-1]() | |||
|
|||
|
|||
def fake_time(): | |||
def fake_time(*args, **kwargs): |
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 thought this version does not break the application, in addition, fake_time
function is not defined in api.pyi
, but maybe I did not understand something.
Would you prefer this version (with a docstring ofc):
def fake_time(*args, **kwargs): | |
def fake_time(_ : Any): |
@JulianMaurin I mean, you can add the usage in a class like you described in the description of your pull request. I did not say you are doing it wrong, you are in the right path. |
Handle rest_framework.throttling.SimpleRateThrottle
According to the issue: #382.
I can confirm that the issue comes from an incompatibility with the DRF throttling view (see: DRF code).
Once the
SimpleRateThrottle.timer
is patched withfake_time
, executingself.timer()
(equivalent totimer(self)
) is working withtime.time
but not withfake_time
:How to reproduce
About the fix
It was not that simple. Testing an API using the test client will load the module after the patch that is performed by Freezgun. To reproduce the context of late loading I introduce the
tests.fake_module_lazy
module (In order to add a test demonstrating the error).Then, to fix the error I changed the signature of the
freezegun.api.fake_time
function, to accept any args/kwars (and so allow theself.timer
usage at the origin of the issue).I feel the change is not risky at all but I can miss something.