typing
type_name ¶
Convert a Python type to its string representation (without the module name).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Any
|
A Python type. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The string representation of the Python type. |
Source code in objinspect/typing.py
simplified_type_name ¶
Simplifies the type name by removing module paths and optional "None" union.
is_generic_alias ¶
is_generic_alias(t) -> bool
Check if a type is an alias type (list[str], dict[str, int], etc...])
Example
Source code in objinspect/typing.py
is_union_type ¶
is_union_type(t) -> bool
Check if a type is a union type (float | int, str | None, etc...)
Example
Source code in objinspect/typing.py
is_iterable_type ¶
is_iterable_type(t) -> bool
Check if a type is an iterable type (list, tuple, etc...)
Example
Source code in objinspect/typing.py
is_mapping_type ¶
is_mapping_type(t) -> bool
Check if a type is a mapping type (dict, OrderedDict, etc...)
Example
Source code in objinspect/typing.py
type_simplified ¶
Example
Source code in objinspect/typing.py
get_enum_choices ¶
Get the options of a Python Enum.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
e
|
Enum
|
A Python Enum. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[str, ...]
|
A tuple of the names of the Enum options. |
Example
Source code in objinspect/typing.py
is_direct_literal ¶
Determine if the given type is a 'pure' Literal type. It checks if the input type is a direct instance of Literal,not including the Literal class itself. This function distinguishes between the 'Literal' class itself and instantiated Literal types. It returns True only for the latter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
Any
|
The type to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the type is a pure Literal, False otherwise. |
Example
Source code in objinspect/typing.py
is_or_contains_literal ¶
Determine if the given type is a Literal type or contains a Literal type.
Example
Source code in objinspect/typing.py
get_literal_choices ¶
Get the options of a Python Literal.
Source code in objinspect/typing.py
literal_contains ¶
Check if a value is in a Python Literal.
Source code in objinspect/typing.py
get_choices ¶
Try to get the choices of a Literal or Enum type. Will also work with a Union type that contains Literal or Enum types. Returns None if the type is not a Literal or Enum.
Source code in objinspect/typing.py
type_origin ¶
A wrapper for typing.get_origin to get the origin of a type.
Example
Source code in objinspect/typing.py
type_args ¶
A wrapper for typing.get_args to get the arguments of a type.