Skip to content

Util

lumi_filter.util

Utility classes for enhanced mapping functionality.

This module provides utility classes that extend standard mapping behavior to support advanced lookup patterns and inheritance-based resolution.

Classes:

Name Description
ClassHierarchyMapping

Mapping that supports class hierarchy lookups via MRO

Features
  • Method Resolution Order (MRO) based lookups
  • Union type support for field type resolution
  • Inheritance-aware field mapping
  • Standard MutableMapping interface compliance

ClassHierarchyMapping

Bases: MutableMapping

Mapping that supports class hierarchy lookups via Method Resolution Order.

This mapping class enables lookups that traverse the class hierarchy using Python's Method Resolution Order (MRO), allowing for inheritance-based field type resolution.

Parameters:

Name Type Description Default
mapping dict

Initial mapping data

None
Source code in lumi_filter/util.py
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
class ClassHierarchyMapping(MutableMapping):
    """Mapping that supports class hierarchy lookups via Method Resolution Order.

    This mapping class enables lookups that traverse the class hierarchy using
    Python's Method Resolution Order (MRO), allowing for inheritance-based
    field type resolution.

    Args:
        mapping (dict, optional): Initial mapping data
    """

    def __init__(self, mapping=None):
        self.data = dict(mapping) if mapping else {}

    def __getitem__(self, key):
        if isinstance(key, types.UnionType):
            keys = get_args(key)
        else:
            keys = [key]
        for key in keys:
            for cls in inspect.getmro(key):
                if cls in self.data:
                    return self.data[cls]

        raise KeyError(key)

    def __setitem__(self, key, value):
        self.data[key] = value

    def __delitem__(self, key):
        del self.data[key]

    def __iter__(self):
        return iter(self.data)

    def __len__(self):
        return len(self.data)

    def __contains__(self, key):
        return any(cls in self.data for cls in inspect.getmro(key))