ts_lib_xml.pydantic_xml module#

class BaseXmlModel[source]#

Bases: BaseModel

Base pydantic-xml model.

classmethod model_rebuild(**kwargs: Any) None[source]#

Try to rebuild the pydantic-core schema for the model.

This may be necessary when one of the annotations is a ForwardRef which could not be resolved during the initial attempt to build the schema, and automatic rebuilding fails.

Parameters:
  • force – Whether to force the rebuilding of the model schema, defaults to False.

  • raise_errors – Whether to raise errors, defaults to True.

  • _parent_namespace_depth – The depth level of the parent namespace, defaults to 2.

  • _types_namespace – The types namespace, defaults to None.

Returns:

Returns None if the schema is already “complete” and rebuilding was not required. If rebuilding _was_ required, returns True if rebuilding was successful, otherwise False.

classmethod from_xml_tree(root: Element, context: Dict[str, Any] | None = None) ModelT[source]#

Deserializes an xml element tree to an object of cls type.

Parameters:
  • root – xml element to deserialize the object from

  • context – pydantic validation context

Returns:

deserialized object

classmethod from_xml(source: str | bytes, context: Dict[str, Any] | None = None) ModelT[source]#

Deserializes an xml string to an object of cls type.

Parameters:
  • source – xml string

  • context – pydantic validation context

Returns:

deserialized object

to_xml_tree(*, skip_empty: bool = False) Element[source]#

Serializes the object to an xml tree.

Parameters:

skip_empty – skip empty elements (elements without sub-elements, attributes and text, Nones)

Returns:

object xml representation

to_xml(*, skip_empty: bool = False, **kwargs: Any) str | bytes[source]#

Serializes the object to an xml string.

Parameters:
  • skip_empty – skip empty elements (elements without sub-elements, attributes and text, Nones)

  • kwargs – additional xml serialization arguments

Returns:

object xml representation

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

class RootXmlModel(root: RootModelRootType = PydanticUndefined)[source]#

Bases: RootModel[~RootModelRootType], BaseXmlModel, Generic[RootModelRootType]

Base pydantic-xml root model.

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'root': FieldInfo(annotation=~RootModelRootType, required=True)}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

exception ModelError[source]#

Bases: BaseError

Model definition error.

exception ParsingError[source]#

Bases: BaseError

Xml parsing error.

attr(name: str | None = None, ns: str | None = None, *, default: Any = PydanticUndefined, default_factory: Callable[[], Any] | None = PydanticUndefined, **kwargs: Any) Any[source]#

Marks a pydantic field as an xml attribute.

Parameters:
  • name – attribute name

  • ns – attribute xml namespace

  • default – the default value of the field.

  • default_factory – the factory function used to construct the default for the field.

  • kwargs – pydantic field arguments. See pydantic.Field

element(tag: str | None = None, ns: str | None = None, nsmap: Dict[str, str] | None = None, nillable: bool = False, *, default: Any = PydanticUndefined, default_factory: Callable[[], Any] | None = PydanticUndefined, **kwargs: Any) Any[source]#

Marks a pydantic field as an xml element.

Parameters:
  • tag – element tag

  • ns – element xml namespace

  • nsmap – element xml namespace map

  • nillable – is element nillable. See https://www.w3.org/TR/xmlschema-1/#xsi_nil.

  • default – the default value of the field.

  • default_factory – the factory function used to construct the default for the field.

  • kwargs – pydantic field arguments. See pydantic.Field

wrapped(path: str, entity: FieldInfo | None = None, ns: str | None = None, nsmap: Dict[str, str] | None = None, *, default: Any = PydanticUndefined, default_factory: Callable[[], Any] | None = PydanticUndefined, **kwargs: Any) Any[source]#

Marks a pydantic field as a wrapped xml entity.

Parameters:
  • entity – wrapped entity

  • path – entity path

  • ns – element xml namespace

  • nsmap – element xml namespace map

  • default – the default value of the field.

  • default_factory – the factory function used to construct the default for the field.

  • kwargs – pydantic field arguments. See pydantic.Field

computed_attr(prop: PropertyT | None = None, *, name: str | None = None, ns: str | None = None, **kwargs: Any) PropertyT | Callable[[PropertyT], PropertyT][source]#

Marks a property as an xml attribute.

Parameters:
  • prop – decorated property

  • name – attribute name

  • ns – attribute xml namespace

  • kwargs – pydantic computed field arguments. See pydantic.computed_field

computed_element(prop: PropertyT | None = None, *, tag: str | None = None, ns: str | None = None, nsmap: Dict[str, str] | None = None, nillable: bool = False, **kwargs: Any) PropertyT | Callable[[PropertyT], PropertyT][source]#

Marks a property as an xml element.

Parameters:
  • prop – decorated property

  • tag – element tag

  • ns – element xml namespace

  • nsmap – element xml namespace map

  • nillable – is element nillable. See https://www.w3.org/TR/xmlschema-1/#xsi_nil.

  • kwargs – pydantic computed field arguments. See pydantic.computed_field

create_model(__model_name: str, *, __tag__: str | None = None, __ns__: str | None = None, __nsmap__: Dict[str, str] | None = None, __ns_attrs__: bool | None = None, __skip_empty__: bool | None = None, __search_mode__: SearchMode | None = None, __base__: Type[Model] | Tuple[Type[Model], ...] | None = None, __module__: str | None = None, **kwargs: Any) Type[Model][source]#

Dynamically creates a new pydantic-xml model.

Parameters:
  • __model_name – model name

  • __tag__ – element tag

  • __ns__ – element namespace

  • __nsmap__ – element namespace map

  • __ns_attrs__ – use namespaced attributes

  • __skip_empty__ – skip empty elements (elements without sub-elements, attributes and text)

  • __search_mode__ – element search mode

  • __base__ – model base class

  • __module__ – module name that the model belongs to

  • kwargs – pydantic model creation arguments. See https://docs.pydantic.dev/latest/api/base_model/#pydantic.create_model.

Returns:

created model