Skip to content

Commit 248e48d

Browse files
committed
core: improve types for warn_if_empty
ok, works with this advice python/mypy#1927 + overloads
1 parent 216944b commit 248e48d

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎my/core/common.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,21 @@ def _warn_iterable(it, f=None):
314314
return _warn_iterator(it, f=f)
315315

316316

317+
# ok, this seems to work...
318+
# https://github.com/python/mypy/issues/1927#issue-167100413
319+
FL = TypeVar('FL', bound=Callable[..., List])
320+
FI = TypeVar('FI', bound=Callable[..., Iterable])
321+
317322
@overload
318-
def warn_if_empty(f: Callable[[], List[X]] ) -> Callable[[], List[X]] : ...
323+
def warn_if_empty(f: FL) -> FL: ...
319324
@overload
320-
def warn_if_empty(f: Callable[[], Iterable[X]]) -> Callable[[], Iterable[X]]: ...
325+
def warn_if_empty(f: FI) -> FI: ...
326+
327+
321328
def warn_if_empty(f):
322329
from functools import wraps
323330
@wraps(f)
324331
def wrapped(*args, **kwargs):
325332
res = f(*args, **kwargs)
326333
return _warn_iterable(res, f=f)
327-
return wrapped
334+
return wrapped # type: ignore

‎tests/misc.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def nonempty() -> Iterable[str]:
6060
yield 'aba'
6161

6262
@warn_if_empty
63-
def empty() -> List[str]:
63+
def empty() -> List[int]:
6464
return []
6565

6666
# should be rejected by mypy!

0 commit comments

Comments
 (0)