XmlContext#

class xsdata.formats.dataclass.context.XmlContext(element_name_generator=<function return_input>, attribute_name_generator=<function return_input>, class_type='dataclasses', models_package=None)[source]#

The models context class.

The context is responsible to provide binding metadata for models and their fields.

Parameters:
  • element_name_generator (Callable) – Default element name generator

  • attribute_name_generator (Callable) – Default attribute name generator

  • class_type (str) – Default class type dataclasses

  • models_package (Optional[str]) – Restrict auto locate to a specific package

cache#

Internal cache for binding metadata instances

xsi_cache#

Internal cache for xsi types to class locations

sys_modules#

The number of loaded sys modules

reset()[source]#

Reset all internal caches.

get_builder(globalns=None)[source]#

Return a new xml meta builder instance.

Return type:

XmlMetaBuilder

fetch(clazz, parent_ns=None, xsi_type=None)[source]#

Build the model metadata for the given class.

Parameters:
  • clazz (Type) – The requested dataclass type

  • parent_ns (Optional[str]) – The inherited parent namespace

  • xsi_type (Optional[str]) – 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

Returns:

A xml meta instance

build_xsi_cache()[source]#

Index all imported data classes by their xsi:type qualified name.

is_binding_model(clazz)[source]#

Return whether the clazz is a binding model.

If the models package is not empty also validate the class is located within that package.

Parameters:

clazz (Type[TypeVar(T)]) – The class type to inspect

Return type:

bool

Returns:

The bool result.

find_types(qname)[source]#

Find all classes that match the given xsi:type qname.

  • Ignores native schema types, xs:string, xs:float, xs:int, …

  • Rebuild cache if new modules were imported since last run

Parameters:

qname (str) – A namespace qualified name

Return type:

List[Type[TypeVar(T)]]

Returns:

A list of the matched classes.

find_type(qname)[source]#

Return the last imported class that matches the given xsi:type qname.

Parameters:

qname (str) – A namespace qualified name

Return type:

Optional[Type[TypeVar(T)]]

Returns:

A class type or None if no matches.

find_type_by_fields(field_names)[source]#

Find a data class that matches best the given list of field names.

Parameters:

field_names (Set[str]) – A set of field names

Return type:

Optional[Type[TypeVar(T)]]

Returns:

The best matching class or None if no matches. The class must have all the fields. If more than one classes have all the given fields, return the one with the least extra fields.

find_subclass(clazz, qname)[source]#

Find a subclass for the given clazz and xsi:type qname.

Compare all classes that match the given xsi:type qname and return the first one that is either a subclass or shares the same parent class as the original class.

Parameters:
  • clazz (Type) – The input clazz type

  • qname (str) – The xsi:type to lookup from cache

  • matches. (The matching class type or None if no) –

Return type:

Optional[Type]

build(clazz, parent_ns=None, globalns=None)[source]#

Fetch or build the binding metadata for the given class.

Parameters:
Return type:

XmlMeta

Returns:

The class binding metadata instance.

build_recursive(clazz, parent_ns=None)[source]#

Build the binding metadata for the given class and all of its dependencies.

This method is used in benchmarks!

Parameters:
  • clazz (Type) – The class type

  • parent_ns (Optional[str]) – The inherited parent namespace

local_names_match(names, clazz)[source]#

Check if the given field names match the given class type.

Silently ignore, typing errors. These classes are from third party libraries most of them time.

Parameters:
  • names (Set[str]) – A set of field names

  • clazz (Type) – The class type to inspect

Return type:

bool

Returns:

Whether the class contains all the field names.

classmethod is_derived(obj, clazz)[source]#

Return whether the obj is a subclass or a parent of the given class type.

Return type:

bool

classmethod get_subclasses(clazz)[source]#

Return an iterator of the given class subclasses.

Return type:

Iterator[Type]