Datasets:

ArXiv:
Elron commited on
Commit
53bf478
1 Parent(s): 4c35afc

Upload type_utils.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. type_utils.py +69 -50
type_utils.py CHANGED
@@ -19,21 +19,30 @@ def isoftype(object, type):
19
  bool: True if the object is of the specified type, False otherwise.
20
 
21
  Examples:
22
- >>> isoftype(1, int)
23
- True
24
- >>> isoftype([1, 2, 3], typing.List[int])
25
- True
26
- >>> isoftype([1, 2, 3], typing.List[str])
27
- False
28
- >>> isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]])
29
- True
30
  """
 
 
 
31
  if hasattr(type, "__origin__"):
32
  origin = type.__origin__
33
  type_args = typing.get_args(type)
34
 
 
 
 
 
 
 
35
  if origin is list or origin is set:
36
  return all(isoftype(element, type_args[0]) for element in object)
 
37
  if origin is dict:
38
  return all(
39
  isoftype(key, type_args[0]) and isoftype(value, type_args[1])
@@ -46,8 +55,6 @@ def isoftype(object, type):
46
  )
47
  return None
48
 
49
- if type == typing.Any:
50
- return True
51
  return isinstance(object, type)
52
 
53
 
@@ -141,17 +148,21 @@ def get_origin(type_):
141
  Return None for unsupported types.
142
 
143
  Examples:
144
- ```python
145
- from typing_utils import get_origin
146
-
147
- get_origin(Literal[42]) is Literal
148
- get_origin(int) is None
149
- get_origin(ClassVar[int]) is ClassVar
150
- get_origin(Generic) is Generic
151
- get_origin(Generic[T]) is Generic
152
- get_origin(Union[T, int]) is Union
153
- get_origin(List[Tuple[T, T]][int]) == list
154
- ```
 
 
 
 
155
  """
156
  if hasattr(typing, "get_origin"): # python 3.8+
157
  _getter = typing.get_origin
@@ -185,15 +196,18 @@ def get_args(type_) -> typing.Tuple:
185
  For unions, basic simplifications used by Union constructor are performed.
186
 
187
  Examples:
188
- ```python
189
- from typing_utils import get_args
190
-
191
- get_args(Dict[str, int]) == (str, int)
192
- get_args(int) == ()
193
- get_args(Union[int, Union[T, int], str][int]) == (int, str)
194
- get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int])
195
- get_args(Callable[[], T][int]) == ([], int)
196
- ```
 
 
 
197
  """
198
  if hasattr(typing, "get_args"): # python 3.8+
199
  _getter = typing.get_args
@@ -440,25 +454,30 @@ def issubtype(
440
  Also works for nested types including ForwardRefs.
441
 
442
  Examples:
443
- ```python
444
- from typing_utils import issubtype
445
-
446
- issubtype(typing.List, typing.Any) == True
447
- issubtype(list, list) == True
448
- issubtype(list, typing.List) == True
449
- issubtype(list, typing.Sequence) == True
450
- issubtype(typing.List[int], list) == True
451
- issubtype(typing.List[typing.List], list) == True
452
- issubtype(list, typing.List[int]) == False
453
- issubtype(list, typing.Union[typing.Tuple, typing.Set]) == False
454
- issubtype(typing.List[typing.List], typing.List[typing.Sequence]) == True
455
- JSON = typing.Union[
456
- int, float, bool, str, None, typing.Sequence["JSON"],
457
- typing.Mapping[str, "JSON"]
458
- ]
459
- issubtype(str, JSON, forward_refs={'JSON': JSON}) == True
460
- issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) == True
461
- issubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) == False
462
- ```
 
 
 
 
 
463
  """
464
  return _is_normal_subtype(normalize(left), normalize(right), forward_refs)
 
19
  bool: True if the object is of the specified type, False otherwise.
20
 
21
  Examples:
22
+ .. highlight:: python
23
+ .. code-block:: python
24
+
25
+ isoftype(1, int) # True
26
+ isoftype([1, 2, 3], typing.List[int]) # True
27
+ isoftype([1, 2, 3], typing.List[str]) # False
28
+ isoftype([[1, 2], [3, 4]], typing.List[typing.List[int]]) # True
 
29
  """
30
+ if type == typing.Any:
31
+ return True
32
+
33
  if hasattr(type, "__origin__"):
34
  origin = type.__origin__
35
  type_args = typing.get_args(type)
36
 
37
+ if origin is typing.Union:
38
+ return any(isoftype(object, sub_type) for sub_type in type_args)
39
+
40
+ if not isinstance(object, origin):
41
+ return False
42
+
43
  if origin is list or origin is set:
44
  return all(isoftype(element, type_args[0]) for element in object)
45
+
46
  if origin is dict:
47
  return all(
48
  isoftype(key, type_args[0]) and isoftype(value, type_args[1])
 
55
  )
56
  return None
57
 
 
 
58
  return isinstance(object, type)
59
 
60
 
 
148
  Return None for unsupported types.
149
 
150
  Examples:
151
+ Here are some code examples using `get_origin` from the `typing_utils` module:
152
+
153
+ .. code-block:: python
154
+
155
+ from typing_utils import get_origin
156
+
157
+ # Examples of get_origin usage
158
+ get_origin(Literal[42]) is Literal # True
159
+ get_origin(int) is None # True
160
+ get_origin(ClassVar[int]) is ClassVar # True
161
+ get_origin(Generic) is Generic # True
162
+ get_origin(Generic[T]) is Generic # True
163
+ get_origin(Union[T, int]) is Union # True
164
+ get_origin(List[Tuple[T, T]][int]) == list # True
165
+
166
  """
167
  if hasattr(typing, "get_origin"): # python 3.8+
168
  _getter = typing.get_origin
 
196
  For unions, basic simplifications used by Union constructor are performed.
197
 
198
  Examples:
199
+ Here are some code examples using `get_args` from the `typing_utils` module:
200
+
201
+ .. code-block:: python
202
+
203
+ from typing_utils import get_args
204
+
205
+ # Examples of get_args usage
206
+ get_args(Dict[str, int]) == (str, int) # True
207
+ get_args(int) == () # True
208
+ get_args(Union[int, Union[T, int], str][int]) == (int, str) # True
209
+ get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) # True
210
+ get_args(Callable[[], T][int]) == ([], int) # True
211
  """
212
  if hasattr(typing, "get_args"): # python 3.8+
213
  _getter = typing.get_args
 
454
  Also works for nested types including ForwardRefs.
455
 
456
  Examples:
457
+ Here are some code examples using `issubtype` from the `typing_utils` module:
458
+
459
+ .. code-block:: python
460
+
461
+ from typing_utils import issubtype
462
+
463
+ # Examples of issubtype checks
464
+ issubtype(typing.List, typing.Any) # True
465
+ issubtype(list, list) # True
466
+ issubtype(list, typing.List) # True
467
+ issubtype(list, typing.Sequence) # True
468
+ issubtype(typing.List[int], list) # True
469
+ issubtype(typing.List[typing.List], list) # True
470
+ issubtype(list, typing.List[int]) # False
471
+ issubtype(list, typing.Union[typing.Tuple, typing.Set]) # False
472
+ issubtype(typing.List[typing.List], typing.List[typing.Sequence]) # True
473
+
474
+ # Example with custom JSON type
475
+ JSON = typing.Union[
476
+ int, float, bool, str, None, typing.Sequence["JSON"],
477
+ typing.Mapping[str, "JSON"]
478
+ ]
479
+ issubtype(str, JSON, forward_refs={'JSON': JSON}) # True
480
+ issubtype(typing.Dict[str, str], JSON, forward_refs={'JSON': JSON}) # True
481
+ issubtype(typing.Dict[str, bytes], JSON, forward_refs={'JSON': JSON}) # False
482
  """
483
  return _is_normal_subtype(normalize(left), normalize(right), forward_refs)