Skip to content

Commit

Permalink
REFACTOR: make latest mypy happy
Browse files Browse the repository at this point in the history
+ CHORE: update deps
  • Loading branch information
yashaka committed Jul 20, 2024
1 parent 0ed4982 commit d01b5f4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 56 deletions.
58 changes: 29 additions & 29 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions selene/common/_typing_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

Lambda = Callable[[T], R]
Proc = Callable[[T], None]
Predicate = Callable[[T], bool]
Predicate = Callable[[T], bool] # todo: should we return bool or Optional[bool]?
Fn = Callable[[T], R]


Expand All @@ -67,7 +67,7 @@ def __init__(
self._name = name
self._fn = fn

def __call__(self, entity: E) -> R | None:
def __call__(self, entity: E) -> R | None: # todo: do we really need None here?
return self._fn(entity)

def __str__(self):
Expand Down Expand Up @@ -150,25 +150,25 @@ def _full_description_or(
else alternative
)

@staticmethod
@overload
def _inverted(
predicate: Predicate[E],
_truthy_exceptions: Iterable[Type[Exception]] = (),
) -> Predicate[E]: ...

@staticmethod
@overload
def _inverted(
predicate: Query[E, bool],
_truthy_exceptions: Iterable[Type[Exception]] = (),
) -> Query[E, bool]: ...
# @staticmethod
# @overload
# def _inverted(
# predicate: Predicate[E],
# _truthy_exceptions: Iterable[Type[Exception]] = (),
# ) -> Predicate[E]: ...
#
# @staticmethod
# @overload
# def _inverted(
# predicate: Query[E, bool],
# _truthy_exceptions: Iterable[Type[Exception]] = (),
# ) -> Query[E, bool]: ...

@staticmethod
def _inverted(
predicate: Predicate[E] | Query[E, bool],
predicate: Callable[[E], bool | None],
_truthy_exceptions: Iterable[Type[Exception]] = (),
) -> Predicate[E] | Query[E, bool]:
) -> Callable[[E], bool | None]:
# TODO: ensure it works correctly:) e.g. unit test it

def not_predicate(entity: E) -> bool:
Expand All @@ -187,6 +187,7 @@ def not_predicate(entity: E) -> bool:
not_predicate,
)

# todo: consider refactoring to getattr(predicate, '__module__', None)
not_predicate.__module__ = predicate.__module__
not_predicate.__annotations__ = predicate.__annotations__

Expand Down
8 changes: 4 additions & 4 deletions selene/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def _to_raise_if_not(
by: Callable[[R], bool],
actual: Callable[[E], R] | None = None,
*,
_describe_actual_result: Callable[[E | R], str] | None = None,
_describe_actual_result: Callable[[R], str] | None = None,
_inverted: bool = False,
_falsy_exceptions: Iterable[Type[Exception]] = (AssertionError,),
): ...
Expand All @@ -134,7 +134,7 @@ def _to_raise_if_not(
cls,
by: Callable[[E | R], bool],
# todo: should we provide lambda it: it as default to actual?
actual: Optional[Callable[[E], E | R]] = None,
actual: Callable[[E], E | R] | None = None,
*,
# we can't name it:
# - as `describe` because it will not be clear what are we going to describe
Expand Down Expand Up @@ -258,9 +258,9 @@ def describe_error(error):
def _to_raise_if(
cls,
by: Callable[[E | R], bool],
actual: Callable[[E], R] | None = None,
actual: Callable[[E], E | R] | None = None,
*,
_describe_actual_result: Callable[[R], str] | None = None,
_describe_actual_result: Callable[[E | R], str] | None = None,
_falsy_exceptions: Iterable[Type[Exception]] = (AssertionError,),
):
return cls._to_raise_if_not(
Expand Down
17 changes: 11 additions & 6 deletions selene/core/wait.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import time
import warnings
from typing_extensions import Generic, Callable, TypeVar, Optional
from typing_extensions import Generic, Callable, TypeVar, Optional, cast

from selene.core.exceptions import TimeoutException

Expand Down Expand Up @@ -93,11 +93,11 @@ def or_fail_with(
def hook_failure(
self,
) -> Optional[Callable[[TimeoutException], Exception]]:
# TODO: hook_failure or failure_hook?
# todo: hook_failure or failure_hook?
return self._hook_failure

# TODO: consider renaming to `def to(...)`, though will sound awkward when wait.to(condition)
# TODO: do we need a second description/named param?
# todo: consider renaming to `def to(...)`, though will sound awkward when wait.to(condition)
# todo: do we need a second description/named param?
def for_(self, fn: Callable[[E], R]) -> R:
def logic(fn: Callable[[E], R]) -> R:
finish_time = time.time() + self._timeout
Expand Down Expand Up @@ -131,7 +131,12 @@ def logic(fn: Callable[[E], R]) -> R:

raise self._hook_failure(failure)

return self._decorator(self)(logic)(fn)
decorator = cast(
Callable[[Wait[E]], Callable[[Callable[..., R]], Callable[..., R]]],
self._decorator,
)

return decorator(self)(logic)(fn)

def until(self, fn: Callable[[E], R]) -> bool:
try:
Expand All @@ -140,7 +145,7 @@ def until(self, fn: Callable[[E], R]) -> bool:
except TimeoutException:
return False

# TODO: do we really need these aliases?
# todo: do we really need these aliases?
def command(self, name: str, fn: Callable[[E], None]) -> None:
self.for_(Command(name, fn))

Expand Down

0 comments on commit d01b5f4

Please sign in to comment.