From c9636086130edad9d4fca44368995b612ebc484b Mon Sep 17 00:00:00 2001 From: Robsdedude Date: Thu, 25 Apr 2024 13:52:20 +0200 Subject: [PATCH] Fix `tick` delta type handling ## `FrozenDateTimeFactory.tick` The `delta` argument is capable of handling `float`s. In previous versions of freezgun, the `.pyi` type annotations were correctly reflecting that. For some reason, when moving the type annotations into the `.py` file, this information got lost. Further, checking for `isinstance(delta, numbers.Real)` is probably not what was intended as `fraction.Fraction` is a subclass of `Real`, but will cause an error when passed into `datetime.timedelta(seconds=delta)`. ## `StepTickTimeFactory.tick` The same issue with the type hint applies here. Fruther, passing an integer/float `delta` would lead to that number being added to the frozen `datetime.datetime`, which is not a valid operation (`TypeError: unsupported operand type(s) for +: 'datetime.datetime' and 'int'`). --- freezegun/api.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/freezegun/api.py b/freezegun/api.py index 0f11e0c..e48a871 100644 --- a/freezegun/api.py +++ b/freezegun/api.py @@ -531,8 +531,8 @@ def __init__(self, time_to_freeze: datetime.datetime): def __call__(self) -> datetime.datetime: return self.time_to_freeze - def tick(self, delta: Union[datetime.timedelta, int]=datetime.timedelta(seconds=1)) -> datetime.datetime: - if isinstance(delta, numbers.Real): + def tick(self, delta: Union[datetime.timedelta, float]=datetime.timedelta(seconds=1)) -> datetime.datetime: + if isinstance(delta, float): # noinspection PyTypeChecker self.time_to_freeze += datetime.timedelta(seconds=delta) else: @@ -557,9 +557,11 @@ def __call__(self) -> datetime.datetime: self.tick() return return_time - def tick(self, delta: Union[datetime.timedelta, int, None]=None) -> datetime.datetime: + def tick(self, delta: Union[datetime.timedelta, float, None]=None) -> datetime.datetime: if not delta: delta = datetime.timedelta(seconds=self.step_width) + elif isinstance(delta, float): + delta = datetime.timedelta(seconds=delta) self.time_to_freeze += delta # type: ignore return self.time_to_freeze