Skip to content

Commit

Permalink
Fix tick delta type handling
Browse files Browse the repository at this point in the history
## `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'`).
  • Loading branch information
robsdedude authored Apr 25, 2024
1 parent 17ea422 commit c963608
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions freezegun/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit c963608

Please sign in to comment.