Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typeshed types #403

Merged
merged 1 commit into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions freezegun/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .api import freeze_time as freeze_time

10 changes: 7 additions & 3 deletions freezegun/_async.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import functools
from typing import Any, Callable, TypeVar, cast


def wrap_coroutine(api, coroutine):
_CallableT = TypeVar("_CallableT", bound=Callable[..., Any])


def wrap_coroutine(api: Any, coroutine: _CallableT) -> _CallableT:
@functools.wraps(coroutine)
async def wrapper(*args, **kwargs):
async def wrapper(*args: Any, **kwargs: Any) -> Any:
with api as time_factory:
if api.as_arg:
result = await coroutine(time_factory, *args, **kwargs)
else:
result = await coroutine(*args, **kwargs)
return result

return wrapper
return cast(_CallableT, wrapper)
10 changes: 7 additions & 3 deletions freezegun/_async_coroutine.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import functools
from typing import Any, Callable, TypeVar, cast

import asyncio


def wrap_coroutine(api, coroutine):
_CallableT = TypeVar("_CallableT", bound=Callable[..., Any])


def wrap_coroutine(api: Any, coroutine: _CallableT) -> _CallableT:
@functools.wraps(coroutine)
@asyncio.coroutine
def wrapper(*args, **kwargs):
def wrapper(*args: Any, **kwargs: Any) -> Any:
with api as time_factory:
if api.as_arg:
result = yield from coroutine(time_factory, *args, **kwargs)
else:
result = yield from coroutine(*args, **kwargs)
return result

return wrapper
return cast(_CallableT, wrapper)
56 changes: 56 additions & 0 deletions freezegun/api.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from datetime import date, datetime, timedelta
from numbers import Real
from typing import Any, Awaitable, Callable, Iterator, Optional, Sequence, Type, TypeVar, Union, overload

_T = TypeVar("_T")
_Freezable = Union[str, datetime, date, timedelta]

class TickingDateTimeFactory(object):
def __init__(self, time_to_freeze: datetime, start: datetime) -> None: ...
def __call__(self) -> datetime: ...

class FrozenDateTimeFactory(object):
def __init__(self, time_to_freeze: datetime) -> None: ...
def __call__(self) -> datetime: ...
def tick(self, delta: Union[float, Real, timedelta] = ...) -> None: ...
def move_to(self, target_datetime: Optional[_Freezable]) -> None: ...

class StepTickTimeFactory(object):
def __init__(self, time_to_freeze: datetime, step_width: float) -> None: ...
def __call__(self) -> datetime: ...
def tick(self, delta: Optional[timedelta] = ...) -> None: ...
def update_step_width(self, step_width: float) -> None: ...
def move_to(self, target_datetime: Optional[_Freezable]) -> None: ...

class _freeze_time:
def __init__(
self,
time_to_freeze_str: Optional[_Freezable],
tz_offset: float,
ignore: Sequence[str],
tick: bool,
as_arg: bool,
auto_tick_seconds: float,
) -> None: ...
@overload
def __call__(self, func: Type[_T]) -> Type[_T]: ...
@overload
def __call__(self, func: Callable[..., Awaitable[_T]]) -> Callable[..., Awaitable[_T]]: ...
@overload
def __call__(self, func: Callable[..., _T]) -> Callable[..., _T]: ...
def __enter__(self) -> Any: ...
def __exit__(self, *args: Any) -> None: ...
def start(self) -> Any: ...
def stop(self) -> None: ...
def decorate_class(self, klass: Type[_T]) -> _T: ...
def decorate_coroutine(self, coroutine: _T) -> _T: ...
def decorate_callable(self, func: Callable[..., _T]) -> Callable[..., _T]: ...

def freeze_time(
time_to_freeze: Optional[Union[_Freezable, Callable[..., _Freezable], Iterator[_Freezable]]] = ...,
tz_offset: Optional[float] = ...,
ignore: Optional[Sequence[str]] = ...,
tick: Optional[bool] = ...,
as_arg: Optional[bool] = ...,
auto_tick_seconds: Optional[float] = ...,
) -> _freeze_time: ...
7 changes: 4 additions & 3 deletions freezegun/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from typing import List, Optional


DEFAULT_IGNORE_LIST = [
Expand All @@ -15,7 +16,7 @@


class Settings:
def __init__(self, default_ignore_list=None):
def __init__(self, default_ignore_list: Optional[List[str]]=None) -> None:
self.default_ignore_list = default_ignore_list or DEFAULT_IGNORE_LIST[:]


Expand All @@ -26,7 +27,7 @@ class ConfigurationError(Exception):
pass


def configure(default_ignore_list=None, extend_ignore_list=None):
def configure(default_ignore_list: Optional[List[str]]=None, extend_ignore_list: Optional[List[str]]=None) -> None:
if default_ignore_list is not None and extend_ignore_list is not None:
raise ConfigurationError("Either default_ignore_list or extend_ignore_list might be given, not both")
if default_ignore_list:
Expand All @@ -35,6 +36,6 @@ def configure(default_ignore_list=None, extend_ignore_list=None):
settings.default_ignore_list = [*settings.default_ignore_list, *extend_ignore_list]


def reset_config():
def reset_config() -> None:
global settings
settings = Settings()
Empty file added freezegun/py.typed
Empty file.
7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
[tool.mypy]
strict = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
# and then run "tox" from this directory.

[tox]
envlist = py35, py36, py37, py38, pypy3
envlist = py35, py36, py37, py38, pypy3, py{38}-mypy

[testenv]
commands = pytest --cov {posargs}
deps = -rrequirements.txt

[testenv:py38-mypy]
deps =
mypy == 0.902
commands = mypy freezegun