diff --git a/mypy.ini b/mypy.ini index 6d88873..3c5b18f 100644 --- a/mypy.ini +++ b/mypy.ini @@ -8,14 +8,20 @@ plugins = [mypy.plugins.django-stubs] django_settings_module = "testfixtures.tests.test_django.settings" -# Things that need to be resolved before adding a py.typed: -[mypy-testfixtures.datetime] -disable_error_code = no-untyped-def - # "nice to have" stuff to fix: [mypy-testfixtures.tests.*] disable_error_code = no-untyped-call,no-untyped-def +# Be more picky with mock_(date|time) tests: +[mypy-testfixtures.tests.test_date] +enable_error_code = no-untyped-call,no-untyped-def + +[mypy-testfixtures.tests.test_datetime] +enable_error_code = no-untyped-call,no-untyped-def + +[mypy-testfixtures.tests.test_time] +enable_error_code = no-untyped-call,no-untyped-def + # permanent exclusions and workaround: [mypy-constantly.*] ignore_missing_imports = True diff --git a/testfixtures/datetime.py b/testfixtures/datetime.py index 1ab0eba..4d08fc9 100644 --- a/testfixtures/datetime.py +++ b/testfixtures/datetime.py @@ -1,15 +1,70 @@ from calendar import timegm from datetime import datetime, timedelta, date, tzinfo as TZInfo -from typing import Callable, Tuple, Any, cast, overload +from typing import ( + Callable, + Self, + Tuple, + TypedDict, + overload, + TypeVar, + Generic, +) + +T = TypeVar('T', bound=datetime | date) + + +class DateTimeKwargs(TypedDict, total=False): + tzinfo: TZInfo | None + delta: float | None + delta_type: str + date_type: type[date] + strict: bool + + +class DateTimeArgs(TypedDict, total=False): + year: int + month: int + day: int + hour: int + minute: int + second: int + microsecond: int + + +class DateKwargs(TypedDict, total=False): + delta: float | None + delta_type: str + strict: bool + +class DateArgs(TypedDict, total=False): + year: int + month: int + day: int -class Queue(list): + +class TimeKwargs(TypedDict, total=False): + delta: float | None + delta_type: str + + +class TimeArgs(TypedDict, total=False): + year: int + month: int + day: int + hour: int + minute: int + second: int + microsecond: int + + +class Queue(list[T]): delta: float delta_delta: float delta_type: str - def __init__(self, delta: float | None, delta_delta: float, delta_type: str): + def __init__(self, delta: float | None, delta_delta: float, delta_type: str) -> None: super().__init__() if delta is None: self.delta = 0 @@ -20,34 +75,34 @@ def __init__(self, delta: float | None, delta_delta: float, delta_type: str): self.delta_type = delta_type def advance_next(self, delta: timedelta) -> None: - self[-1] += delta + self[-1] += delta # type: ignore[assignment] - def next(self) -> 'MockedCurrent': + def next(self) -> T: instance = self.pop(0) if not self: self.delta += self.delta_delta n = instance + timedelta(**{self.delta_type: self.delta}) - self.append(n) + self.append(n) # type: ignore[arg-type] return instance -class MockedCurrent: +class MockedCurrent(Generic[T]): - _mock_queue: Queue + _mock_queue: Queue[T] _mock_base_class: type _mock_class: type _mock_tzinfo: TZInfo | None - _mock_date_type: type[date] | None - _correct_mock_type: Callable | None = None + _mock_date_type: type[date] + _correct_mock_type: Callable[[T], Self] | None = None def __init_subclass__( cls, concrete: bool = False, - queue: Queue | None = None, + queue: Queue[T] | None = None, strict: bool | None = None, tzinfo: TZInfo | None = None, - date_type: type[date] | None = None - ): + date_type: type[date] = date, + ) -> None: if concrete: assert not queue is None, 'queue must be passed if concrete=True' cls._mock_queue = queue @@ -57,7 +112,7 @@ def __init_subclass__( cls._mock_date_type = date_type @classmethod - def add(cls, *args, **kw): + def add(cls, *args: int | T, **kw: int | TZInfo | None) -> None: if 'tzinfo' in kw or len(args) > 7: raise TypeError('Cannot add using tzinfo on %s' % cls.__name__) if args and isinstance(args[0], cls._mock_base_class): @@ -69,53 +124,53 @@ def add(cls, *args, **kw): 'Cannot add %s with tzinfo of %s as configured to use %s' % ( instance.__class__.__name__, instance_tzinfo, cls._mock_tzinfo )) - instance = instance.replace(tzinfo=None) + instance = instance.replace(tzinfo=None) # type: ignore[attr-defined] if cls._correct_mock_type: instance = cls._correct_mock_type(instance) else: - instance = cls(*args, **kw) + instance = cls(*args, **kw) # type: ignore[arg-type] cls._mock_queue.append(instance) @classmethod - def set(cls, *args, **kw) -> None: + def set(cls, *args: int | T, **kw: int | TZInfo | None) -> None: cls._mock_queue.clear() cls.add(*args, **kw) @classmethod - def tick(cls, *args, **kw) -> None: + def tick(cls, *args: timedelta, **kw: float) -> None: if kw: delta = timedelta(**kw) else: delta, = args cls._mock_queue.advance_next(delta) - def __add__(self, other): - instance = super().__add__(other) + def __add__(self, other: timedelta) -> Self: + instance = super().__add__(other) # type: ignore[misc] if self._correct_mock_type: instance = self._correct_mock_type(instance) return instance - def __new__(cls, *args, **kw): + def __new__(cls, *args: int, **kw: int | TZInfo | None) -> Self: if cls is cls._mock_class: - return super().__new__(cls, *args, **kw) + return super().__new__(cls, *args, **kw) # type: ignore[misc] else: - return cls._mock_class(*args, **kw) + return cls._mock_class(*args, **kw) # type: ignore[misc] def mock_factory( type_name: str, - mock_class: type[MockedCurrent], + mock_class: type[MockedCurrent[T]], default: Tuple[int, ...], - args: tuple, - kw: dict[str, Any], + args: tuple[int | T | None | TZInfo, ...], + kw: dict[str, int | TZInfo | None], delta: float | None, delta_type: str, delta_delta: float = 1, date_type: type[date] | None = None, tzinfo: TZInfo | None = None, strict: bool = False -): - cls = cast(type[MockedCurrent], type( +) -> type[MockedCurrent[T]]: + cls = type( type_name, (mock_class,), {}, @@ -124,123 +179,123 @@ def mock_factory( strict=strict, tzinfo=tzinfo, date_type=date_type, - )) + ) if args != (None,): if not (args or kw): args = default - cls.add(*args, **kw) + cls.add(*args, **kw) # type: ignore[attr-defined] return cls -class MockDateTime(MockedCurrent, datetime): - - @overload - @classmethod - def add( - cls, - year: int, - month: int, - day: int, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: TZInfo = ..., - ) -> None: - ... - - @overload - @classmethod - def add( - cls, - instance: datetime, - ) -> None: - ... - - @classmethod - def add(cls, *args, **kw): - """ - This will add the :class:`datetime.datetime` created from the - supplied parameters to the queue of datetimes to be returned by - :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`. An instance - of :class:`~datetime.datetime` may also be passed as a single - positional argument. - """ - return super().add(*args, **kw) - - @overload - @classmethod - def set( - cls, - year: int, - month: int, - day: int, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: TZInfo = ..., - ) -> None: - ... - - @overload - @classmethod - def set( - cls, - instance: datetime, - ) -> None: - ... - - @classmethod - def set(cls, *args, **kw): - """ - This will set the :class:`datetime.datetime` created from the - supplied parameters as the next datetime to be returned by - :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`, clearing out - any datetimes in the queue. An instance - of :class:`~datetime.datetime` may also be passed as a single - positional argument. - """ - return super().set(*args, **kw) - - @overload - @classmethod - def tick( - cls, - days: float = ..., - seconds: float = ..., - microseconds: float = ..., - milliseconds: float = ..., - minutes: float = ..., - hours: float = ..., - weeks: float = ..., - ) -> None: - ... - - @overload - @classmethod - def tick( - cls, - delta: timedelta, # can become positional-only when Python 3.8 minimum - ) -> None: - ... - - @classmethod - def tick(cls, *args, **kw) -> None: - """ - This method should be called either with a :class:`~datetime.timedelta` - as a positional argument, or with keyword parameters that will be used - to construct a :class:`~datetime.timedelta`. - - The :class:`~datetime.timedelta` will be used to advance the next datetime - to be returned by :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`. - """ - return super().tick(*args, **kw) - - @classmethod - def _correct_mock_type(cls, instance): +class MockDateTime(MockedCurrent[datetime], datetime): +# +# @overload +# @classmethod +# def add( +# cls, +# year: int, +# month: int, +# day: int, +# hour: int = ..., +# minute: int = ..., +# second: int = ..., +# microsecond: int = ..., +# tzinfo: TZInfo = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def add( +# cls, +# instance: datetime, +# ) -> None: +# ... +# +# @classmethod +# def add(cls, *args, **kw): +# """ +# This will add the :class:`datetime.datetime` created from the +# supplied parameters to the queue of datetimes to be returned by +# :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`. An instance +# of :class:`~datetime.datetime` may also be passed as a single +# positional argument. +# """ +# return super().add(*args, **kw) +# +# @overload +# @classmethod +# def set( +# cls, +# year: int, +# month: int, +# day: int, +# hour: int = ..., +# minute: int = ..., +# second: int = ..., +# microsecond: int = ..., +# tzinfo: TZInfo = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def set( +# cls, +# instance: datetime, +# ) -> None: +# ... +# +# @classmethod +# def set(cls, *args, **kw): +# """ +# This will set the :class:`datetime.datetime` created from the +# supplied parameters as the next datetime to be returned by +# :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`, clearing out +# any datetimes in the queue. An instance +# of :class:`~datetime.datetime` may also be passed as a single +# positional argument. +# """ +# return super().set(*args, **kw) +# +# @overload +# @classmethod +# def tick( +# cls, +# days: float = ..., +# seconds: float = ..., +# microseconds: float = ..., +# milliseconds: float = ..., +# minutes: float = ..., +# hours: float = ..., +# weeks: float = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def tick( +# cls, +# delta: timedelta, # can become positional-only when Python 3.8 minimum +# ) -> None: +# ... +# +# @classmethod +# def tick(cls, *args, **kw) -> None: +# """ +# This method should be called either with a :class:`~datetime.timedelta` +# as a positional argument, or with keyword parameters that will be used +# to construct a :class:`~datetime.timedelta`. +# +# The :class:`~datetime.timedelta` will be used to advance the next datetime +# to be returned by :meth:`~MockDateTime.now` or :meth:`~MockDateTime.utcnow`. +# """ +# return super().tick(*args, **kw) + + @classmethod + def _correct_mock_type(cls, instance: datetime) -> Self: return cls._mock_class( instance.year, instance.month, @@ -252,17 +307,18 @@ def _correct_mock_type(cls, instance): instance.tzinfo, ) + @classmethod - def _adjust_instance_using_tzinfo(cls, instance: datetime) -> datetime: + def _adjust_instance_using_tzinfo(cls, instance: datetime) -> Self: if cls._mock_tzinfo: offset = cls._mock_tzinfo.utcoffset(instance) if offset is None: raise TypeError('tzinfo with .utcoffset() returning None is not supported') instance = instance - offset - return instance + return instance # type: ignore[return-value] @classmethod - def now(cls, tz: TZInfo | None = None) -> datetime: # type: ignore[override] + def now(cls, tz: TZInfo | None = None) -> Self: """ :param tz: An optional timezone to apply to the returned time. If supplied, it must be an instance of a @@ -273,24 +329,22 @@ def now(cls, tz: TZInfo | None = None) -> datetime: # type: ignore[override] If `tz` is supplied, see :ref:`timezones`. """ - instance = cast(datetime, cls._mock_queue.next()) + instance = cls._mock_queue.next() if tz is not None: instance = tz.fromutc(cls._adjust_instance_using_tzinfo(instance).replace(tzinfo=tz)) return cls._correct_mock_type(instance) @classmethod - def utcnow(cls) -> datetime: # type: ignore[override] + def utcnow(cls) -> Self: """ This will return the next supplied or calculated datetime from the internal queue, rather than the actual current UTC datetime. If you care about timezones, see :ref:`timezones`. """ - instance = cast(datetime, cls._mock_queue.next()) + instance = cls._mock_queue.next() return cls._adjust_instance_using_tzinfo(instance) - _mock_date_type: type[date] - def date(self) -> date: """ This will return the date component of the current mock instance, @@ -301,69 +355,69 @@ def date(self) -> date: self.month, self.day ) - - -@overload -def mock_datetime( - tzinfo: TZInfo | None = None, - delta: float | None = None, - delta_type: str = 'seconds', - date_type: type[date] = date, - strict: bool = False -) -> type[MockDateTime]: - ... - - -@overload -def mock_datetime( - year: int, - month: int, - day: int, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - tzinfo: TZInfo | None = None, - delta: float | None = None, - delta_type: str = 'seconds', - date_type: type[date] = date, - strict: bool = False -) -> type[MockDateTime]: - ... - - -@overload -def mock_datetime( - default: datetime, - tzinfo: TZInfo | None = None, - delta: float | None = None, - delta_type: str = 'seconds', - date_type: type[date] = date, - strict: bool = False -) -> type[MockDateTime]: - ... - - -@overload -def mock_datetime( - default: None, # explicit None positional - tzinfo: TZInfo | None = None, - delta: float | None = None, - delta_type: str = 'seconds', - date_type: type[date] = date, - strict: bool = False -) -> type[MockDateTime]: - ... - - +# +# +# @overload +# def mock_datetime( +# tzinfo: TZInfo | None = None, +# delta: float | None = None, +# delta_type: str = 'seconds', +# date_type: type[date] = date, +# strict: bool = False +# ) -> type[MockDateTime]: +# ... +# +# +# @overload +# def mock_datetime( +# year: int, +# month: int, +# day: int, +# hour: int = ..., +# minute: int = ..., +# second: int = ..., +# microsecond: int = ..., +# tzinfo: TZInfo | None = None, +# delta: float | None = None, +# delta_type: str = 'seconds', +# date_type: type[date] = date, +# strict: bool = False +# ) -> type[MockDateTime]: +# ... +# +# +# @overload +# def mock_datetime( +# default: datetime, +# tzinfo: TZInfo | None = None, +# delta: float | None = None, +# delta_type: str = 'seconds', +# date_type: type[date] = date, +# strict: bool = False +# ) -> type[MockDateTime]: +# ... +# +# +# @overload +# def mock_datetime( +# default: None, # explicit None positional +# tzinfo: TZInfo | None = None, +# delta: float | None = None, +# delta_type: str = 'seconds', +# date_type: type[date] = date, +# strict: bool = False +# ) -> type[MockDateTime]: +# ... +# +# def mock_datetime( - *args, + *args: int | datetime | None | TZInfo, tzinfo: TZInfo | None = None, delta: float | None = None, delta_type: str = 'seconds', date_type: type[date] = date, strict: bool = False, - **kw, + **kw: int | TZInfo | None, ) -> type[MockDateTime]: """ .. currentmodule:: testfixtures.datetime @@ -428,11 +482,11 @@ def mock_datetime( as well as being a subclass of :class:`~testfixtures.datetime.MockDateTime`. """ if len(args) > 7: - tzinfo = args[7] + tzinfo = args[7] # type: ignore[assignment] args = args[:7] else: tzinfo = tzinfo or (getattr(args[0], 'tzinfo', None) if args else None) - return cast(type[MockDateTime], mock_factory( + return mock_factory( 'MockDateTime', MockDateTime, (2001, 1, 1, 0, 0, 0), @@ -444,165 +498,166 @@ def mock_datetime( delta_type=delta_type, date_type=date_type, strict=strict, - )) + ) # type: ignore[return-value] -class MockDate(MockedCurrent, date): +class MockDate(MockedCurrent[date], date): @classmethod - def _correct_mock_type(cls, instance): + def _correct_mock_type(cls, instance: date) -> Self: return cls._mock_class( instance.year, instance.month, instance.day, ) - @overload - @classmethod - def add( - cls, - year: int, - month: int, - day: int, - ) -> None: - ... - - @overload - @classmethod - def add( - cls, - instance: date, - ) -> None: - ... - - @classmethod - def add(cls, *args, **kw): - """ - This will add the :class:`datetime.date` created from the - supplied parameters to the queue of dates to be returned by - :meth:`~MockDate.today`. An instance - of :class:`~datetime.date` may also be passed as a single - positional argument. - """ - return super().add(*args, **kw) - - @overload - @classmethod - def set( - cls, - year: int, - month: int, - day: int, - ) -> None: - ... - - @overload - @classmethod - def set( - cls, - instance: date, - ) -> None: - ... - - @classmethod - def set(cls, *args, **kw) -> None: - """ - This will set the :class:`datetime.date` created from the - supplied parameters as the next date to be returned by - :meth:`~MockDate.today`, regardless of any dates in the - queue. An instance - of :class:`~datetime.date` may also be passed as a single - positional argument. - """ - return super().set(*args, **kw) - - @overload - @classmethod - def tick( - cls, - days: float = ..., - weeks: float = ..., - ) -> None: - ... - - @overload - @classmethod - def tick( - cls, - delta: timedelta, # can become positional-only when Python 3.8 minimum - ) -> None: - ... - - @classmethod - def tick(cls, *args, **kw) -> None: - """ - This method should be called either with a :class:`~datetime.timedelta` - as a positional argument, or with keyword parameters that will be used - to construct a :class:`~datetime.timedelta`. - - The :class:`~datetime.timedelta` will be used to advance the next date - to be returned by :meth:`~MockDate.today`. - """ - return super().tick(*args, **kw) - @classmethod - def today(cls) -> date: # type: ignore[override] +# @overload +# @classmethod +# def add( +# cls, +# year: int, +# month: int, +# day: int, +# ) -> None: +# ... +# +# @overload +# @classmethod +# def add( +# cls, +# instance: date, +# ) -> None: +# ... +# +# @classmethod +# def add(cls, *args, **kw): +# """ +# This will add the :class:`datetime.date` created from the +# supplied parameters to the queue of dates to be returned by +# :meth:`~MockDate.today`. An instance +# of :class:`~datetime.date` may also be passed as a single +# positional argument. +# """ +# return super().add(*args, **kw) +# +# @overload +# @classmethod +# def set( +# cls, +# year: int, +# month: int, +# day: int, +# ) -> None: +# ... +# +# @overload +# @classmethod +# def set( +# cls, +# instance: date, +# ) -> None: +# ... +# +# @classmethod +# def set(cls, *args, **kw) -> None: +# """ +# This will set the :class:`datetime.date` created from the +# supplied parameters as the next date to be returned by +# :meth:`~MockDate.today`, regardless of any dates in the +# queue. An instance +# of :class:`~datetime.date` may also be passed as a single +# positional argument. +# """ +# return super().set(*args, **kw) +# +# @overload +# @classmethod +# def tick( +# cls, +# days: float = ..., +# weeks: float = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def tick( +# cls, +# delta: timedelta, # can become positional-only when Python 3.8 minimum +# ) -> None: +# ... +# +# @classmethod +# def tick(cls, *args, **kw) -> None: +# """ +# This method should be called either with a :class:`~datetime.timedelta` +# as a positional argument, or with keyword parameters that will be used +# to construct a :class:`~datetime.timedelta`. +# +# The :class:`~datetime.timedelta` will be used to advance the next date +# to be returned by :meth:`~MockDate.today`. +# """ +# return super().tick(*args, **kw) + + @classmethod + def today(cls) -> Self: """ This will return the next supplied or calculated date from the internal queue, rather than the actual current date. """ - return cast(date, cls._mock_queue.next()) - - -@overload -def mock_date( - delta: float | None = None, - delta_type: str = 'days', - date_type: type[date] = date, - strict: bool = False -) -> type[MockDate]: - ... - - -@overload -def mock_date( - year: int, - month: int, - day: int, - delta: float | None = None, - delta_type: str = 'days', - strict: bool = False, -) -> type[MockDate]: - ... - - -@overload -def mock_date( - default: date, - delta: float | None = None, - delta_type: str = 'days', - strict: bool = False, -) -> type[MockDate]: - ... - - -@overload -def mock_date( - default: None, # explicit None positional - delta: float | None = None, - delta_type: str = 'days', - strict: bool = False, -) -> type[MockDate]: - ... - - + return cls._mock_queue.next() # type: ignore[return-value] + + +# @overload +# def mock_date( +# delta: float | None = None, +# delta_type: str = 'days', +# date_type: type[date] = date, +# strict: bool = False +# ) -> type[MockDate]: +# ... +# +# +# @overload +# def mock_date( +# year: int, +# month: int, +# day: int, +# delta: float | None = None, +# delta_type: str = 'days', +# strict: bool = False, +# ) -> type[MockDate]: +# ... +# +# +# @overload +# def mock_date( +# default: date, +# delta: float | None = None, +# delta_type: str = 'days', +# strict: bool = False, +# ) -> type[MockDate]: +# ... +# +# +# @overload +# def mock_date( +# default: None, # explicit None positional +# delta: float | None = None, +# delta_type: str = 'days', +# strict: bool = False, +# ) -> type[MockDate]: +# ... +# +# def mock_date( - *args, + *args: int | date | None, delta: float | None = None, delta_type: str = 'days', strict: bool = False, - **kw + **kw: int ) -> type[MockDate]: """ .. currentmodule:: testfixtures.datetime @@ -645,177 +700,183 @@ def mock_date( The mock returned will behave exactly as the :class:`datetime.date` class as well as being a subclass of :class:`~testfixtures.datetime.MockDate`. """ - return cast(type[MockDate], mock_factory( - 'MockDate', MockDate, (2001, 1, 1), args, kw, + return mock_factory( + 'MockDate', MockDate, (2001, 1, 1), args, kw, # type: ignore[arg-type] delta=delta, delta_type=delta_type, strict=strict, - )) + ) # type: ignore[return-value] ms = 10**6 -class MockTime(MockedCurrent, datetime): - - @overload - @classmethod - def add( - cls, - year: int, - month: int, - day: int, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - ) -> None: - ... - - @overload - @classmethod - def add( - cls, - instance: datetime, - ) -> None: - ... - - @classmethod - def add(cls, *args, **kw): - """ - This will add the time specified by the supplied parameters to the - queue of times to be returned by calls to the mock. The - parameters are the same as the :class:`datetime.datetime` - constructor. An instance of :class:`~datetime.datetime` may also - be passed as a single positional argument. - """ - return super().add(*args, **kw) - - @overload - @classmethod - def set( - cls, - year: int, - month: int, - day: int, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., - ) -> None: - ... - - @overload - @classmethod - def set( - cls, - instance: datetime, - ) -> None: - ... - - @classmethod - def set(cls, *args, **kw): - """ - This will set the time specified by the supplied parameters as - the next time to be returned by a call to the mock, regardless of - any times in the queue. The parameters are the same as the - :class:`datetime.datetime` constructor. An instance of - :class:`~datetime.datetime` may also be passed as a single - positional argument. - """ - return super().set(*args, **kw) - - @overload - @classmethod - def tick( - cls, - days: float = ..., - seconds: float = ..., - microseconds: float = ..., - milliseconds: float = ..., - minutes: float = ..., - hours: float = ..., - weeks: float = ..., - ) -> None: - ... - - @overload - @classmethod - def tick( - cls, - delta: timedelta, # can become positional-only when Python 3.8 minimum - ) -> None: - ... - - @classmethod - def tick(cls, *args, **kw): - """ - This method should be called either with a :class:`~datetime.timedelta` - as a positional argument, or with keyword parameters that will be used - to construct a :class:`~datetime.timedelta`. - - The :class:`~datetime.timedelta` will be used to advance the next time - to be returned by a call to the mock. - """ - return super().tick(*args, **kw) - - def __new__(cls, *args, **kw) -> float: # type: ignore[misc] +class MockTime(MockedCurrent[datetime], datetime): + +# @overload +# @classmethod +# def add( +# cls, +# year: int, +# month: int, +# day: int, +# hour: int = ..., +# minute: int = ..., +# second: int = ..., +# microsecond: int = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def add( +# cls, +# instance: datetime, +# ) -> None: +# ... +# +# @classmethod +# def add(cls, *args, **kw): +# """ +# This will add the time specified by the supplied parameters to the +# queue of times to be returned by calls to the mock. The +# parameters are the same as the :class:`datetime.datetime` +# constructor. An instance of :class:`~datetime.datetime` may also +# be passed as a single positional argument. +# """ +# return super().add(*args, **kw) +# +# @overload +# @classmethod +# def set( +# cls, +# year: int, +# month: int, +# day: int, +# hour: int = ..., +# minute: int = ..., +# second: int = ..., +# microsecond: int = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def set( +# cls, +# instance: datetime, +# ) -> None: +# ... +# +# @classmethod +# def set(cls, *args, **kw): +# """ +# This will set the time specified by the supplied parameters as +# the next time to be returned by a call to the mock, regardless of +# any times in the queue. The parameters are the same as the +# :class:`datetime.datetime` constructor. An instance of +# :class:`~datetime.datetime` may also be passed as a single +# positional argument. +# """ +# return super().set(*args, **kw) +# +# @overload +# @classmethod +# def tick( +# cls, +# days: float = ..., +# seconds: float = ..., +# microseconds: float = ..., +# milliseconds: float = ..., +# minutes: float = ..., +# hours: float = ..., +# weeks: float = ..., +# ) -> None: +# ... +# +# @overload +# @classmethod +# def tick( +# cls, +# delta: timedelta, # can become positional-only when Python 3.8 minimum +# ) -> None: +# ... +# +# @classmethod +# def tick(cls, *args, **kw): +# """ +# This method should be called either with a :class:`~datetime.timedelta` +# as a positional argument, or with keyword parameters that will be used +# to construct a :class:`~datetime.timedelta`. +# +# The :class:`~datetime.timedelta` will be used to advance the next time +# to be returned by a call to the mock. +# """ +# return super().tick(*args, **kw) +# + def __new__(cls, *args: int, **kw: int) -> Self | float: # type: ignore[misc] """ Return a :class:`float` representing the mocked current time as would normally be returned by :func:`time.time`. """ if args or kw: # Used when adding stuff to the queue - return super().__new__(cls, *args, **kw) + return super().__new__(cls, *args, **kw) # type: ignore[misc] else: - instance = cast(datetime, cls._mock_queue.next()) + instance = cls._mock_queue.next() time: float = timegm(instance.utctimetuple()) - time += (float(instance.microsecond)/ms) + time += (float(instance.microsecond) / ms) return time - -@overload -def mock_time( - delta: float | None = None, - delta_type: str = 'seconds', -) -> type[MockTime]: - ... - - -@overload +# +# +# @overload +# def mock_time( +# delta: float | None = None, +# delta_type: str = 'seconds', +# ) -> type[MockTime]: +# ... +# +# +# @overload +# def mock_time( +# year: int, +# month: int, +# day: int, +# hour: int = ..., +# minute: int = ..., +# second: int = ..., +# microsecond: int = ..., +# delta: float | None = None, +# delta_type: str = 'seconds', +# ) -> type[MockTime]: +# ... +# +# +# @overload +# def mock_time( +# default: datetime, +# delta: float | None = None, +# delta_type: str = 'seconds', +# ) -> type[MockTime]: +# ... +# +# +# @overload +# def mock_time( +# default: None, # explicit None positional +# delta: float | None = None, +# delta_type: str = 'seconds', +# ) -> type[MockTime]: +# ... +# +# def mock_time( - year: int, - month: int, - day: int, - hour: int = ..., - minute: int = ..., - second: int = ..., - microsecond: int = ..., + *args: int | datetime | None, delta: float | None = None, delta_type: str = 'seconds', + **kw: int, ) -> type[MockTime]: - ... - - -@overload -def mock_time( - default: datetime, - delta: float | None = None, - delta_type: str = 'seconds', -) -> type[MockTime]: - ... - - -@overload -def mock_time( - default: None, # explicit None positional - delta: float | None = None, - delta_type: str = 'seconds', -) -> type[MockTime]: - ... - - -def mock_time(*args, delta: float | None = None, delta_type: str = 'seconds', **kw) -> type[MockTime]: """ .. currentmodule:: testfixtures.datetime @@ -859,8 +920,8 @@ def mock_time(*args, delta: float | None = None, delta_type: str = 'seconds', ** """ if 'tzinfo' in kw or len(args) > 7 or (args and getattr(args[0], 'tzinfo', None)): raise TypeError("You don't want to use tzinfo with test_time") - return cast(type[MockTime], mock_factory( - 'MockTime', MockTime, (2001, 1, 1, 0, 0, 0), args, kw, + return mock_factory( + 'MockTime', MockTime, (2001, 1, 1, 0, 0, 0), args, kw, # type: ignore[arg-type] delta=delta, delta_type=delta_type, - )) + ) # type: ignore[return-value] diff --git a/testfixtures/tests/test_date.py b/testfixtures/tests/test_date.py index 8ef19b0..061ccdf 100644 --- a/testfixtures/tests/test_date.py +++ b/testfixtures/tests/test_date.py @@ -15,24 +15,24 @@ class TestDate(TestCase): # greatfully received! @replace('datetime.date', mock_date()) - def test_today(self): + def test_today(self) -> None: from datetime import date compare(date.today(), d(2001, 1, 1)) compare(date.today(), d(2001, 1, 2)) compare(date.today(), d(2001, 1, 4)) @replace('datetime.date', mock_date(2001, 2, 3)) - def test_today_supplied(self): + def test_today_supplied(self) -> None: from datetime import date compare(date.today(), d(2001, 2, 3)) @replace('datetime.date', mock_date(year=2001, month=2, day=3)) - def test_today_all_kw(self): + def test_today_all_kw(self) -> None: from datetime import date compare(date.today(), d(2001, 2, 3)) @replace('datetime.date', mock_date(None)) - def test_today_sequence(self, t: type[MockDate]): + def test_today_sequence(self, t: type[MockDate]) -> None: t.add(2002, 1, 1) t.add(2002, 1, 2) t.add(2002, 1, 3) @@ -42,7 +42,7 @@ def test_today_sequence(self, t: type[MockDate]): compare(date.today(), d(2002, 1, 3)) @replace('datetime.date', mock_date(None)) - def test_today_requested_longer_than_supplied(self, t: type[MockDate]): + def test_today_requested_longer_than_supplied(self, t: type[MockDate]) -> None: t.add(2002, 1, 1) t.add(2002, 1, 2) from datetime import date @@ -52,34 +52,34 @@ def test_today_requested_longer_than_supplied(self, t: type[MockDate]): compare(date.today(), d(2002, 1, 5)) @replace('datetime.date', mock_date(None)) - def test_add_date_supplied(self): + def test_add_date_supplied(self) -> None: from datetime import date - date = cast(type[MockDate], date) - date.add(d(2001, 1, 2)) - date.add(date(2001, 1, 3)) + date_mock = cast(type[MockDate], date) + date_mock.add(d(2001, 1, 2)) + date_mock.add(date(2001, 1, 3)) compare(date.today(), d(2001, 1, 2)) compare(date.today(), d(2001, 1, 3)) - def test_instantiate_with_date(self): + def test_instantiate_with_date(self) -> None: from datetime import date t = mock_date(date(2002, 1, 1)) compare(t.today(), d(2002, 1, 1)) @replace('datetime.date', mock_date(strict=True)) - def test_call(self, t: type[MockDate]): + def test_call(self, t: type[MockDate]) -> None: compare(t(2002, 1, 2), d(2002, 1, 2)) from datetime import date dt = date(2003, 2, 1) self.assertFalse(dt.__class__ is d) compare(dt, d(2003, 2, 1)) - def test_gotcha_import(self): + def test_gotcha_import(self) -> None: # standard `replace` caveat, make sure you - # patch all revelent places where date + # patch all relevant places where date # has been imported: @replace('datetime.date', mock_date()) - def test_something(): + def test_something() -> None: from datetime import date compare(date.today(), d(2001, 1, 1)) compare(sample1.str_today_1(), '2001-01-02') @@ -89,26 +89,27 @@ def test_something(): # This convoluted check is because we can't stub # out the date, since we're testing stubbing out # the date ;-) + assert s.raised is not None j, dt1, j, dt2, j = s.raised.args[0].split("'") # check we can parse the date - dt1 = strptime(dt1, '%Y-%m-%d') + strptime(dt1, '%Y-%m-%d') # check the dt2 bit was as it should be compare(dt2, '2001-01-02') # What you need to do is replace the imported type: @replace('testfixtures.tests.sample1.date', mock_date()) - def test_something(): + def test_something_fixed() -> None: compare(sample1.str_today_1(), '2001-01-01') - test_something() + test_something_fixed() - def test_gotcha_import_and_obtain(self): + def test_gotcha_import_and_obtain(self) -> None: # Another gotcha is where people have locally obtained # a class attributes, where the normal patching doesn't # work: @replace('testfixtures.tests.sample1.date', mock_date()) - def test_something(): + def test_something() -> None: compare(sample1.str_today_2(), '2001-01-01') with ShouldRaise(AssertionError) as s: @@ -116,6 +117,7 @@ def test_something(): # This convoluted check is because we can't stub # out the date, since we're testing stubbing out # the date ;-) + assert s.raised is not None j, dt1, j, dt2, j = s.raised.args[0].split("'") # check we can parse the date dt1 = strptime(dt1, '%Y-%m-%d') @@ -124,15 +126,15 @@ def test_something(): # What you need to do is replace the imported name: @replace('testfixtures.tests.sample1.today', mock_date().today) - def test_something(): + def test_something_fixed() -> None: compare(sample1.str_today_2(), '2001-01-01') - test_something() + test_something_fixed() # if you have an embedded `today` as above, *and* you need to supply # a list of required dates, then it's often simplest just to # do a manual try-finally with a replacer: - def test_import_and_obtain_with_lists(self): + def test_import_and_obtain_with_lists(self) -> None: t = mock_date(None) t.add(2002, 1, 1) @@ -148,166 +150,166 @@ def test_import_and_obtain_with_lists(self): r.restore() @replace('datetime.date', mock_date()) - def test_repr(self): + def test_repr(self) -> None: from datetime import date compare(repr(date), "") @replace('datetime.date', mock_date(delta=2)) - def test_delta(self): + def test_delta(self) -> None: from datetime import date compare(date.today(), d(2001, 1, 1)) compare(date.today(), d(2001, 1, 3)) compare(date.today(), d(2001, 1, 5)) @replace('datetime.date', mock_date(delta_type='weeks')) - def test_delta_type(self): + def test_delta_type(self) -> None: from datetime import date compare(date.today(), d(2001, 1, 1)) compare(date.today(), d(2001, 1, 8)) compare(date.today(), d(2001, 1, 22)) @replace('datetime.date', mock_date(None)) - def test_set(self): + def test_set(self) -> None: from datetime import date - date = cast(type[MockDate], date) - date.set(2001, 1, 2) + date_mock = cast(type[MockDate], date) + date_mock.set(2001, 1, 2) compare(date.today(), d(2001, 1, 2)) - date.set(2002, 1, 1) + date_mock.set(2002, 1, 1) compare(date.today(), d(2002, 1, 1)) compare(date.today(), d(2002, 1, 3)) @replace('datetime.date', mock_date(None)) - def test_set_date_supplied(self): + def test_set_date_supplied(self) -> None: from datetime import date - date = cast(type[MockDate], date) - date.set(d(2001, 1, 2)) + date_mock = cast(type[MockDate], date) + date_mock.set(d(2001, 1, 2)) compare(date.today(), d(2001, 1, 2)) - date.set(date(2001, 1, 3)) + date_mock.set(date(2001, 1, 3)) compare(date.today(), d(2001, 1, 3)) @replace('datetime.date', mock_date(None)) - def test_set_kw(self): + def test_set_kw(self) -> None: from datetime import date - date = cast(type[MockDate], date) - date.set(year=2001, month=1, day=2) + date_mock = cast(type[MockDate], date) + date_mock.set(year=2001, month=1, day=2) compare(date.today(), d(2001, 1, 2)) @replace('datetime.date', mock_date(None)) - def test_add_kw(self, t: type[MockDate]): + def test_add_kw(self, t: type[MockDate]) -> None: t.add(year=2002, month=1, day=1) from datetime import date compare(date.today(), d(2002, 1, 1)) @replace('datetime.date', mock_date(strict=True)) - def test_isinstance_strict_true(self): + def test_isinstance_strict_true(self) -> None: from datetime import date - date = cast(type[MockDate], date) + date_mock = cast(type[MockDate], date) to_check = [] - to_check.append(date(1999, 1, 1)) - to_check.append(date.today()) - date.set(2001, 1, 2) - to_check.append(date.today()) - date.add(2001, 1, 3) - to_check.append(date.today()) - to_check.append(date.today()) - date.set(date(2001, 1, 4)) - to_check.append(date.today()) - date.add(date(2001, 1, 5)) - to_check.append(date.today()) - to_check.append(date.today()) - date.set(d(2001, 1, 4)) - to_check.append(date.today()) - date.add(d(2001, 1, 5)) - to_check.append(date.today()) - to_check.append(date.today()) + to_check.append(date_mock(1999, 1, 1)) + to_check.append(date_mock.today()) + date_mock.set(2001, 1, 2) + to_check.append(date_mock.today()) + date_mock.add(2001, 1, 3) + to_check.append(date_mock.today()) + to_check.append(date_mock.today()) + date_mock.set(date_mock(2001, 1, 4)) + to_check.append(date_mock.today()) + date_mock.add(date_mock(2001, 1, 5)) + to_check.append(date_mock.today()) + to_check.append(date_mock.today()) + date_mock.set(d(2001, 1, 4)) + to_check.append(date_mock.today()) + date_mock.add(d(2001, 1, 5)) + to_check.append(date_mock.today()) + to_check.append(date_mock.today()) for inst in to_check: - self.assertTrue(isinstance(inst, date), inst) - self.assertTrue(inst.__class__ is date, inst) + self.assertTrue(isinstance(inst, date_mock), inst) + self.assertTrue(inst.__class__ is date_mock, inst) self.assertTrue(isinstance(inst, d), inst) self.assertFalse(inst.__class__ is d, inst) - def test_strict_addition(self): + def test_strict_addition(self) -> None: mock_d = mock_date(strict=True) dt = mock_d(2001, 1, 1) + timedelta(days=1) assert type(dt) is mock_d - def test_non_strict_addition(self): + def test_non_strict_addition(self) -> None: from datetime import date mock_d = mock_date(strict=False) dt = mock_d(2001, 1, 1) + timedelta(days=1) assert type(dt) is date - def test_strict_add(self): + def test_strict_add(self) -> None: mock_d = mock_date(None, strict=True) mock_d.add(2001, 1, 1) assert type(mock_d.today()) is mock_d - def test_non_strict_add(self): + def test_non_strict_add(self) -> None: from datetime import date mock_d = mock_date(None, strict=False) mock_d.add(2001, 1, 1) assert type(mock_d.today()) is date @replace('datetime.date', mock_date()) - def test_isinstance_default(self): + def test_isinstance_default(self) -> None: from datetime import date - date = cast(type[MockDate], date) + date_mock = cast(type[MockDate], date) to_check = [] - to_check.append(date(1999, 1, 1)) - to_check.append(date.today()) - date.set(2001, 1, 2) - to_check.append(date.today()) - date.add(2001, 1, 3) - to_check.append(date.today()) - to_check.append(date.today()) - date.set(date(2001, 1, 4)) - to_check.append(date.today()) - date.add(date(2001, 1, 5)) - to_check.append(date.today()) - to_check.append(date.today()) - date.set(d(2001, 1, 4)) - to_check.append(date.today()) - date.add(d(2001, 1, 5)) - to_check.append(date.today()) - to_check.append(date.today()) + to_check.append(date_mock(1999, 1, 1)) + to_check.append(date_mock.today()) + date_mock.set(2001, 1, 2) + to_check.append(date_mock.today()) + date_mock.add(2001, 1, 3) + to_check.append(date_mock.today()) + to_check.append(date_mock.today()) + date_mock.set(date_mock(2001, 1, 4)) + to_check.append(date_mock.today()) + date_mock.add(date_mock(2001, 1, 5)) + to_check.append(date_mock.today()) + to_check.append(date_mock.today()) + date_mock.set(d(2001, 1, 4)) + to_check.append(date_mock.today()) + date_mock.add(d(2001, 1, 5)) + to_check.append(date_mock.today()) + to_check.append(date_mock.today()) for inst in to_check: - self.assertFalse(isinstance(inst, date), inst) - self.assertFalse(inst.__class__ is date, inst) + self.assertFalse(isinstance(inst, date_mock), inst) + self.assertFalse(inst.__class__ is date_mock, inst) self.assertTrue(isinstance(inst, d), inst) self.assertTrue(inst.__class__ is d, inst) - def test_tick_when_static(self): + def test_tick_when_static(self) -> None: date = mock_date(delta=0) compare(date.today(), expected=d(2001, 1, 1)) date.tick(days=1) compare(date.today(), expected=d(2001, 1, 2)) - def test_tick_when_dynamic(self): + def test_tick_when_dynamic(self) -> None: # hopefully not that common? date = mock_date() - compare(date.today(), expected=date(2001, 1, 1)) + compare(date.today(), expected=d(2001, 1, 1)) date.tick(days=1) - compare(date.today(), expected=date(2001, 1, 3)) + compare(date.today(), expected=d(2001, 1, 3)) - def test_tick_with_timedelta_instance(self): + def test_tick_with_timedelta_instance(self) -> None: date = mock_date(delta=0) compare(date.today(), expected=d(2001, 1, 1)) date.tick(timedelta(days=1)) compare(date.today(), expected=d(2001, 1, 2)) - def test_old_import(self): + def test_old_import(self) -> None: from testfixtures import test_date assert test_date is mock_date - def test_add_timedelta_not_strict(self): + def test_add_timedelta_not_strict(self) -> None: mock_class = mock_date() value = mock_class.today() + timedelta(days=1) assert isinstance(value, date) assert type(value) is date - def test_add_timedelta_strict(self): + def test_add_timedelta_strict(self) -> None: mock_class = mock_date(strict=True) value = mock_class.today() + timedelta(days=1) assert isinstance(value, date) diff --git a/testfixtures/tests/test_datetime.py b/testfixtures/tests/test_datetime.py index 9dd2d64..ca9674e 100644 --- a/testfixtures/tests/test_datetime.py +++ b/testfixtures/tests/test_datetime.py @@ -4,8 +4,14 @@ from datetime import tzinfo from typing import cast -from testfixtures import mock_datetime, mock_date -from testfixtures import replace, Replacer, compare, ShouldRaise +from testfixtures import ( + Replacer, + ShouldRaise, + compare, + mock_datetime, + mock_date, + replace, +) from testfixtures.datetime import MockDateTime from testfixtures.tests import sample1 from unittest import TestCase @@ -16,10 +22,10 @@ class SampleTZInfo(tzinfo): def tzname(self, dt: datetime | None) -> str: return "SAMPLE" - def utcoffset(self, dt): + def utcoffset(self, dt: datetime | None) -> timedelta | None: return timedelta(minutes=3) + self.dst(dt) - def dst(self, dt): + def dst(self, dt: datetime | None) -> timedelta: return timedelta(minutes=1) @@ -28,10 +34,10 @@ class SampleTZInfo2(tzinfo): def tzname(self, dt: datetime | None) -> str: return "SAMPLE2" - def utcoffset(self, dt): + def utcoffset(self, dt: datetime | None) -> timedelta | None: return timedelta(minutes=5) - def dst(self, dt): + def dst(self, dt: datetime | None) -> timedelta: return timedelta(minutes=0) @@ -40,14 +46,14 @@ class WeirdTZInfo(tzinfo): def tzname(self, dt: datetime | None) -> str: return "WEIRD" - def utcoffset(self, dt): + def utcoffset(self, dt: datetime | None) -> timedelta | None: return None - def dst(self, dt): + def dst(self, dt: datetime | None) -> timedelta | None: return None -def test_sample_tzinfos(): +def test_sample_tzinfos() -> None: compare(SampleTZInfo().tzname(None), expected='SAMPLE') compare(SampleTZInfo2().tzname(None), expected='SAMPLE2') compare(WeirdTZInfo().tzname(None), expected='WEIRD') @@ -58,62 +64,62 @@ def test_sample_tzinfos(): class TestDateTime(TestCase): @replace('datetime.datetime', mock_datetime()) - def test_now(self): + def test_now(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 1, 0, 0, 0)) compare(datetime.now(), d(2001, 1, 1, 0, 0, 10)) compare(datetime.now(), d(2001, 1, 1, 0, 0, 30)) @replace('datetime.datetime', mock_datetime()) - def test_now_with_tz_supplied(self): + def test_now_with_tz_supplied(self) -> None: from datetime import datetime info = SampleTZInfo() compare(datetime.now(info), d(2001, 1, 1, 0, 4, tzinfo=SampleTZInfo())) @replace('datetime.datetime', mock_datetime(tzinfo=SampleTZInfo())) - def test_now_with_tz_setup(self): + def test_now_with_tz_setup(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 1)) @replace('datetime.datetime', mock_datetime(tzinfo=WeirdTZInfo())) - def test_now_with_werid_tz_setup(self): + def test_now_with_werid_tz_setup(self) -> None: from datetime import datetime with ShouldRaise(TypeError('tzinfo with .utcoffset() returning None is not supported')): datetime.now(tz=SampleTZInfo()) @replace('datetime.datetime', mock_datetime(tzinfo=SampleTZInfo())) - def test_now_with_tz_setup_and_supplied(self): + def test_now_with_tz_setup_and_supplied(self) -> None: from datetime import datetime info = SampleTZInfo2() compare(datetime.now(info), d(2001, 1, 1, 0, 1, tzinfo=info)) @replace('datetime.datetime', mock_datetime(tzinfo=SampleTZInfo())) - def test_now_with_tz_setup_and_same_supplied(self): + def test_now_with_tz_setup_and_same_supplied(self) -> None: from datetime import datetime info = SampleTZInfo() compare(datetime.now(info), d(2001, 1, 1, tzinfo=info)) - def test_now_with_tz_instance(self): + def test_now_with_tz_instance(self) -> None: dt = mock_datetime(d(2001, 1, 1, tzinfo=SampleTZInfo())) compare(dt.now(), d(2001, 1, 1)) - def test_now_with_tz_instance_and_supplied(self): + def test_now_with_tz_instance_and_supplied(self) -> None: dt = mock_datetime(d(2001, 1, 1, tzinfo=SampleTZInfo())) info = SampleTZInfo2() compare(dt.now(info), d(2001, 1, 1, 0, 1, tzinfo=info)) - def test_now_with_tz_instance_and_same_supplied(self): + def test_now_with_tz_instance_and_same_supplied(self) -> None: dt = mock_datetime(d(2001, 1, 1, tzinfo=SampleTZInfo())) info = SampleTZInfo() compare(dt.now(info), d(2001, 1, 1, tzinfo=info)) @replace('datetime.datetime', mock_datetime(2002, 1, 1, 1, 2, 3)) - def test_now_supplied(self): + def test_now_supplied(self) -> None: from datetime import datetime compare(datetime.now(), d(2002, 1, 1, 1, 2, 3)) @replace('datetime.datetime', mock_datetime(None)) - def test_now_sequence(self, t): + def test_now_sequence(self, t: type[MockDateTime]) -> None: t.add(2002, 1, 1, 1, 0, 0) t.add(2002, 1, 1, 2, 0, 0) t.add(2002, 1, 1, 3, 0, 0) @@ -123,7 +129,7 @@ def test_now_sequence(self, t): compare(datetime.now(), d(2002, 1, 1, 3, 0, 0)) @replace('datetime.datetime', mock_datetime()) - def test_add_and_set(self, t): + def test_add_and_set(self, t: type[MockDateTime]) -> None: t.add(2002, 1, 1, 1, 0, 0) t.add(2002, 1, 1, 2, 0, 0) t.set(2002, 1, 1, 3, 0, 0) @@ -133,7 +139,7 @@ def test_add_and_set(self, t): compare(datetime.now(), d(2002, 1, 1, 3, 0, 30)) @replace('datetime.datetime', mock_datetime(None)) - def test_add_datetime_supplied(self, t: type[MockDateTime]): + def test_add_datetime_supplied(self, t: type[MockDateTime]) -> None: from datetime import datetime t.add(d(2002, 1, 1, 1)) t.add(datetime(2002, 1, 1, 2)) @@ -147,13 +153,13 @@ def test_add_datetime_supplied(self, t: type[MockDateTime]): ))): t.add(d(2001, 1, 1, tzinfo=tzinfo)) - def test_instantiate_with_datetime(self): + def test_instantiate_with_datetime(self) -> None: from datetime import datetime t = mock_datetime(datetime(2002, 1, 1, 1)) compare(t.now(), d(2002, 1, 1, 1, 0, 0)) @replace('datetime.datetime', mock_datetime(None)) - def test_now_requested_longer_than_supplied(self, t: type[MockDateTime]): + def test_now_requested_longer_than_supplied(self, t: type[MockDateTime]) -> None: t.add(2002, 1, 1, 1, 0, 0) t.add(2002, 1, 1, 2, 0, 0) from datetime import datetime @@ -163,14 +169,14 @@ def test_now_requested_longer_than_supplied(self, t: type[MockDateTime]): compare(datetime.now(), d(2002, 1, 1, 2, 0, 30)) @replace('datetime.datetime', mock_datetime(strict=True)) - def test_call(self, t: type[MockDateTime]): + def test_call(self, t: type[MockDateTime]) -> None: compare(t(2002, 1, 2, 3, 4, 5), d(2002, 1, 2, 3, 4, 5)) from datetime import datetime dt = datetime(2001, 1, 1, 1, 0, 0) self.assertFalse(dt.__class__ is d) compare(dt, d(2001, 1, 1, 1, 0, 0)) - def test_date_return_type(self): + def test_date_return_type(self) -> None: with Replacer() as r: r.replace('datetime.datetime', mock_datetime()) from datetime import datetime @@ -179,7 +185,7 @@ def test_date_return_type(self): compare(d, date(2001, 1, 1)) self.assertTrue(d.__class__ is date) - def test_date_return_type_picky(self): + def test_date_return_type_picky(self) -> None: # type checking is a bitch :-/ date_type = mock_date(strict=True) with Replacer() as r: @@ -195,7 +201,7 @@ def test_date_return_type_picky(self): # if you have an embedded `now` as above, *and* you need to supply # a list of required datetimes, then it's often simplest just to # do a manual try-finally with a replacer: - def test_import_and_obtain_with_lists(self): + def test_import_and_obtain_with_lists(self) -> None: t = mock_datetime(None) t.add(2002, 1, 1, 1, 0, 0) @@ -211,36 +217,36 @@ def test_import_and_obtain_with_lists(self): r.restore() @replace('datetime.datetime', mock_datetime()) - def test_repr(self): + def test_repr(self) -> None: from datetime import datetime compare(repr(datetime), "") @replace('datetime.datetime', mock_datetime(delta=1)) - def test_delta(self): + def test_delta(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 1, 0, 0, 0)) compare(datetime.now(), d(2001, 1, 1, 0, 0, 1)) compare(datetime.now(), d(2001, 1, 1, 0, 0, 2)) @replace('datetime.datetime', mock_datetime(delta_type='minutes')) - def test_delta_type(self): + def test_delta_type(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 1, 0, 0, 0)) compare(datetime.now(), d(2001, 1, 1, 0, 10, 0)) compare(datetime.now(), d(2001, 1, 1, 0, 30, 0)) @replace('datetime.datetime', mock_datetime(None)) - def test_set(self): + def test_set(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) - datetime.set(2001, 1, 1, 1, 0, 1) + dt_mock = cast(type[MockDateTime], datetime) + dt_mock.set(2001, 1, 1, 1, 0, 1) compare(datetime.now(), d(2001, 1, 1, 1, 0, 1)) - datetime.set(2002, 1, 1, 1, 0, 0) + dt_mock.set(2002, 1, 1, 1, 0, 0) compare(datetime.now(), d(2002, 1, 1, 1, 0, 0)) compare(datetime.now(), d(2002, 1, 1, 1, 0, 20)) @replace('datetime.datetime', mock_datetime(None)) - def test_set_datetime_supplied(self, t: type[MockDateTime]): + def test_set_datetime_supplied(self, t: type[MockDateTime]) -> None: from datetime import datetime t.set(d(2002, 1, 1, 1)) compare(datetime.now(), d(2002, 1, 1, 1, 0, 0)) @@ -255,57 +261,57 @@ def test_set_datetime_supplied(self, t: type[MockDateTime]): t.set(d(2001, 1, 1, tzinfo=tzinfo)) @replace('datetime.datetime', mock_datetime(None, tzinfo=SampleTZInfo())) - def test_set_tz_setup(self): + def test_set_tz_setup(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) - datetime.set(year=2002, month=1, day=1) + dt_mock = cast(type[MockDateTime], datetime) + dt_mock.set(year=2002, month=1, day=1) compare(datetime.now(), d(2002, 1, 1)) @replace('datetime.datetime', mock_datetime(None)) - def test_set_kw(self): + def test_set_kw(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) - datetime.set(year=2002, month=1, day=1) + dt_mock = cast(type[MockDateTime], datetime) + dt_mock.set(year=2002, month=1, day=1) compare(datetime.now(), d(2002, 1, 1)) @replace('datetime.datetime', mock_datetime(None)) - def test_set_tzinfo_kw(self): + def test_set_tzinfo_kw(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) + dt_mock = cast(type[MockDateTime], datetime) with ShouldRaise(TypeError('Cannot add using tzinfo on MockDateTime')): - datetime.set(year=2002, month=1, day=1, tzinfo=SampleTZInfo()) + dt_mock.set(year=2002, month=1, day=1, tzinfo=SampleTZInfo()) @replace('datetime.datetime', mock_datetime(None)) - def test_set_tzinfo_args(self): + def test_set_tzinfo_args(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) + dt_mock = cast(type[MockDateTime], datetime) with ShouldRaise(TypeError('Cannot add using tzinfo on MockDateTime')): - datetime.set(2002, 1, 2, 3, 4, 5, 6, SampleTZInfo()) + dt_mock.set(2002, 1, 2, 3, 4, 5, 6, SampleTZInfo()) # type: ignore[arg-type] @replace('datetime.datetime', mock_datetime(None)) - def test_add_kw(self, t: type[MockDateTime]): + def test_add_kw(self, t: type[MockDateTime]) -> None: from datetime import datetime t.add(year=2002, day=1, month=1) compare(datetime.now(), d(2002, 1, 1)) @replace('datetime.datetime', mock_datetime(None)) - def test_add_tzinfo_kw(self, t: type[MockDateTime]): + def test_add_tzinfo_kw(self, t: type[MockDateTime]) -> None: with ShouldRaise(TypeError('Cannot add using tzinfo on MockDateTime')): t.add(year=2002, month=1, day=1, tzinfo=SampleTZInfo()) @replace('datetime.datetime', mock_datetime(None)) - def test_add_tzinfo_args(self, t: type[MockDateTime]): + def test_add_tzinfo_args(self, t: type[MockDateTime]) -> None: with ShouldRaise(TypeError('Cannot add using tzinfo on MockDateTime')): - t.add(2002, 1, 2, 3, 4, 5, 6, SampleTZInfo()) + t.add(2002, 1, 2, 3, 4, 5, 6, SampleTZInfo()) # type: ignore[arg-type] @replace('datetime.datetime', mock_datetime(2001, 1, 2, 3, 4, 5, 6, SampleTZInfo())) - def test_max_number_args(self): + def test_max_number_args(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 2, 3, 4, 5, 6)) @replace('datetime.datetime', mock_datetime(2001, 1, 2)) - def test_min_number_args(self): + def test_min_number_args(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 2)) @@ -319,152 +325,152 @@ def test_min_number_args(self): microsecond=6, tzinfo=SampleTZInfo() )) - def test_all_kw(self): + def test_all_kw(self) -> None: from datetime import datetime compare(datetime.now(), d(2001, 1, 2, 3, 4, 5, 6)) @replace('datetime.datetime', mock_datetime(2001, 1, 2)) - def test_utc_now(self): + def test_utc_now(self) -> None: from datetime import datetime compare(datetime.utcnow(), d(2001, 1, 2)) @replace('datetime.datetime', mock_datetime(2001, 1, 2, tzinfo=SampleTZInfo())) - def test_utc_now_with_tz(self): + def test_utc_now_with_tz(self) -> None: from datetime import datetime compare(datetime.utcnow(), d(2001, 1, 1, 23, 56)) @replace('datetime.datetime', mock_datetime(strict=True)) - def test_isinstance_strict(self): + def test_isinstance_strict(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) + dt_mock = cast(type[MockDateTime], datetime) to_check = [] - to_check.append(datetime(1999, 1, 1)) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) - to_check.append(datetime.utcnow()) - datetime.set(2001, 1, 1, 20) - to_check.append(datetime.now()) - datetime.add(2001, 1, 1, 21) - to_check.append(datetime.now()) - to_check.append(datetime.now()) - datetime.set(datetime(2001, 1, 1, 22)) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) - datetime.add(datetime(2001, 1, 1, 23)) - to_check.append(datetime.now()) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) - datetime.set(d(2001, 1, 1, 22)) - to_check.append(datetime.now()) - datetime.add(d(2001, 1, 1, 23)) - to_check.append(datetime.now()) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) + to_check.append(dt_mock(1999, 1, 1)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) + to_check.append(dt_mock.utcnow()) + dt_mock.set(2001, 1, 1, 20) + to_check.append(dt_mock.now()) + dt_mock.add(2001, 1, 1, 21) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now()) + dt_mock.set(dt_mock(2001, 1, 1, 22)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) + dt_mock.add(dt_mock(2001, 1, 1, 23)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) + dt_mock.set(d(2001, 1, 1, 22)) + to_check.append(dt_mock.now()) + dt_mock.add(d(2001, 1, 1, 23)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) for inst in to_check: - self.assertTrue(isinstance(inst, datetime), inst) - self.assertTrue(inst.__class__ is datetime, inst) + self.assertTrue(isinstance(inst, dt_mock), inst) + self.assertTrue(inst.__class__ is dt_mock, inst) self.assertTrue(isinstance(inst, d), inst) self.assertFalse(inst.__class__ is d, inst) - def test_strict_addition(self): + def test_strict_addition(self) -> None: mock_dt = mock_datetime(strict=True) dt = mock_dt(2001, 1, 1) + timedelta(days=1) assert type(dt) is mock_dt - def test_non_strict_addition(self): + def test_non_strict_addition(self) -> None: from datetime import datetime mock_dt = mock_datetime(strict=False) dt = mock_dt(2001, 1, 1) + timedelta(days=1) assert type(dt) is datetime - def test_strict_add(self): + def test_strict_add(self) -> None: mock_dt = mock_datetime(None, strict=True) mock_dt.add(2001, 1, 1) assert type(mock_dt.now()) is mock_dt - def test_non_strict_add(self): + def test_non_strict_add(self) -> None: from datetime import datetime mock_dt = mock_datetime(None, strict=False) mock_dt.add(2001, 1, 1) assert type(mock_dt.now()) is datetime @replace('datetime.datetime', mock_datetime()) - def test_isinstance_default(self): + def test_isinstance_default(self) -> None: from datetime import datetime - datetime = cast(type[MockDateTime], datetime) + dt_mock = cast(type[MockDateTime], datetime) to_check = [] - to_check.append(datetime(1999, 1, 1)) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) - to_check.append(datetime.utcnow()) - datetime.set(2001, 1, 1, 20) - to_check.append(datetime.now()) - datetime.add(2001, 1, 1, 21) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) - datetime.set(datetime(2001, 1, 1, 22)) - to_check.append(datetime.now()) - datetime.add(datetime(2001, 1, 1, 23)) - to_check.append(datetime.now()) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) - datetime.set(d(2001, 1, 1, 22)) - to_check.append(datetime.now()) - datetime.add(d(2001, 1, 1, 23)) - to_check.append(datetime.now()) - to_check.append(datetime.now()) - to_check.append(datetime.now(SampleTZInfo())) + to_check.append(dt_mock(1999, 1, 1)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) + to_check.append(dt_mock.utcnow()) + dt_mock.set(2001, 1, 1, 20) + to_check.append(dt_mock.now()) + dt_mock.add(2001, 1, 1, 21) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) + dt_mock.set(dt_mock(2001, 1, 1, 22)) + to_check.append(dt_mock.now()) + dt_mock.add(dt_mock(2001, 1, 1, 23)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) + dt_mock.set(d(2001, 1, 1, 22)) + to_check.append(dt_mock.now()) + dt_mock.add(d(2001, 1, 1, 23)) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now()) + to_check.append(dt_mock.now(SampleTZInfo())) for inst in to_check: - self.assertFalse(isinstance(inst, datetime), inst) - self.assertFalse(inst.__class__ is datetime, inst) + self.assertFalse(isinstance(inst, dt_mock), inst) + self.assertFalse(inst.__class__ is dt_mock, inst) self.assertTrue(isinstance(inst, d), inst) self.assertTrue(inst.__class__ is d, inst) - def test_subsecond_deltas(self): - datetime = mock_datetime(delta=0.5) - compare(datetime.now(), datetime(2001, 1, 1, 0, 0, 0, 0)) - compare(datetime.now(), datetime(2001, 1, 1, 0, 0, 0, 500000)) - compare(datetime.now(), datetime(2001, 1, 1, 0, 0, 1, 0)) - - def test_ms_delta(self): - datetime = mock_datetime(delta=100, delta_type='microseconds') - compare(datetime.now(), datetime(2001, 1, 1, 0, 0, 0, 0)) - compare(datetime.now(), datetime(2001, 1, 1, 0, 0, 0, 100)) - compare(datetime.now(), datetime(2001, 1, 1, 0, 0, 0, 200)) - - def test_tick_when_static(self): - datetime = mock_datetime(delta=0) - compare(datetime.now(), expected=d(2001, 1, 1)) - datetime.tick(hours=1) - compare(datetime.now(), expected=d(2001, 1, 1, 1)) - - def test_tick_when_dynamic(self): + def test_subsecond_deltas(self) -> None: + mock_dt = mock_datetime(delta=0.5) + compare(mock_dt.now(), d(2001, 1, 1, 0, 0, 0, 0)) + compare(mock_dt.now(), d(2001, 1, 1, 0, 0, 0, 500000)) + compare(mock_dt.now(), d(2001, 1, 1, 0, 0, 1, 0)) + + def test_ms_delta(self) -> None: + mock_dt = mock_datetime(delta=100, delta_type='microseconds') + compare(mock_dt.now(), d(2001, 1, 1, 0, 0, 0, 0)) + compare(mock_dt.now(), d(2001, 1, 1, 0, 0, 0, 100)) + compare(mock_dt.now(), d(2001, 1, 1, 0, 0, 0, 200)) + + def test_tick_when_static(self) -> None: + mock_dt = mock_datetime(delta=0) + compare(mock_dt.now(), expected=d(2001, 1, 1)) + mock_dt.tick(hours=1) + compare(mock_dt.now(), expected=d(2001, 1, 1, 1)) + + def test_tick_when_dynamic(self) -> None: # hopefully not that common? - datetime = mock_datetime() - compare(datetime.now(), expected=d(2001, 1, 1)) - datetime.tick(hours=1) - compare(datetime.now(), expected=d(2001, 1, 1, 1, 0, 10)) - - def test_tick_with_timedelta_instance(self): - datetime = mock_datetime(delta=0) - compare(datetime.now(), expected=d(2001, 1, 1)) - datetime.tick(timedelta(hours=1)) - compare(datetime.now(), expected=d(2001, 1, 1, 1)) - - def test_old_import(self): + mock_dt = mock_datetime() + compare(mock_dt.now(), expected=d(2001, 1, 1)) + mock_dt.tick(hours=1) + compare(mock_dt.now(), expected=d(2001, 1, 1, 1, 0, 10)) + + def test_tick_with_timedelta_instance(self) -> None: + mock_dt = mock_datetime(delta=0) + compare(mock_dt.now(), expected=d(2001, 1, 1)) + mock_dt.tick(timedelta(hours=1)) + compare(mock_dt.now(), expected=d(2001, 1, 1, 1)) + + def test_old_import(self) -> None: from testfixtures import test_datetime assert test_datetime is mock_datetime - def test_add_timedelta_not_strict(self): + def test_add_timedelta_not_strict(self) -> None: mock_class = mock_datetime() value = mock_class.now() + timedelta(seconds=10) assert isinstance(value, datetime) assert type(value) is datetime - def test_add_timedelta_strict(self): + def test_add_timedelta_strict(self) -> None: mock_class = mock_datetime(strict=True) value = mock_class.now() + timedelta(seconds=10) assert isinstance(value, datetime) diff --git a/testfixtures/tests/test_time.py b/testfixtures/tests/test_time.py index 43889ba..810ed94 100644 --- a/testfixtures/tests/test_time.py +++ b/testfixtures/tests/test_time.py @@ -1,27 +1,28 @@ from datetime import timedelta from typing import cast from unittest import TestCase + from testfixtures import mock_time, replace, compare, ShouldRaise from .test_datetime import SampleTZInfo -from ..datetime import MockTime +from testfixtures.datetime import MockTime class TestTime(TestCase): @replace('time.time', mock_time()) - def test_time_call(self): + def test_time_call(self) -> None: from time import time compare(time(), 978307200.0) compare(time(), 978307201.0) compare(time(), 978307203.0) @replace('time.time', mock_time(2002, 1, 1, 1, 2, 3)) - def test_time_supplied(self): + def test_time_supplied(self) -> None: from time import time compare(time(), 1009846923.0) @replace('time.time', mock_time(None)) - def test_time_sequence(self, t: type[MockTime]): + def test_time_sequence(self, t: type[MockTime]) -> None: t.add(2002, 1, 1, 1, 0, 0) t.add(2002, 1, 1, 2, 0, 0) t.add(2002, 1, 1, 3, 0, 0) @@ -31,7 +32,7 @@ def test_time_sequence(self, t: type[MockTime]): compare(time(), 1009854000.0) @replace('time.time', mock_time(None)) - def test_add_datetime_supplied(self, t: type[MockTime]): + def test_add_datetime_supplied(self, t: type[MockTime]) -> None: from datetime import datetime from time import time t.add(datetime(2002, 1, 1, 2)) @@ -44,13 +45,13 @@ def test_add_datetime_supplied(self, t: type[MockTime]): ))): t.add(datetime(2001, 1, 1, tzinfo=tzinfo)) - def test_instantiate_with_datetime(self): + def test_instantiate_with_datetime(self) -> None: from datetime import datetime t = mock_time(datetime(2002, 1, 1, 2)) compare(t(), 1009850400.0) @replace('time.time', mock_time(None)) - def test_now_requested_longer_than_supplied(self, t: type[MockTime]): + def test_now_requested_longer_than_supplied(self, t: type[MockTime]) -> None: t.add(2002, 1, 1, 1, 0, 0) t.add(2002, 1, 1, 2, 0, 0) from time import time @@ -60,42 +61,42 @@ def test_now_requested_longer_than_supplied(self, t: type[MockTime]): compare(time(), 1009850403.0) @replace('time.time', mock_time()) - def test_call(self, t: type[MockTime]): + def test_call(self, t: type[MockTime]) -> None: compare(t(), 978307200.0) from time import time compare(time(), 978307201.0) @replace('time.time', mock_time()) - def test_repr_time(self): + def test_repr_time(self) -> None: from time import time compare(repr(time), "") @replace('time.time', mock_time(delta=10)) - def test_delta(self): + def test_delta(self) -> None: from time import time compare(time(), 978307200.0) compare(time(), 978307210.0) compare(time(), 978307220.0) @replace('time.time', mock_time(delta_type='minutes')) - def test_delta_type(self): + def test_delta_type(self) -> None: from time import time compare(time(), 978307200.0) compare(time(), 978307260.0) compare(time(), 978307380.0) @replace('time.time', mock_time(None)) - def test_set(self): + def test_set(self) -> None: from time import time - time = cast(type[MockTime], time) - time.set(2001, 1, 1, 1, 0, 1) + time_mock = cast(type[MockTime], time) + time_mock.set(2001, 1, 1, 1, 0, 1) compare(time(), 978310801.0) - time.set(2002, 1, 1, 1, 0, 0) + time_mock.set(2002, 1, 1, 1, 0, 0) compare(time(), 1009846800.0) compare(time(), 1009846802.0) @replace('time.time', mock_time(None)) - def test_set_datetime_supplied(self, t: type[MockTime]): + def test_set_datetime_supplied(self, t: type[MockTime]) -> None: from datetime import datetime from time import time t.set(datetime(2001, 1, 1, 1, 0, 1)) @@ -109,60 +110,60 @@ def test_set_datetime_supplied(self, t: type[MockTime]): t.set(datetime(2001, 1, 1, tzinfo=tzinfo)) @replace('time.time', mock_time(None)) - def test_set_kw(self): + def test_set_kw(self) -> None: from time import time - time = cast(type[MockTime], time) - time.set(year=2001, month=1, day=1, hour=1, second=1) + time_mock = cast(type[MockTime], time) + time_mock.set(year=2001, month=1, day=1, hour=1, second=1) compare(time(), 978310801.0) @replace('time.time', mock_time(None)) - def test_set_kw_tzinfo(self): + def test_set_kw_tzinfo(self) -> None: from time import time - time = cast(type[MockTime], time) + time_mock = cast(type[MockTime], time) with ShouldRaise(TypeError('Cannot add using tzinfo on MockTime')): - time.set(year=2001, tzinfo=SampleTZInfo()) + time_mock.set(year=2001, tzinfo=SampleTZInfo()) @replace('time.time', mock_time(None)) - def test_set_args_tzinfo(self): + def test_set_args_tzinfo(self) -> None: from time import time - time = cast(type[MockTime], time) + time_mock = cast(type[MockTime], time) with ShouldRaise(TypeError('Cannot add using tzinfo on MockTime')): - time.set(2002, 1, 2, 3, 4, 5, 6, SampleTZInfo()) + time_mock.set(2002, 1, 2, 3, 4, 5, 6, SampleTZInfo()) # type: ignore[arg-type] @replace('time.time', mock_time(None)) - def test_add_kw(self): + def test_add_kw(self) -> None: from time import time - time = cast(type[MockTime], time) - time.add(year=2001, month=1, day=1, hour=1, second=1) + time_mock = cast(type[MockTime], time) + time_mock.add(year=2001, month=1, day=1, hour=1, second=1) compare(time(), 978310801.0) @replace('time.time', mock_time(None)) - def test_add_tzinfo_kw(self): + def test_add_tzinfo_kw(self) -> None: from time import time - time = cast(type[MockTime], time) + time_mock = cast(type[MockTime], time) with ShouldRaise(TypeError('Cannot add using tzinfo on MockTime')): - time.add(year=2001, tzinfo=SampleTZInfo()) + time_mock.add(year=2001, tzinfo=SampleTZInfo()) @replace('time.time', mock_time(None)) - def test_add_tzinfo_args(self): + def test_add_tzinfo_args(self) -> None: from time import time - time = cast(type[MockTime], time) + time_mock = cast(type[MockTime], time) with ShouldRaise(TypeError('Cannot add using tzinfo on MockTime')): - time.add(2001, 1, 2, 3, 4, 5, 6, SampleTZInfo()) + time_mock.add(2001, 1, 2, 3, 4, 5, 6, SampleTZInfo()) # type: ignore[arg-type] @replace('time.time', mock_time(2001, 1, 2, 3, 4, 5, 600000)) - def test_max_number_args(self): + def test_max_number_args(self) -> None: from time import time compare(time(), 978404645.6) - def test_max_number_tzinfo(self): + def test_max_number_tzinfo(self) -> None: with ShouldRaise(TypeError( "You don't want to use tzinfo with test_time" )): - mock_time(2001, 1, 2, 3, 4, 5, 6, SampleTZInfo()) + mock_time(2001, 1, 2, 3, 4, 5, 6, SampleTZInfo()) # type: ignore[arg-type] @replace('time.time', mock_time(2001, 1, 2)) - def test_min_number_args(self): + def test_min_number_args(self) -> None: from time import time compare(time(), 978393600.0) @@ -175,54 +176,54 @@ def test_min_number_args(self): second=5, microsecond=6, )) - def test_all_kw(self): + def test_all_kw(self) -> None: from time import time compare(time(), 978404645.000006) - def test_kw_tzinfo(self): + def test_kw_tzinfo(self) -> None: with ShouldRaise(TypeError( "You don't want to use tzinfo with test_time" )): - mock_time(year=2001, tzinfo=SampleTZInfo()) + mock_time(year=2001, tzinfo=SampleTZInfo()) # type: ignore[arg-type] - def test_instance_tzinfo(self): + def test_instance_tzinfo(self) -> None: from datetime import datetime with ShouldRaise(TypeError( "You don't want to use tzinfo with test_time" )): mock_time(datetime(2001, 1, 1, tzinfo=SampleTZInfo())) - def test_subsecond_deltas(self): + def test_subsecond_deltas(self) -> None: time = mock_time(delta=0.5) compare(time(), 978307200.0) compare(time(), 978307200.5) compare(time(), 978307201.0) - def test_ms_deltas(self): + def test_ms_deltas(self) -> None: time = mock_time(delta=1000, delta_type='microseconds') compare(time(), 978307200.0) compare(time(), 978307200.001) compare(time(), 978307200.002) - def test_tick_when_static(self): + def test_tick_when_static(self) -> None: time = mock_time(delta=0) compare(time(), expected=978307200.0) time.tick(seconds=1) compare(time(), expected=978307201.0) - def test_tick_when_dynamic(self): + def test_tick_when_dynamic(self) -> None: # hopefully not that common? time = mock_time() compare(time(), expected=978307200.0) time.tick(seconds=1) compare(time(), expected=978307202.0) - def test_tick_with_timedelta_instance(self): + def test_tick_with_timedelta_instance(self) -> None: time = mock_time(delta=0) compare(time(), expected=978307200.0) time.tick(timedelta(seconds=1)) compare(time(), expected=978307201.0) - def test_old_import(self): + def test_old_import(self) -> None: from testfixtures import test_time assert test_time is mock_time