Skip to content

add_nillable_text_overrides

xsdata.codegen.handlers.add_nillable_text_overrides

AddNillableTextOverrides

Bases: RelativeHandlerInterface

Override inherited text attrs for nillable classes.

A nillable element accepts nil content, even if its base type requires a text value. The parser binds the nil text as None, so the class needs its own nillable copy of the text attr.

Source code in xsdata/codegen/handlers/add_nillable_text_overrides.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class AddNillableTextOverrides(RelativeHandlerInterface):
    """Override inherited text attrs for nillable classes.

    A nillable element accepts nil content, even if its base type
    requires a text value. The parser binds the nil text as None,
    so the class needs its own nillable copy of the text attr.
    """

    __slots__ = ()

    def process(self, target: Class) -> None:
        """Process entrypoint for classes.

        Skip classes that are not nillable or have their own text
        attr, the attr types processor cascades the flag to them.

        Args:
            target: The target class instance
        """
        if not target.nillable or any(x.xml_type is None for x in target.attrs):
            return

        for attr in self.base_attrs(target):
            if attr.xml_type is None:
                if not attr.is_nillable:
                    clone = attr.clone()
                    clone.restrictions.nillable = True
                    target.attrs.insert(0, clone)
                return

process(target)

Process entrypoint for classes.

Skip classes that are not nillable or have their own text attr, the attr types processor cascades the flag to them.

Parameters:

Name Type Description Default
target Class

The target class instance

required
Source code in xsdata/codegen/handlers/add_nillable_text_overrides.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def process(self, target: Class) -> None:
    """Process entrypoint for classes.

    Skip classes that are not nillable or have their own text
    attr, the attr types processor cascades the flag to them.

    Args:
        target: The target class instance
    """
    if not target.nillable or any(x.xml_type is None for x in target.attrs):
        return

    for attr in self.base_attrs(target):
        if attr.xml_type is None:
            if not attr.is_nillable:
                clone = attr.clone()
                clone.restrictions.nillable = True
                target.attrs.insert(0, clone)
            return