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:
parent
216944b3cd
commit
248e48dc30
2 changed files with 11 additions and 4 deletions
|
@ -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
|
||||||
|
|
|
@ -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!
|
||||||
|
|
Loading…
Add table
Reference in a new issue