xsdata.formats.dataclass.context module

class xsdata.formats.dataclass.context.XmlContext(name_generator=<function XmlContext.<lambda>>, cache=<factory>)[source]

Bases: object

Generate and cache the necessary metadata to bind an xml document data to a dataclass model.

Parameters
  • name_generator (Callable) – Callable to convert attribute names to local document names if the model fields metadata name is missing.

  • cache (Dict[Type, XmlMeta]) – Local storage to store and reuse models’ bind metadata.

name_generator()
cache: Dict[Type, XmlMeta]
fetch(clazz, parent_ns=None, xsi_type=None)[source]

Fetch the model metadata of the given dataclass type, namespace and xsi type.

Parameters
  • clazz (Type) – A dataclass model

  • parent_ns (Optional[str]) – The parent dataclass namespace if present.

  • xsi_type (Optional[QName]) – if present it means that the given clazz is derived and the lookup procedure needs to check and match a dataclass model to the qualified name instead.

Return type

XmlMeta

find_subclass(clazz, xsi_type)[source]

Find a derived class of the given clazz that matches the given qualified xsi type.

The derived class is either a subclass or shares the same parent class as the given class.

Parameters
  • clazz (Type) –

  • xsi_type (QName) –

Return type

Optional[Type]

match_class_source_qname(clazz, xsi_type)[source]

Match a given source qualified name with the given xsi type.

Parameters
  • clazz (Type) –

  • xsi_type (QName) –

Return type

bool

build(clazz, parent_ns=None)[source]

Fetch from cache or build the metadata object for the given class and parent namespace.

Parameters
Return type

XmlMeta

get_type_hints(clazz, parent_ns)[source]

Build the model class fields metadata.

Parameters
Return type

Iterator[XmlVar]

static resolve_namespaces(xml_type, namespace, parent_namespace)[source]

Resolve the namespace(s) for the given xml type and the parent namespace.

Only elements and wildcards are allowed to inherit the parent namespace if the given namespace is empty.

In case of wildcard try to decode the ##any, ##other, ##local, ##target.

Parameters
Return type

List[str]

static default_value(var)[source]

Return the default value/factory for the given field.

Parameters

var (Field) –

Return type

Any

static real_types(type_hint)[source]

Return a list of real types that can be used to bind or cast data.

Parameters

type_hint (Any) –

Return type

List

classmethod is_derived(obj, clazz)[source]

Return whether the given obj is derived from the given dataclass type.

Parameters
Return type

bool