Skip to content

Commit c963608

Browse files
authored
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'`).
1 parent 17ea422 commit c963608

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

freezegun/api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ def __init__(self, time_to_freeze: datetime.datetime):
531531
def __call__(self) -> datetime.datetime:
532532
return self.time_to_freeze
533533

534-
def tick(self, delta: Union[datetime.timedelta, int]=datetime.timedelta(seconds=1)) -> datetime.datetime:
535-
if isinstance(delta, numbers.Real):
534+
def tick(self, delta: Union[datetime.timedelta, float]=datetime.timedelta(seconds=1)) -> datetime.datetime:
535+
if isinstance(delta, float):
536536
# noinspection PyTypeChecker
537537
self.time_to_freeze += datetime.timedelta(seconds=delta)
538538
else:
@@ -557,9 +557,11 @@ def __call__(self) -> datetime.datetime:
557557
self.tick()
558558
return return_time
559559

560-
def tick(self, delta: Union[datetime.timedelta, int, None]=None) -> datetime.datetime:
560+
def tick(self, delta: Union[datetime.timedelta, float, None]=None) -> datetime.datetime:
561561
if not delta:
562562
delta = datetime.timedelta(seconds=self.step_width)
563+
elif isinstance(delta, float):
564+
delta = datetime.timedelta(seconds=delta)
563565
self.time_to_freeze += delta # type: ignore
564566
return self.time_to_freeze
565567

0 commit comments

Comments
 (0)