mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
Add is_series type guard, with tests
This commit is contained in:
@@ -6,4 +6,4 @@ import typing as _t
|
|||||||
_S = _t.TypeVar("_S")
|
_S = _t.TypeVar("_S")
|
||||||
|
|
||||||
Series = _t.Union[_t.MutableSequence[_S], _t.Tuple[_S], _t.Set[_S]]
|
Series = _t.Union[_t.MutableSequence[_S], _t.Tuple[_S], _t.Set[_S]]
|
||||||
"""Like Sequence, but excludes `str`."""
|
"""Like `typing.Sequence`, but excludes `str`."""
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import typing
|
import typing
|
||||||
|
|
||||||
# Local
|
# Local
|
||||||
from ..typing import is_type
|
from ..typing import is_type, is_series
|
||||||
|
|
||||||
|
|
||||||
class EmptyTestClass:
|
class EmptyTestClass:
|
||||||
@@ -64,3 +64,14 @@ def test_is_type():
|
|||||||
result = is_type(value, _type)
|
result = is_type(value, _type)
|
||||||
if result is not expected:
|
if result is not expected:
|
||||||
raise AssertionError(f"Got `{value}`, expected `{str(_type)}`")
|
raise AssertionError(f"Got `{value}`, expected `{str(_type)}`")
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_series():
|
||||||
|
checks = (
|
||||||
|
((1, 2, 3), True),
|
||||||
|
([1, 2, 3], True),
|
||||||
|
("1,2,3", False),
|
||||||
|
({1, 2, 3}, True),
|
||||||
|
)
|
||||||
|
for value, expected in checks:
|
||||||
|
assert is_series(value) is expected
|
||||||
|
|||||||
@@ -25,3 +25,10 @@ def is_type(value: typing.Any, *types: typing.Any) -> bool:
|
|||||||
return isinstance(value, type(_type))
|
return isinstance(value, type(_type))
|
||||||
return isinstance(value, origin)
|
return isinstance(value, origin)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def is_series(value: typing.Any) -> bool:
|
||||||
|
"""Determine if a value is a `hyperglass.types.Series`, i.e. non-string `typing.Sequence`."""
|
||||||
|
if isinstance(value, (typing.MutableSequence, typing.Tuple, typing.Set)):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|||||||
Reference in New Issue
Block a user