Skip to content

# filter_classes

## `xsdata.codegen.handlers.filter_classes`

### `FilterClasses`

Bases: `ContainerHandlerInterface`

Filter classes for code generation based on the configuration strategy.

Source code in `xsdata/codegen/handlers/filter_classes.py`

     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
    34
    35
    36
    37
    38
    39
    40
    41

    class FilterClasses(ContainerHandlerInterface):
        """Filter classes for code generation based on the configuration strategy."""

        __slots__ = ()

        def run(self) -> None:
            """Main entrypoint to filter the class container.

            In order for a class to be considered global it has
            to be derived from an element, complexType, a binding
            operation or message

            If no global types exist, all classes will be generated.
            """
            classes = self.filter_all_globals()
            if classes:
                self.container.set(classes)
            else:
                logger.warning("No global types exist, will generate all types.")

        def filter_all_globals(self) -> list[Class]:
            """Filter all globals and any referenced types.

            This filter is trying to remove unused simple
            types.

            Returns:
                The list of classes for generation.
            """
            occurs = set()
            for obj in self.container:
                if obj.is_complex_type:
                    occurs.add(obj.ref)
                    occurs.update(obj.references)

            return [obj for obj in self.container if obj.ref in occurs]

#### `run()`

Main entrypoint to filter the class container.

In order for a class to be considered global it has to be derived from an element, complexType, a binding operation or message

If no global types exist, all classes will be generated.

Source code in `xsdata/codegen/handlers/filter_classes.py`

    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    def run(self) -> None:
        """Main entrypoint to filter the class container.

        In order for a class to be considered global it has
        to be derived from an element, complexType, a binding
        operation or message

        If no global types exist, all classes will be generated.
        """
        classes = self.filter_all_globals()
        if classes:
            self.container.set(classes)
        else:
            logger.warning("No global types exist, will generate all types.")

#### `filter_all_globals()`

Filter all globals and any referenced types.

This filter is trying to remove unused simple types.

Returns:

Type | Description
--- | ---
`list[Class]` | The list of classes for generation.

Source code in `xsdata/codegen/handlers/filter_classes.py`

    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41

    def filter_all_globals(self) -> list[Class]:
        """Filter all globals and any referenced types.

        This filter is trying to remove unused simple
        types.

        Returns:
            The list of classes for generation.
        """
        occurs = set()
        for obj in self.container:
            if obj.is_complex_type:
                occurs.add(obj.ref)
                occurs.update(obj.references)

        return [obj for obj in self.container if obj.ref in occurs]