my.core: more work on typing @warn_if_empty, extra test
This commit is contained in:
parent
4b22d17188
commit
e3a71ea6c6
2 changed files with 33 additions and 18 deletions
|
@ -53,20 +53,38 @@ from my.core.error import test_sort_res_by
|
|||
from typing import Iterable, List
|
||||
import warnings
|
||||
from my.core import warn_if_empty
|
||||
def test_warn_if_empty():
|
||||
|
||||
def test_warn_if_empty() -> None:
|
||||
@warn_if_empty
|
||||
def nonempty() -> Iterable[str]:
|
||||
yield 'a'
|
||||
yield 'aba'
|
||||
|
||||
@warn_if_empty
|
||||
def empty(arg: str) -> List[str]:
|
||||
def empty() -> List[str]:
|
||||
return []
|
||||
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
assert list(nonempty()) == ['a', 'aba']
|
||||
assert len(w) == 0
|
||||
|
||||
assert empty('whatever') == []
|
||||
eee = empty()
|
||||
assert eee == []
|
||||
assert len(w) == 1
|
||||
|
||||
|
||||
def test_warn_iterable() -> None:
|
||||
from my.core.common import _warn_iterable
|
||||
i1: List[str] = ['a', 'b']
|
||||
i2: Iterable[int] = iter([1, 2, 3])
|
||||
# reveal_type(i1)
|
||||
# reveal_type(i2)
|
||||
x1 = _warn_iterable(i1)
|
||||
x2 = _warn_iterable(i2)
|
||||
# reveal_type(x1)
|
||||
# reveal_type(x2)
|
||||
with warnings.catch_warnings(record=True) as w:
|
||||
assert x1 is i1 # should be unchanged!
|
||||
assert len(w) == 0
|
||||
|
||||
assert list(x2) == [1, 2, 3]
|
||||
assert len(w) == 0
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue