settings
DataclassAdaptable
Mixin class providing the ability to convert (adapt) one dataclass type to another.
Source code in fancy_dataclass/settings.py
adapt_to(dest_type)
Converts a DataclassAdaptable
object to another type, dest_type
.
By default this will attempt to coerce the fields from the original type to the new type, but subclasses may override the behavior, e.g. to allow field renaming.
Source code in fancy_dataclass/settings.py
coerce(obj)
classmethod
Constructs a DataclassAdaptable
object from the attributes of an arbitrary object.
Any missing attributes will be set to their default values.
DocFieldSettings
Bases: FieldSettings
Settings to expose a "doc" attribute of a field.
By default the "doc" field will be extracted from the field metadata, but as a fallback it will look for a PEP 727 Doc
-annotated field type.
For example, the following are equivalent ways to specify documentation for a field:
height: float = field(metadata={'doc': 'height (in cm)'})
height: Annotated[float, Doc('height (in cm)')]
Source code in fancy_dataclass/settings.py
FieldSettings
Bases: DataclassAdaptable
Class storing a bundle of parameters that will be extracted from dataclass field metadata.
Each DataclassMixin
class may store a __field_settings_type__
attribute which is a FieldSettings
subclass. This specifies which keys in the field.metadata
dictionary are recognized by the mixin class. Other keys will be ignored (unless they are used by other mixin classes).
Source code in fancy_dataclass/settings.py
from_field(field)
classmethod
Constructs a FieldSettings
object from a dataclasses.Field
's metadata.
Raises:
Type | Description |
---|---|
TypeError
|
If any field has the wrong type |
Source code in fancy_dataclass/settings.py
type_check()
Checks that every field on the FieldSettings
object is the proper type.
Raises:
Type | Description |
---|---|
TypeError
|
If a field is the wrong type |
Source code in fancy_dataclass/settings.py
MixinSettings
Bases: DataclassAdaptable
Base class for settings to be associated with fancy_dataclass
mixins.
Each DataclassMixin
class may store a __settings_type__
attribute consisting of a subclass of this class. The settings object will be instantiated as a __settings__
attribute on a mixin subclass when it is defined.