Skip to content

brainhops.datamodel.base

Base class of the data models and its converter.

Classes

DataModelBase

Bases: SharedOptions

Base class of all data models.

All such classes are Magic classes, and they implicitly convert values to the declared type of their attributes, at construction or during assignment.

All instances of these classes can be constructed from

  • an instance of a subclass,
  • an instance of a superclass,
  • an instance of "related" class (which shares a superclass), or
  • a dictionary,

using the from_any, from_instance or from_dict factories, as long as their attributes or keys are compatible.

Some classes are polymorphic, meaning that they can return instances of their subclasses based on the value of their arguments.

Methods:

from_dict classmethod
from_dict(other: Mapping, *args, **kwargs) -> Self

Build an instance from a mapping.

Only keys that can be passed to the constructor are used. Other keys are ignored, unlike in from_any

A key that corresponds to a fixed attribute (that is, an attribute that cannot be set by the constructor) is checked against its value.

Extra arguments (*args and **kwargs) are passed to the constructor and take precedence over the mapping.

Raises:

Type Description
ValueError

If a fixed attribute is given a value other than None or the value that the class fixes.

from_instance classmethod
from_instance(other: Any, *args, **kwargs) -> Self

Build an instance from an instance of a related data model.

Two classes are related when:

  • one of them is a parent of the other, or
  • when both belong to the same polymorphic family, or
  • when they represent the same "data model".

In all these cases, attributes that have the same name also have the same meaning, so they can be copied by name.

Note

  • Only attributes that name keyword arguments are used.
  • An attribute that is None leaves the default of the class in place.
  • Extra arguments (*args and **kwargs) are passed to the constructor and take precedence.
  • Unless other is already an instance of this class, an attribute of the instance that that corresponds to a fixed attribute in the target class is checked against its value.
  • In genemost casesral, cls.from_instance(other) is equivalent to other.to(cls, copy=True).

Parameters:

Name Type Description Default
other DataModelBase

The instance to copy.

required
*args Any

Constructor arguments, which take precedence over the fields of other.

()
**kwargs Any

Constructor arguments, which take precedence over the fields of other.

{}

Returns:

Type Description
Self

The new instance.

Raises:

Type Description
TypeError

If other is not an instance of a data model. A file, a mapping and any other value are read by from_any.

ConversionError

If the class of other is not related to this class, so that fields with the same name may mean different things.

ValueError

If a fixed field of other is set to a value other than the value that the class fixes.

from_any classmethod
from_any(other: Any, *args, **kwargs) -> Self

Build an instance from a mapping, a data model or an argument.

This constructors dispatches to from_dict, from_instance or the constructor, depending on the type of other.

Raises:

Type Description
TypeError

If a mapping has a key that names no field. Unlike from_dict, this method catches misspelled keys.

to
to(
    cls: type | None = None, *, copy: bool = False, **kwargs
) -> Any

Convert the object to another class of its data model.

Without a class, the object keeps its own class, and its attributes may be replaced by the keyword arguments.

Otherwise, the object is converted by cls.from_instance, which copies the attributes of a related class by name. Some subclasses override this method so that it triggers a more specific conversion.

Parameters:

Name Type Description Default
cls type

The class to convert to. By default, the class of the object.

None
copy bool

Whether to return a new object even when nothing changes.

False
**kwargs Any

Fields of the result, which take precedence over those of the object.

{}

Returns:

Type Description
DataModelBase

The converted object, an instance of cls.

Raises:

Type Description
ConversionError

If cls is not related to the class of the object.

DataModelConverter

Bases: Converter[DataModelBase, Any]

Converter to a DataModelBase, applied to fields of that type.

An instance of the target class is returned unchanged. Any other value goes through DataModelBase.from_any, so a mapping or a parent instance is read field by field rather than passed to the constructor. A TypeError or ValueError raised during conversion becomes a [ConversionError][bagof.converters.ConversionError] that keeps the original message. A refused loss (LossyConversionError), such as a loss of metadata, is a TypeError too, but it passes through unchanged, so that the error policy of the caller can act on it as on any other loss. Any other conversion error of brainhops is wrapped, so that a caller that tries several converters, such as the converter of a union, still moves on to the next one.

Methods:

__call__
__call__(value: Any) -> DataModelBase

Convert a value to an instance of the target class.