types-freezegun
Typing stubs for the `freezegun` library, enabling static type checking with tools like Mypy, PyCharm, and Pytype. This PEP 561 compliant package provides type hints for `freezegun`, which allows Python tests to manipulate time. It is maintained as part of the `typeshed` project and its updates generally follow `freezegun`'s release cycle for compatibility.
Warnings
- breaking `types-freezegun` is deprecated and should be uninstalled if you are using `freezegun` version 1.2.1 or newer. `freezegun` now includes its own type annotations directly within the package, rendering these external stubs redundant.
- gotcha Mismatched versions of `freezegun` and `types-freezegun` can lead to incorrect or incomplete type checking results. While `typeshed` aims for compatibility, newer features or breaking changes in `freezegun` might not be immediately reflected in older `types-freezegun` stubs, or vice-versa.
- gotcha When `freezegun` is active, `datetime.datetime.now()` returns `FakeDatetime` objects, which inherit from `datetime.datetime` but are not strictly identical. Libraries or code performing strict type checks (e.g., `type(obj) is datetime.datetime`) rather than `isinstance(obj, datetime.datetime)` might encounter runtime errors or unexpected behavior, even if type checking passes.
Install
-
pip install types-freezegun
Imports
- freeze_time
from freezegun import freeze_time
Quickstart
import datetime
from freezegun import freeze_time
# Example using the decorator
@freeze_time("2023-01-01")
def demonstrate_decorator_freeze():
assert datetime.datetime.now() == datetime.datetime(2023, 1, 1)
print(f"Current time (decorator): {datetime.datetime.now()}")
demonstrate_decorator_freeze()
# Example using the context manager
with freeze_time("2024-02-29 10:30:00"):
assert datetime.date.today() == datetime.date(2024, 2, 29)
assert datetime.datetime.now().hour == 10
print(f"Current date (context manager): {datetime.date.today()}")
# To verify type checking, run a tool like Mypy (e.g., `mypy your_script.py`)
# with `types-freezegun` installed. It will ensure `freeze_time` and
# related objects are used with correct types.