type_utils
trestle.common.type_utils
¤
Utilities for dealing with models.
Attributes¤
logger = logging.getLogger(__name__)
module-attribute
¤
Functions¤
get_inner_type(collection_field_type)
¤
Get the inner model in a generic collection model such as a List or a Dict.
For a dict the return type is of the value and not the key.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
collection_field_type
|
Union[Type[List[Any]], Type[Dict[str, Any]]]
|
Provided type annotation from a pydantic object |
required |
Returns:
Type | Description |
---|---|
Type[Any]
|
The desired type. |
Source code in trestle/common/type_utils.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
get_origin(field_type)
¤
Generalized and robust get_origin function.
This function is derived from work by pydantic, however, avoids complications from various python versions.
Source code in trestle/common/type_utils.py
26 27 28 29 30 31 32 33 |
|
is_collection_field_type(field_type)
¤
Check whether a type hint is a collection type as used by OSCAL.
Specifically this is whether the type is a list or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field_type
|
Type[Any]
|
A type or a type alias of a field typically as served via pydantic introspection |
required |
Returns:
Type | Description |
---|---|
bool
|
True if it is a collection type list. |
Source code in trestle/common/type_utils.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
handler: python