adorn.unit.dataclass module
Representation of dataclasses.dataclass
- class adorn.unit.dataclass.DataClass
Bases:
adorn.unit.unit.UnitA wrapper around
dataclass()from dataclasses import dataclass from adorn.orchestrator.base import Base from adorn.params import Params from adorn.unit.dataclass import DataClass class Example(DataClass): _registry = dict() @Example.register() @dataclass class A: a: int b: bool base = Base([Example()]) assert base.from_obj(A, Params({"a": 1, "b": False})) == A(1, False)
- contains(obj, orchestrator)
Check if obj is registered to
DataClass- Parameters
obj (Any) – the Type that may be a subclass of
Anumorchestrator (Orchestrator) – container of all types, not used
- Returns
- if
Truethenobjis represented byAnum, otherwise
False
- if
- Return type
bool
- from_obj(target_cls, orchestrator, obj)
Convert obj to an instance of the type
target_cls- Parameters
target_cls (Type) – the target Type, the given
objwill be converted intoorchestrator (Orchestrator) – container of all types, used to instantiate the arguments for the constructor of the given
dataclassobj (Any) – an instance, that will be converted to an instance of
target_cls
- Returns
an instantiated
dataclass()- Return type
_D
- get(key, orchestrator)
Return the relevant
Anumfor the key- Parameters
key (Any) – the Type that may be a subclass of the
Unitorchestrator (Orchestrator) – container of all types, not used
- Returns
- if
keyis anAnumthen return it, otherwise
None
- if
- Return type
Optional[type]
- classmethod register()
Add
dataclass()to the_registry- Returns
- funcion that adds the decorated
dataclass()to the_registry
- Return type
Callable[[Type[_D]], Type[_D]]
- type_check(target_cls, orchestrator, obj)
Ensure obj can be converted to the type
target_cls- Exceptions:
UnRepresentedTypeError:objdid not map to any of the types contained in the_registry
ParamError:objwas not of typeParams
- Parameters
target_cls (Type) – the target Type, the given
objwill be checked againstorchestrator (Orchestrator) – container of all types, used to check the arguments of the constructor for the given
dataclassobj (Any) – a
Params, that will be checked to see if it can be converted to an instance oftarget_cls
- Returns
- if
TypeCheckErrorthen there was an issue which would prevent converting
objto an instance oftarget_cls, otherwiseNone
- if
- Return type
Optional[TypeCheckError]