Robot Framework
robottypes.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from collections.abc import Iterable, Mapping
17 from collections import UserString
18 from io import IOBase
19 from os import PathLike
20 from typing import Any, TypeVar
21 try:
22  from types import UnionType
23 except ImportError: # Python < 3.10
24  UnionType = ()
25 from typing import Union
26 try:
27  from typing import TypedDict
28 except ImportError: # Python < 3.8
29  typeddict_types = ()
30 else:
31  typeddict_types = (type(TypedDict('Dummy', {})),)
32 try:
33  from typing_extensions import TypedDict as ExtTypedDict
34 except ImportError:
35  pass
36 else:
37  typeddict_types += (type(ExtTypedDict('Dummy', {})),)
38 
39 from .platform import PY_VERSION
40 
41 
42 TRUE_STRINGS = {'TRUE', 'YES', 'ON', '1'}
43 FALSE_STRINGS = {'FALSE', 'NO', 'OFF', '0', 'NONE', ''}
44 
45 
46 def is_integer(item):
47  return isinstance(item, int)
48 
49 
50 def is_number(item):
51  return isinstance(item, (int, float))
52 
53 
54 def is_bytes(item):
55  return isinstance(item, (bytes, bytearray))
56 
57 
58 def is_string(item):
59  return isinstance(item, str)
60 
61 
62 def is_pathlike(item):
63  return isinstance(item, PathLike)
64 
65 
66 def is_list_like(item):
67  if isinstance(item, (str, bytes, bytearray, UserString, IOBase)):
68  return False
69  return isinstance(item, Iterable)
70 
71 
72 def is_dict_like(item):
73  return isinstance(item, Mapping)
74 
75 
76 def is_union(item, allow_tuple=False):
77  return (isinstance(item, UnionType)
78  or getattr(item, '__origin__', None) is Union
79  or (allow_tuple and isinstance(item, tuple)))
80 
81 
82 
86 def type_name(item, capitalize=False):
87  if getattr(item, '__origin__', None):
88  item = item.__origin__
89  if hasattr(item, '_name') and item._name:
90  # Union, Any, etc. from typing have real name in _name and __name__ is just
91  # generic `SpecialForm`. Also, pandas.Series has _name but it's None.
92  name = item._name
93  elif is_union(item):
94  name = 'Union'
95  elif isinstance(item, IOBase):
96  name = 'file'
97  else:
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('_'))
102  # Generics from typing. With newer versions we get "real" type via __origin__.
103  if PY_VERSION < (3, 7):
104  if name in ('List', 'Set', 'Tuple'):
105  name = name.lower()
106  elif name == 'Dict':
107  name = 'dictionary'
108  return name.capitalize() if capitalize and name.islower() else name
109 
110 
111 
116 def type_repr(typ):
117  if typ is type(None):
118  return 'None'
119  if typ is Ellipsis:
120  return '...'
121  if typ is Any: # Needed with Python 3.6, with newer `Any._name` exists.
122  return 'Any'
123  if is_union(typ):
124  return ' | '.join(type_repr(a) for a in typ.__args__)
125  name = _get_type_name(typ)
126  if _has_args(typ):
127  args = ', '.join(type_repr(a) for a in typ.__args__)
128  return f'{name}[{args}]'
129  return name
130 
131 
132 def _get_type_name(typ):
133  for attr in '__name__', '_name':
134  name = getattr(typ, attr, None)
135  if name:
136  return name
137  return str(typ)
138 
139 
140 def _has_args(typ):
141  args = getattr(typ, '__args__', ())
142  # __args__ contains TypeVars when accessed directly from typing.List and other
143  # such types withPython 3.7-3.8. With Python 3.6 __args__ is None in that case
144  # and with Python 3.9+ it doesn't exist at all. When using like List[int].__args__
145  # everything works the same way regardless the version.
146  return args and not all(isinstance(t, TypeVar) for t in args)
147 
148 
149 
162 def is_truthy(item):
163  if is_string(item):
164  return item.upper() not in FALSE_STRINGS
165  return bool(item)
166 
167 
168 
169 def is_falsy(item):
170  return not is_truthy(item)
def is_dict_like(item)
Definition: robottypes.py:72
def is_truthy(item)
Returns True or False depending on is the item considered true or not.
Definition: robottypes.py:162
def is_falsy(item)
Opposite of :func:is_truthy.
Definition: robottypes.py:169
def type_name(item, capitalize=False)
Return "non-technical" type name for objects and types.
Definition: robottypes.py:86
def type_repr(typ)
Return string representation for types.
Definition: robottypes.py:116
def is_union(item, allow_tuple=False)
Definition: robottypes.py:76
def is_list_like(item)
Definition: robottypes.py:66