freezegun
freezegun copied to clipboard
Handle rest_framework.throttling.SimpleRateThrottle
Handle rest_framework.throttling.SimpleRateThrottle
According to the issue: https://github.com/spulec/freezegun/issues/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 with fake_time
, executing self.timer()
(equivalent to timer(self)
) is working with time.time
but not with fake_time
:
How to reproduce
>>> import time
>>> class A:
... timer = time.time
... def test(self):
... print(self.timer())
...
>>> A().test()
1658255082.1697752
>>>
>>> from freezegun.api import fake_time
>>> class B:
... timer = fake_time
... def test(self):
... print(self.timer())
...
>>> B().test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in test
TypeError: fake_time() takes 0 positional arguments but 1 was given
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 the self.timer
usage at the origin of the issue).
I feel the change is not risky at all but I can miss something.
@staticdev, @boxed ,
Any opinion about this PR?
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 bow
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.
@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.