typedload Module to load data into typed data structures
Classes
Loader
A loader object that recursively loads data into
the desired type.
basictypes: a set of types that are considered as
building blocks for everything else and do not
need to be converted further.
If you are not loading from json, you probably
want to add bytes to the set.
failonextra: Disabled by default.
When enabled, the loader will raise exceptions if
there are fields in the data that are not being used
by the type.
basiccast: Enabled by default.
When disabled, instead of trying to perform casts,
exceptions will be raised.
Since many json seem to encode numbers as strings,
to avoid extra complications this functionality is
provided.
If you know that your original data is encoded
properly, it is better to disable this.
dictequivalence: Enabled by default.
Automatically convert dict-like classes to dictionary
when loading. This enables them to be loaded into other
classes.
At the moment it supports:
argparse.Namespace
raiseconditionerrors: Enabled by default.
Raises exceptions when evaluating a condition from an
handler. When disabled, the exceptions are not raised
and the condition is considered False.
uniondebugconflict: Disabled by default
When enabled, all the possible types for the unions
are evaluated instead of stopping at the first that works.
If more than one type in the union works, an error is raised
because the types are conflicting and the union might return
different types with the same input value.
This option makes the loading slower and is only to be used when
debugging issues.
mangle_key: Defaults to 'name'
Specifies which key is used into the metadata dictionaries
to perform name-mangling.
handlers: This is the list that the loader uses to
perform its task.
The type is:
List[
Tuple[
Callable[[Type[T]], bool],
Callable[['Loader', Any, Type[T]], T]
]
]
The elements are: Tuple[Condition,Loader]
Condition(type) -> Bool
Loader(loader, value, type) -> type
In most cases, it is sufficient to append new elements
at the end, to handle more types.
There is an internal cache to speed up lookup, so after the
first call to load, this should no longer be modified.
strconstructed: Set of types to construct from a string.
frefs: Dictionary to resolve ForwardRef.
Something like
class Node(NamedTuple):
next: Optional['Node']
requires a ForwardRef (also in python3.7), which means that the type
is stored as string and must be resolved at runtime.
This dictionary contains the names of the types as keys, and the
actual types as values.
A loader object by default starts with an empty dictionary and
fills it with the types it encounters, but it is possible to
manually add more types to the dictionary.
Setting this to None disables any support for ForwardRef.
Reusing the same loader object on unrelated types might cause
failures, if the types are different but use the same names.
pep563: Set to true to use __future__.annotations
WARNING: DEPRECATED Support for this might be removed in any future
release without notice.
Check deferred evaluation in the documentation for more details.
This will make typedload much slower.
This PEP is broken and superseeded by PEP649.
Do not report bugs about this "feature". It's not here to stay.
These parameters can be set as named arguments in the constructor
or they can be set later on.
The constructor will accept any named argument, but only the documented
ones have any effect. This is to allow custom handlers to have their
own parameters as well.
Because internal caches are used, after the first call to load() these properties
should no longer be modified.
Using unions is complicated. The best is to use tagged unions using a Literal field.
If the types in the union are too similar to each other, it is easy to obtain an unexpected type.