@@ -21,10 +21,12 @@ def type_name(t: T.Any) -> str:
21
21
str: The string representation of the Python type.
22
22
23
23
Example:
24
+ ```python
24
25
>>> type_to_str(datetime.datetime)
25
26
'datetime'
26
27
>>> type_to_str(int)
27
28
'int'
29
+ ```
28
30
"""
29
31
type_str = repr (t )
30
32
if "<class '" in type_str :
@@ -37,13 +39,15 @@ def is_generic_alias(t) -> bool:
37
39
Check if a type is an alias type (list[str], dict[str, int], etc...])
38
40
39
41
Example:
40
- >>> is_generic_alias(list[str])
41
- True
42
- >>> is_generic_alias(int)
43
- False
44
- >>> String = str
45
- >>> is_generic_alias(String)
46
- False
42
+ ```python
43
+ >>> is_generic_alias(list[str])
44
+ True
45
+ >>> is_generic_alias(int)
46
+ False
47
+ >>> String = str
48
+ >>> is_generic_alias(String)
49
+ False
50
+ ```
47
51
"""
48
52
return type (t ) in ALIAS_TYPES
49
53
@@ -52,14 +56,16 @@ def is_union_type(t) -> bool:
52
56
"""
53
57
Check if a type is a union type (float | int, str | None, etc...)
54
58
55
- Examples:
56
- >>> is_union_type(int | str)
57
- True
58
- >>> from typing import Union
59
- >>> is_union_type(Union[int, str])
60
- True
61
- >>> is_union_type(list)
62
- False
59
+ Example:
60
+ ```python
61
+ >>> is_union_type(int | str)
62
+ True
63
+ >>> from typing import Union
64
+ >>> is_union_type(Union[int, str])
65
+ True
66
+ >>> is_union_type(list)
67
+ False
68
+ ```
63
69
"""
64
70
return type (t ) in UNION_TYPES
65
71
@@ -68,7 +74,8 @@ def is_iterable_type(t) -> bool:
68
74
"""
69
75
Check if a type is an iterable type (list, tuple, etc...)
70
76
71
- Examples:
77
+ Example:
78
+ ```python
72
79
>>> is_iterable_type(list)
73
80
True
74
81
>>> is_iterable_type(dict)
@@ -79,6 +86,7 @@ def is_iterable_type(t) -> bool:
79
86
True
80
87
>>> is_iterable_type(typing.Dict)
81
88
True
89
+ ```
82
90
"""
83
91
typing_iterables = [
84
92
typing .List ,
@@ -113,7 +121,8 @@ def is_mapping_type(t) -> bool:
113
121
"""
114
122
Check if a type is a mapping type (dict, OrderedDict, etc...)
115
123
116
- Examples:
124
+ Example:
125
+ ```python
117
126
>>> is_mapping_type(dict)
118
127
True
119
128
>>> is_mapping_type(list)
@@ -122,6 +131,7 @@ def is_mapping_type(t) -> bool:
122
131
True
123
132
>>> is_mapping_type(typing.OrderedDict)
124
133
True
134
+ ```
125
135
"""
126
136
typing_mappings = [
127
137
typing .Dict ,
@@ -140,11 +150,13 @@ def is_mapping_type(t) -> bool:
140
150
141
151
def type_simplified (t : T .Any ) -> T .Any | tuple [T .Any , ...]:
142
152
"""
143
- Examples:
144
- >>> type_simplify(list[str])
145
- <class 'list'>
146
- >>> type_simplify(float | list[str])
147
- (<class 'float'>, <class 'list'>)
153
+ Example:
154
+ ```python
155
+ >>> type_simplify(list[str])
156
+ <class 'list'>
157
+ >>> type_simplify(float | list[str])
158
+ (<class 'float'>, <class 'list'>)
159
+ ```
148
160
"""
149
161
origin = type_origin (t )
150
162
if isinstance (type (origin ), types .NoneType ) or origin is None :
@@ -172,13 +184,15 @@ def get_enum_choices(e) -> tuple[str, ...]:
172
184
tuple: A tuple of the names of the Enum options.
173
185
174
186
Example:
187
+ ```python
175
188
>>> import enum
176
189
>>> class Color(enum.Enum):
177
190
... RED = 1
178
191
... GREEN = 2
179
192
... BLUE = 3
180
193
>>> get_enum_choices(Color)
181
194
('RED', 'GREEN', 'BLUE')
195
+ ```
182
196
"""
183
197
if not is_enum (e ):
184
198
raise TypeError (f"'{ e } ' is not an Enum" )
@@ -197,7 +211,8 @@ def is_direct_literal(t: T.Any) -> bool:
197
211
Returns:
198
212
bool: True if the type is a pure Literal, False otherwise.
199
213
200
- Examples:
214
+ Example:
215
+ ```python
201
216
>>> from typing_extensions import Literal
202
217
>>> is_direct_literal(Literal[1, 2, 3])
203
218
True
@@ -207,6 +222,7 @@ def is_direct_literal(t: T.Any) -> bool:
207
222
False
208
223
>>> is_direct_literal(Union[str, Literal[1, 2]])
209
224
False
225
+ ```
210
226
"""
211
227
if t is typing_extensions .Literal :
212
228
return False
@@ -219,17 +235,19 @@ def is_or_contains_literal(t: T.Any) -> bool:
219
235
"""
220
236
Determine if the given type is a Literal type or contains a Literal type.
221
237
222
- Examples:
223
- >>> from typing import Union, Optional
224
- >>> from typing_extensions import Literal
225
- >>> is_or_contains_literal(Literal[1, 2, 3])
226
- True
227
- >>> is_or_contains_literal(Union[int, Literal[1, 2]])
228
- True
229
- >>> is_or_contains_literal(Optional[Literal['a', 'b']])
230
- True
231
- >>> is_or_contains_literal(int)
232
- False
238
+ Example:
239
+ ```python
240
+ >>> from typing import Union, Optional
241
+ >>> from typing_extensions import Literal
242
+ >>> is_or_contains_literal(Literal[1, 2, 3])
243
+ True
244
+ >>> is_or_contains_literal(Union[int, Literal[1, 2]])
245
+ True
246
+ >>> is_or_contains_literal(Optional[Literal['a', 'b']])
247
+ True
248
+ >>> is_or_contains_literal(int)
249
+ False
250
+ ```
233
251
"""
234
252
if is_direct_literal (t ):
235
253
return True
@@ -270,10 +288,12 @@ def type_origin(t: T.Any) -> T.Any:
270
288
A wrapper for typing.get_origin to get the origin of a type.
271
289
272
290
Example:
291
+ ```python
273
292
>>> type_args(list[list[str]])
274
293
<class 'list'>
275
294
>>> type_origin(float | int)
276
295
<class 'types.UnionType'>
296
+ ```
277
297
"""
278
298
return typing .get_origin (t )
279
299
@@ -283,12 +303,14 @@ def type_args(t: T.Any) -> tuple[T.Any, ...]:
283
303
A wrapper for typing.get_args to get the arguments of a type.
284
304
285
305
Example:
306
+ ```python
286
307
>>> type_args(list[str])
287
308
(<class 'str'>,)
288
309
>>> type_args(dict[str, int])
289
310
(<class 'str'>, <class 'int'>)
290
311
>>> type_args(list[list[str]])
291
312
(list[str],)
313
+ ```
292
314
"""
293
315
return typing .get_args (t )
294
316
0 commit comments