core: improve types for warn_if_empty

ok, works with this advice https://github.com/python/mypy/issues/1927 + overloads
This commit is contained in:
Dima Gerasimov 2020-05-25 01:23:30 +01:00
parent 216944b3cd
commit 248e48dc30
2 changed files with 11 additions and 4 deletions

View file

@ -314,14 +314,21 @@ def _warn_iterable(it, f=None):
return _warn_iterator(it, f=f) 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 @overload
def warn_if_empty(f: Callable[[], List[X]] ) -> Callable[[], List[X]] : ... def warn_if_empty(f: FL) -> FL: ...
@overload @overload
def warn_if_empty(f: Callable[[], Iterable[X]]) -> Callable[[], Iterable[X]]: ... def warn_if_empty(f: FI) -> FI: ...
def warn_if_empty(f): def warn_if_empty(f):
from functools import wraps from functools import wraps
@wraps(f) @wraps(f)
def wrapped(*args, **kwargs): def wrapped(*args, **kwargs):
res = f(*args, **kwargs) res = f(*args, **kwargs)
return _warn_iterable(res, f=f) return _warn_iterable(res, f=f)
return wrapped return wrapped # type: ignore

View file

@ -60,7 +60,7 @@ def test_warn_if_empty() -> None:
yield 'aba' yield 'aba'
@warn_if_empty @warn_if_empty
def empty() -> List[str]: def empty() -> List[int]:
return [] return []
# should be rejected by mypy! # should be rejected by mypy!