Skip to content

# merge_duplicate_classes

## `xsdata.codegen.handlers.merge_duplicate_classes`

### `MergeDuplicateClasses`

Bases: `ContainerHandlerInterface`

Remove duplicate classes.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`container` | `ContainerInterface` | The class container instance | required

Attributes:

Name | Type | Description
--- | --- | ---
`merges` | `dict[int, int]` | The merge instructions applied at the end

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

     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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73

    class MergeDuplicateClasses(ContainerHandlerInterface):
        """Remove duplicate classes.

        Args:
            container: The class container instance

        Attributes:
            merges: The merge instructions applied at the end
        """

        __slots__ = ("merges",)

        def __init__(self, container: ContainerInterface):
            """Initialize the rename duplicate class handler."""
            super().__init__(container)

            self.merges: dict[int, int] = {}

        def run(self) -> None:
            """Detect and merge duplicate classes conflicts."""
            for classes in self.group_duplicate_classes():
                if len(classes) > 1:
                    self.merge_classes(classes)

            if self.merges:
                for target in self.container:
                    self.update_references(target)

        def group_duplicate_classes(self) -> list[list[Class]]:
            """Group duplicate classes.

            This is very slow, because our gen models are unhashable.
            """
            groups: list[list[Class]] = []
            for cls in self.container:
                found = False
                for group in groups:
                    if cls == group[0]:
                        group.append(cls)
                        found = True
                        break
                if not found:
                    groups.append([cls])
            return groups

        def merge_classes(self, classes: list[Class]) -> None:
            """Remove the duplicate classes and schedule updates.

            Args:
                classes: A list of duplicate classes
            """
            keep = classes.pop()
            replace = keep.ref
            self.container.remove(*classes)

            for item in classes:
                self.merges[item.ref] = replace

        def update_references(self, target: Class) -> None:
            """Search and update the target class for merged references.

            Args:
                target: The target class instance to inspect and update
            """
            for tp in target.types():
                if tp.reference in self.merges:
                    tp.reference = self.merges[tp.reference]

#### `__init__(container)`

Initialize the rename duplicate class handler.

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

    19
    20
    21
    22
    23

    def __init__(self, container: ContainerInterface):
        """Initialize the rename duplicate class handler."""
        super().__init__(container)

        self.merges: dict[int, int] = {}

#### `run()`

Detect and merge duplicate classes conflicts.

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

    25
    26
    27
    28
    29
    30
    31
    32
    33

    def run(self) -> None:
        """Detect and merge duplicate classes conflicts."""
        for classes in self.group_duplicate_classes():
            if len(classes) > 1:
                self.merge_classes(classes)

        if self.merges:
            for target in self.container:
                self.update_references(target)

#### `group_duplicate_classes()`

Group duplicate classes.

This is very slow, because our gen models are unhashable.

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

    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50

    def group_duplicate_classes(self) -> list[list[Class]]:
        """Group duplicate classes.

        This is very slow, because our gen models are unhashable.
        """
        groups: list[list[Class]] = []
        for cls in self.container:
            found = False
            for group in groups:
                if cls == group[0]:
                    group.append(cls)
                    found = True
                    break
            if not found:
                groups.append([cls])
        return groups

#### `merge_classes(classes)`

Remove the duplicate classes and schedule updates.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`classes` | `list[Class]` | A list of duplicate classes | required

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

    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63

    def merge_classes(self, classes: list[Class]) -> None:
        """Remove the duplicate classes and schedule updates.

        Args:
            classes: A list of duplicate classes
        """
        keep = classes.pop()
        replace = keep.ref
        self.container.remove(*classes)

        for item in classes:
            self.merges[item.ref] = replace

#### `update_references(target)`

Search and update the target class for merged references.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`target` | `Class` | The target class instance to inspect and update | required

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

    65
    66
    67
    68
    69
    70
    71
    72
    73

    def update_references(self, target: Class) -> None:
        """Search and update the target class for merged references.

        Args:
            target: The target class instance to inspect and update
        """
        for tp in target.types():
            if tp.reference in self.merges:
                tp.reference = self.merges[tp.reference]