API Reference¶
Attributes¶
DEFAULT_REMOVE_MODULES: List[Union[str, re.Pattern]] = ['__main__', 'builtins', re.compile('^collections\\.(abc\\.)?'), 'contextlib', 're', 'types', 'typing']
module-attribute
¶
List of standard library modules used as the default value for the remove_modules option.
LITERAL_TYPE_SUPPORTED = sys.version_info >= (3, 8)
module-attribute
¶
(DEPRECATED) Flag for whether PEP 586's typing.Literal is supported. typenames no longer supports Python versions where this is false.
OR_OPERATOR_SUPPORTED = sys.version_info >= (3, 10)
module-attribute
¶
Flag for whether PEP 604's | operator (bitwise or) between types is supported.
STANDARD_COLLECTION_CLASSES = frozenset(STANDARD_COLLECTION_TO_TYPING_ALIAS_MAPPING.keys())
module-attribute
¶
Frozenset of standard collection classes that support use as a generic type starting in Python 3.9 (PEP 585).
STANDARD_COLLECTION_TO_TYPING_ALIAS_MAPPING = {dict: typing.Dict, list: typing.List, set: typing.Set, frozenset: typing.FrozenSet, tuple: typing.Tuple, collections.defaultdict: typing.DefaultDict, collections.OrderedDict: typing.OrderedDict, collections.ChainMap: typing.ChainMap, collections.Counter: typing.Counter, collections.deque: typing.Deque, collections.abc.Awaitable: typing.Awaitable, collections.abc.Coroutine: typing.Coroutine, collections.abc.AsyncIterable: typing.AsyncIterable, collections.abc.AsyncIterator: typing.AsyncIterator, collections.abc.AsyncGenerator: typing.AsyncGenerator, collections.abc.Iterable: typing.Iterable, collections.abc.Iterator: typing.Iterator, collections.abc.Generator: typing.Generator, collections.abc.Reversible: typing.Reversible, collections.abc.Collection: typing.Collection, collections.abc.Container: typing.Container, collections.abc.Callable: typing.Callable, collections.abc.Set: typing.AbstractSet, collections.abc.MutableSet: typing.MutableSet, collections.abc.Mapping: typing.Mapping, collections.abc.MutableMapping: typing.MutableMapping, collections.abc.Sequence: typing.Sequence, collections.abc.MutableSequence: typing.MutableSequence, collections.abc.ByteString: typing.ByteString, collections.abc.MappingView: typing.MappingView, collections.abc.KeysView: typing.KeysView, collections.abc.ItemsView: typing.ItemsView, collections.abc.ValuesView: typing.ValuesView, contextlib.AbstractContextManager: typing.ContextManager, contextlib.AbstractAsyncContextManager: typing.AsyncContextManager, re.Pattern: typing.Pattern, re.Match: typing.Match}
module-attribute
¶
Mapping from standard collection types that support use as a generic type starting in Python 3.9 (PEP 585) to their associated typing module generic alias.
Classes¶
BaseNode
dataclass
¶
Bases: ABC
, Generic[_TypeForm]
Abstract base class for a typenames node.
Source code in typenames/__init__.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
Attributes¶
is_none_type: bool
abstractmethod
property
¶
Whether this node corresponds to NoneType, which is the type of None. Used to identify the "Optional" special case of a union of types.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if this node's tp is NoneType. |
Functions¶
process_module_prefix(module_prefix: str) -> str
¶
Processes a module prefix (including the trailing '.') according to the 'remove_modules' settings in the given configuration.
Source code in typenames/__init__.py
130 131 132 133 134 135 136 |
|
GenericNode
dataclass
¶
Bases: BaseNode
Node that represents a generic type.
Source code in typenames/__init__.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
LiteralNode
dataclass
¶
Bases: BaseNode
Node that represents a literal value. Used for the arguments of Literal. Valid types for a literal value include ints, byte strings, unicode strings, bools, Enum values, None.
Source code in typenames/__init__.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
OptionalSyntax
¶
Bases: str
, Enum
Enum for optional syntax options. See "Optional Syntax" section of README for documentation.
Source code in typenames/__init__.py
51 52 53 54 55 56 57 58 |
|
ParamsListNode
dataclass
¶
Bases: BaseNode
Node that represents a list of parameters. Used for the arguments of Callable.
Source code in typenames/__init__.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
StandardCollectionSyntax
¶
Bases: str
, Enum
Enum for standard collection parameterized generic options. See "Standard Collection Syntax" section of README for documentation.
Source code in typenames/__init__.py
61 62 63 64 65 66 67 |
|
TypeNode
dataclass
¶
Bases: BaseNode
Node that represents a singleton type.
Source code in typenames/__init__.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
TypenamesConfig
dataclass
¶
Dataclass that holds all configuration options. See "Configurable options" section of README for documentation.
Source code in typenames/__init__.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
UnionSyntax
¶
Bases: str
, Enum
Enum for union syntax options. See "Union Syntax" section of README for documentation.
Source code in typenames/__init__.py
43 44 45 46 47 48 |
|
Functions¶
is_annotated_special_form(tp: type) -> bool
¶
Check if type annotation is the typing.Annotated special form.
Source code in typenames/__init__.py
474 475 476 |
|
is_standard_collection_type_alias(tp: type) -> bool
¶
Check if type annotation is a generic type and uses a standard collection type as a generic alias.
Source code in typenames/__init__.py
458 459 460 461 462 463 |
|
is_typing_module_collection_alias(tp: type) -> bool
¶
Check if type annotation is a generic type and uses a typing module collection generic alias, e.g., typing.List.
Source code in typenames/__init__.py
466 467 468 469 470 471 |
|
is_union_or_operator(tp: _TypeForm) -> bool
¶
Check if type annotation is a union and uses | operator (bitwise or).
Source code in typenames/__init__.py
401 402 403 |
|
is_union_special_form(tp: _TypeForm) -> bool
¶
Check if type annotation is a union and uses the typing.Union special form.
Source code in typenames/__init__.py
396 397 398 |
|
parse_type_tree(tp: _TypeForm, config: Optional[TypenamesConfig] = None, **kwargs: Any) -> BaseNode
¶
Parses a given type annotation into a tree data structure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp |
type
|
Type annotation |
required |
config |
Optional[TypenamesConfig]
|
Configuration dataclass. Defaults to None, which will instantiate one with default values. |
None
|
**kwargs |
Any
|
Override configuration options on provided or default configuration. See "Configurable options" section of README for documentation. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
BaseNode |
BaseNode
|
Root node of parsed type tree |
Source code in typenames/__init__.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
|
typenames(tp: _TypeForm, config: Optional[TypenamesConfig] = None, **kwargs: Any) -> str
¶
Render a string representation of a type annotation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tp |
type
|
Type annotation. |
required |
config |
Optional[TypenamesConfig]
|
Configuration dataclass. Defaults to None, which will instantiate one with default values. |
None
|
**kwargs |
Any
|
Override configuration options on provided or default configuration. See "Configurable options" section of README for documentation. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
String representation of input type. |
Source code in typenames/__init__.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
|