Skip to content

Commit

Permalink
core: improve types for warn_if_empty
Browse files Browse the repository at this point in the history
ok, works with this advice python/mypy#1927 + overloads
  • Loading branch information
karlicoss committed May 25, 2020
1 parent 216944b commit 248e48d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions my/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,21 @@ def _warn_iterable(it, f=None):
return _warn_iterator(it, f=f)


# ok, this seems to work...
# https://github.com/python/mypy/issues/1927#issue-167100413
FL = TypeVar('FL', bound=Callable[..., List])
FI = TypeVar('FI', bound=Callable[..., Iterable])

@overload
def warn_if_empty(f: Callable[[], List[X]] ) -> Callable[[], List[X]] : ...
def warn_if_empty(f: FL) -> FL: ...
@overload
def warn_if_empty(f: Callable[[], Iterable[X]]) -> Callable[[], Iterable[X]]: ...
def warn_if_empty(f: FI) -> FI: ...


def warn_if_empty(f):
from functools import wraps
@wraps(f)
def wrapped(*args, **kwargs):
res = f(*args, **kwargs)
return _warn_iterable(res, f=f)
return wrapped
return wrapped # type: ignore
2 changes: 1 addition & 1 deletion tests/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def nonempty() -> Iterable[str]:
yield 'aba'

@warn_if_empty
def empty() -> List[str]:
def empty() -> List[int]:
return []

# should be rejected by mypy!
Expand Down

0 comments on commit 248e48d

Please sign in to comment.