types
Types Utility Functions for Declarative Typed Argument Parsing.
The types
module contains a utility function used for determining and
comparing the types of `pydantic fields.
is_field_a(field, types)
Checks whether the subject is any of the supplied types.
The checks are performed as follows:
field
is one of thetypes
field
is an instance of one of thetypes
field
is a subclass of one of thetypes
If any of these conditions are True
, then the function returns True
,
else False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
field |
pydantic.fields.ModelField |
Subject field to check type of. |
required |
types |
Union[Any, Tuple[Any, ...]] |
Type(s) to compare field against. |
required |
Returns:
Type | Description |
---|---|
bool |
Whether the field is considered one of the types. |
Source code in pydantic_argparse/utils/types.py
def is_field_a(
field: pydantic.fields.ModelField,
types: Union[Any, Tuple[Any, ...]],
) -> bool:
"""Checks whether the subject *is* any of the supplied types.
The checks are performed as follows:
1. `field` *is* one of the `types`
2. `field` *is an instance* of one of the `types`
3. `field` *is a subclass* of one of the `types`
If any of these conditions are `True`, then the function returns `True`,
else `False`.
Args:
field (pydantic.fields.ModelField): Subject field to check type of.
types (Union[Any, Tuple[Any, ...]]): Type(s) to compare field against.
Returns:
bool: Whether the field *is* considered one of the types.
"""
# Create tuple if only one type was provided
if not isinstance(types, tuple):
types = (types,)
# Get field type, or origin if applicable
field_type = get_origin(field.outer_type_) or field.outer_type_
# Check `isinstance` and `issubclass` validity
# In order for `isinstance` and `issubclass` to be valid, all arguments
# should be instances of `type`, otherwise `TypeError` *may* be raised.
is_valid = all(isinstance(t, type) for t in (*types, field_type))
# Perform checks and return
return (
field_type in types
or (is_valid and isinstance(field_type, types))
or (is_valid and issubclass(field_type, types))
)