16 from collections.abc
import Iterable, Mapping
17 from collections
import UserString
19 from os
import PathLike
20 from typing
import Any, TypeVar
22 from types
import UnionType
25 from typing
import Union
27 from typing
import TypedDict
31 typeddict_types = (
type(TypedDict(
'Dummy', {})),)
33 from typing_extensions
import TypedDict
as ExtTypedDict
37 typeddict_types += (
type(ExtTypedDict(
'Dummy', {})),)
39 from .platform
import PY_VERSION
42 TRUE_STRINGS = {
'TRUE',
'YES',
'ON',
'1'}
43 FALSE_STRINGS = {
'FALSE',
'NO',
'OFF',
'0',
'NONE',
''}
47 return isinstance(item, int)
51 return isinstance(item, (int, float))
55 return isinstance(item, (bytes, bytearray))
59 return isinstance(item, str)
63 return isinstance(item, PathLike)
67 if isinstance(item, (str, bytes, bytearray, UserString, IOBase)):
69 return isinstance(item, Iterable)
73 return isinstance(item, Mapping)
77 return (isinstance(item, UnionType)
78 or getattr(item,
'__origin__',
None)
is Union
79 or (allow_tuple
and isinstance(item, tuple)))
87 if getattr(item,
'__origin__',
None):
88 item = item.__origin__
89 if hasattr(item,
'_name')
and item._name:
95 elif isinstance(item, IOBase):
98 typ =
type(item)
if not isinstance(item, type)
else item
99 named_types = {str:
'string', bool:
'boolean', int:
'integer',
100 type(
None):
'None', dict:
'dictionary'}
101 name = named_types.get(typ, typ.__name__.strip(
'_'))
103 if PY_VERSION < (3, 7):
104 if name
in (
'List',
'Set',
'Tuple'):
108 return name.capitalize()
if capitalize
and name.islower()
else name
117 if typ
is type(
None):
124 return ' | '.join(
type_repr(a)
for a
in typ.__args__)
127 args =
', '.join(
type_repr(a)
for a
in typ.__args__)
128 return f
'{name}[{args}]'
133 for attr
in '__name__',
'_name':
134 name = getattr(typ, attr,
None)
141 args = getattr(typ,
'__args__', ())
146 return args
and not all(isinstance(t, TypeVar)
for t
in args)
164 return item.upper()
not in FALSE_STRINGS
def is_truthy(item)
Returns True or False depending on is the item considered true or not.
def is_falsy(item)
Opposite of :func:is_truthy.
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.
def type_repr(typ)
Return string representation for types.
def is_union(item, allow_tuple=False)