Skip to content

deepmerge

Class info

Classes

Name Children Inherits
DeepMerger
jinjarope.deepmerge
A class that handles deep merging of data structures.

    🛈 DocStrings

    Module for deep merging of data structures with customizable merge strategies.

    DeepMerger

    A class that handles deep merging of data structures.

    The merger can be customized by providing different merge strategies for different types.

    Attributes:

    Name Type Description
    mergers dict[type[Any], Callable[..., Any]]

    A dictionary mapping types to their corresponding merge functions

    Example
    merger = DeepMerger()
    source = {"a": {"b": 1}}
    target = {"a": {"c": 2}}
    merger.merge(source, target)
    # {'a': {'b': 1, 'c': 2}}
    
    Source code in src/jinjarope/deepmerge.py
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    class DeepMerger:
        """A class that handles deep merging of data structures.
    
        The merger can be customized by providing different merge strategies
        for different types.
    
        Attributes:
            mergers: A dictionary mapping types to their corresponding merge functions
    
        Example:
            ```python
            merger = DeepMerger()
            source = {"a": {"b": 1}}
            target = {"a": {"c": 2}}
            merger.merge(source, target)
            # {'a': {'b': 1, 'c': 2}}
            ```
        """
    
        mergers: dict[type[Any], Callable[..., Any]] = DEFAULT_MERGERS
    
        def __init__(
            self, mergers: dict[type[Any], Callable[..., Any]] | None = None
        ) -> None:
            """Initialize the DeepMerger with custom merge strategies.
    
            Args:
                mergers: Optional dictionary of type-specific merge functions
            """
            if mergers is not None:
                self.mergers = mergers
    
        def merge(self, source: T, target: T) -> T:
            """Merge two objects of the same type.
    
            Args:
                source: The source object whose values take precedence
                target: The target object to merge into
    
            Returns:
                The merged object
    
            Raises:
                TypeError: If the types cannot be merged
            """
            source_type = type(source)
            target_type = type(target)
            merger = self.mergers.get(target_type)
            if source_type is not target_type or merger is None:
                msg = f"Cannot merge {source_type} with {target_type}"
                raise TypeError(msg)
            return merger(self, source, target)
    

    __init__

    __init__(mergers: dict[type[Any], Callable[..., Any]] | None = None) -> None
    

    Initialize the DeepMerger with custom merge strategies.

    Parameters:

    Name Type Description Default
    mergers dict[type[Any], Callable[..., Any]] | None

    Optional dictionary of type-specific merge functions

    None
    Source code in src/jinjarope/deepmerge.py
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    def __init__(
        self, mergers: dict[type[Any], Callable[..., Any]] | None = None
    ) -> None:
        """Initialize the DeepMerger with custom merge strategies.
    
        Args:
            mergers: Optional dictionary of type-specific merge functions
        """
        if mergers is not None:
            self.mergers = mergers
    

    merge

    merge(source: T, target: T) -> T
    

    Merge two objects of the same type.

    Parameters:

    Name Type Description Default
    source T

    The source object whose values take precedence

    required
    target T

    The target object to merge into

    required

    Returns:

    Type Description
    T

    The merged object

    Raises:

    Type Description
    TypeError

    If the types cannot be merged

    Source code in src/jinjarope/deepmerge.py
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    def merge(self, source: T, target: T) -> T:
        """Merge two objects of the same type.
    
        Args:
            source: The source object whose values take precedence
            target: The target object to merge into
    
        Returns:
            The merged object
    
        Raises:
            TypeError: If the types cannot be merged
        """
        source_type = type(source)
        target_type = type(target)
        merger = self.mergers.get(target_type)
        if source_type is not target_type or merger is None:
            msg = f"Cannot merge {source_type} with {target_type}"
            raise TypeError(msg)
        return merger(self, source, target)
    

    merge_dict

    merge_dict(merger: DeepMerger, source: Mapping, target: Mapping) -> Mapping
    

    Merge two mappings recursively.

    Parameters:

    Name Type Description Default
    merger DeepMerger

    The DeepMerger instance handling the merge operation

    required
    source Mapping

    The source mapping whose values take precedence

    required
    target Mapping

    The target mapping to merge into

    required

    Returns:

    Type Description
    Mapping

    A new dictionary containing the merged key-value pairs

    Example
    merger = DeepMerger()
    source = {"a": {"b": 1}}
    target = {"a": {"c": 2}}
    merge_dict(merger, source, target)
    # {'a': {'b': 1, 'c': 2}}
    
    Source code in src/jinjarope/deepmerge.py
    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
    def merge_dict(merger: DeepMerger, source: Mapping, target: Mapping) -> Mapping:
        """Merge two mappings recursively.
    
        Args:
            merger: The DeepMerger instance handling the merge operation
            source: The source mapping whose values take precedence
            target: The target mapping to merge into
    
        Returns:
            A new dictionary containing the merged key-value pairs
    
        Example:
            ```python
            merger = DeepMerger()
            source = {"a": {"b": 1}}
            target = {"a": {"c": 2}}
            merge_dict(merger, source, target)
            # {'a': {'b': 1, 'c': 2}}
            ```
        """
        result = dict(target)
        for key, source_value in source.items():
            target_value = result.get(key, type(source_value)())
            try:
                value = merger.merge(source_value, target_value)
            except TypeError:
                # If values can't be merged, use source value
                value = source_value
            result[key] = value
        return result
    

    merge_list

    merge_list(merger: DeepMerger, source: list, target: list) -> list
    

    Concatenate two lists.

    Parameters:

    Name Type Description Default
    merger DeepMerger

    The DeepMerger instance handling the merge operation

    required
    source list

    The source list to append

    required
    target list

    The target list to merge into

    required

    Returns:

    Type Description
    list

    A new list containing all elements from both lists

    Example
    merger = DeepMerger()
    merge_list(merger, [3, 4], [1, 2])
    # [1, 2, 3, 4]
    
    Source code in src/jinjarope/deepmerge.py
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    def merge_list(merger: DeepMerger, source: list, target: list) -> list:
        """Concatenate two lists.
    
        Args:
            merger: The DeepMerger instance handling the merge operation
            source: The source list to append
            target: The target list to merge into
    
        Returns:
            A new list containing all elements from both lists
    
        Example:
            ```python
            merger = DeepMerger()
            merge_list(merger, [3, 4], [1, 2])
            # [1, 2, 3, 4]
            ```
        """
        return target + source