-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathis_iterable.py
More file actions
26 lines (18 loc) · 821 Bytes
/
is_iterable.py
File metadata and controls
26 lines (18 loc) · 821 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Check whether objects are iterable"""
from __future__ import annotations
from collections.abc import Collection, Iterable, Mapping
from typing import Any, TypeGuard
__all__ = ["is_collection", "is_iterable"]
collection_types: Any = Collection
iterable_types: Any = Iterable
not_iterable_types: Any = (bytearray, bytes, str, memoryview, Mapping)
def is_collection(value: Any) -> TypeGuard[Collection]:
"""Check if value is a collection, but not a string or a mapping."""
return isinstance(value, collection_types) and not isinstance(
value, not_iterable_types
)
def is_iterable(value: Any) -> TypeGuard[Iterable]:
"""Check if value is an iterable, but not a string or a mapping."""
return isinstance(value, iterable_types) and not isinstance(
value, not_iterable_types
)