utils
Various utility functions and classes.
MissingRequiredFieldError
Bases: ValueError
Error type for a missing required field.
Source code in fancy_dataclass/utils.py
__init__(name, alias=None)
Constructor for MissingRequiredFieldError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
name of the missing field |
required |
alias
|
Optional[str]
|
alias of the missing field (e.g. when converting a JSON key) |
None
|
Source code in fancy_dataclass/utils.py
TypeConversionError
Bases: ValueError
Error type for type conversion.
Source code in fancy_dataclass/utils.py
__init__(tp, val)
Constructor for TypeConversionError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
type
|
type to convert to |
required |
val
|
Any
|
value to convert |
required |
Source code in fancy_dataclass/utils.py
all_subclasses(cls)
Gets all subclasses of a given class, including the class itself.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
Input class |
required |
Returns:
| Type | Description |
|---|---|
List[Type[T]]
|
List of subclasses of the input class |
Source code in fancy_dataclass/utils.py
camel_case_to_kebab_case(name)
Converts a string from camel case to kebab case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
String to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
Kebab case version of the string |
Source code in fancy_dataclass/utils.py
check_dataclass(cls)
Checks whether a given type is a dataclass, raising a TypeError otherwise.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
A Python type |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the given type is not a dataclass |
Source code in fancy_dataclass/utils.py
coerce_to_dataclass(cls, obj)
Coerces the fields from an arbitrary object to an instance of a dataclass type.
Any missing attributes will be set to the dataclass's default values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
Target dataclass type |
required |
obj
|
object
|
Object to coerce |
required |
Returns:
| Type | Description |
|---|---|
T
|
A new object of the desired type, coerced from the input object |
Source code in fancy_dataclass/utils.py
dataclass_field_type(cls, name)
Given a dataclass type and field name, gets the dataclass field's type annotation.
If the annotation is a string, resolves it to a type (see PEP 563, 649).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[DataclassInstance]
|
Dataclass type |
required |
name
|
str
|
Name of field |
required |
Returns:
| Type | Description |
|---|---|
type
|
Fully evaluated type |
Source code in fancy_dataclass/utils.py
dataclass_kw_only(**kwargs)
Identical to the usual dataclass decorator, but adds kw_only=True (if Python version is >= 3.10).
Source code in fancy_dataclass/utils.py
dataclass_type_map(cls, func)
Applies a type function to all dataclass field types, recursively through container types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[DataclassInstance]
|
Target dataclass type to manipulate |
required |
func
|
Callable[[type], type]
|
Function to map onto basic (non-container) field types |
required |
Returns:
| Type | Description |
|---|---|
Type[DataclassInstance]
|
A new dataclass type whose field types have been mapped by the function |
Source code in fancy_dataclass/utils.py
eval_type_str(type_str)
Gets the fully-evaluated type from a "stringized" type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type_str
|
str
|
String representing a type |
required |
Returns:
| Type | Description |
|---|---|
type
|
Fully evaluated type |
Source code in fancy_dataclass/utils.py
fully_qualified_class_name(cls)
Gets the fully qualified name of a class (including full module path and class name).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
A Python class |
required |
Returns:
| Type | Description |
|---|---|
str
|
Fully qualified name of the class |
Source code in fancy_dataclass/utils.py
get_annotations(cls)
Given a type, gets a dict mapping from field names to types.
get_dataclass_fields(obj, include_all=False)
Variant of dataclasses.fields which can optionally include ClassVars and InitVars.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Union[type, object]
|
Python class or object |
required |
include_all
|
bool
|
Whether to include |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[Field, ...]
|
Tuple of |
Source code in fancy_dataclass/utils.py
get_object_from_fully_qualified_name(name)
Given a fully-qualified name of some object, gets the object, importing the module as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Fully qualified object name |
required |
Returns:
| Type | Description |
|---|---|
object
|
Object with the fully qualified name |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the name does not have a . character |
Source code in fancy_dataclass/utils.py
get_subclass_with_name(cls, name)
Gets the subclass of a class with the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
Type[T]
|
A Python class |
required |
name
|
str
|
Name of the subclass |
required |
Returns:
| Type | Description |
|---|---|
Type[T]
|
Subclass of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no subclass with the given name exists |
Source code in fancy_dataclass/utils.py
is_dataclass_type(cls)
Returns True if the given type is a dataclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
type
|
A Python type |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[Type[DataclassInstance]]
|
True if |
issubclass_safe(type1, type2)
Calls issubclass(type1, type2), returning False if an error occurs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
type1
|
type
|
First type |
required |
type2
|
type
|
Second type |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in fancy_dataclass/utils.py
merge_dataclasses(*classes, cls_name='_', bases=None, allow_duplicates=False)
Merges multiple dataclasses together into a single dataclass whose fields have been combined.
This preserves ClassVars but does not recursively merge subfields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classes
|
type
|
Multiple dataclass types |
()
|
cls_name
|
str
|
Name of the output dataclass |
'_'
|
bases
|
Optional[Tuple[type, ...]]
|
Base classes for the new type |
None
|
allow_duplicates
|
bool
|
Whether to allow duplicate field names |
False
|
Returns:
| Type | Description |
|---|---|
type
|
The merged dataclass type |
Raises:
| Type | Description |
|---|---|
TypeError
|
If there are any duplicate field names |
Source code in fancy_dataclass/utils.py
obj_class_name(obj)
Gets the name of the class of an object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
object
|
A Python object |
required |
Returns:
| Type | Description |
|---|---|
str
|
Name of the object's class |
safe_dict_update(d1, d2)
Updates the first dict with the second, in-place.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d1
|
Dict[str, Any]
|
First dict, to be updated |
required |
d2
|
Dict[str, Any]
|
Second dict |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any dict keys overlap |
Source code in fancy_dataclass/utils.py
snake_case_to_camel_case(name)
Converts a string from snake case to camel case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
String to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
Camel case version of the string |
Source code in fancy_dataclass/utils.py
type_is_optional(tp)
Determines if a type is an Optional type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
type
|
Type to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the type is Optional |
Source code in fancy_dataclass/utils.py
type_is_union(tp)
Determines if a type is a Union type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tp
|
type
|
Type to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the type is a Union |