Debian Codegraph @

src/graphql/pyutils/is_iterable.py

raw
1 from collections.abc import Collection, Iterable, Mapping, ValuesView
2 from typing import Any
3
4 __all__ = ["is_collection", "is_iterable"]
5
6 collection_types: Any = Collection
7 if not isinstance({}.values(), Collection): # Python < 3.7.2
8 collection_types = (Collection, ValuesView)
9 iterable_types: Any = Iterable
10 not_iterable_types: Any = (bytes, bytearray, memoryview, str, Mapping)
11
12
13 def is_collection(value: Any) -> bool:
14 """Check if value is a collection, but not a string or a mapping."""
15 return isinstance(value, collection_types) and not isinstance(
16 value, not_iterable_types
17 )
18
19
20 def is_iterable(value: Any) -> bool:
21 """Check if value is an iterable, but not a string or a mapping."""
22 return isinstance(value, iterable_types) and not isinstance(
23 value, not_iterable_types
24 )