MCPcopy Index your code
hub / github.com/RustPython/RustPython / _compose_mro

Function _compose_mro

Lib/functools.py:807–847  ·  view source on GitHub ↗

Calculates the method resolution order for a given class *cls*. Includes relevant abstract base classes (with their respective bases) from the *types* iterable. Uses a modified C3 linearization algorithm.

(cls, types)

Source from the content-addressed store, hash-verified

805 )
806
807def _compose_mro(cls, types):
808 """Calculates the method resolution order for a given class *cls*.
809
810 Includes relevant abstract base classes (with their respective bases) from
811 the *types* iterable. Uses a modified C3 linearization algorithm.
812
813 """
814 bases = set(cls.__mro__)
815 # Remove entries which are already present in the __mro__ or unrelated.
816 def is_related(typ):
817 return (typ not in bases and hasattr(typ, '__mro__')
818 and not isinstance(typ, GenericAlias)
819 and issubclass(cls, typ))
820 types = [n for n in types if is_related(n)]
821 # Remove entries which are strict bases of other entries (they will end up
822 # in the MRO anyway.
823 def is_strict_base(typ):
824 for other in types:
825 if typ != other and typ in other.__mro__:
826 return True
827 return False
828 types = [n for n in types if not is_strict_base(n)]
829 # Subclasses of the ABCs in *types* which are also implemented by
830 # *cls* can be used to stabilize ABC ordering.
831 type_set = set(types)
832 mro = []
833 for typ in types:
834 found = []
835 for sub in typ.__subclasses__():
836 if sub not in bases and issubclass(cls, sub):
837 found.append([s for s in sub.__mro__ if s in type_set])
838 if not found:
839 mro.append(typ)
840 continue
841 # Favor subclasses with the biggest number of useful bases
842 found.sort(key=len, reverse=True)
843 for sub in found:
844 for subcls in sub:
845 if subcls not in mro:
846 mro.append(subcls)
847 return _c3_mro(cls, abcs=mro)
848
849def _find_impl(cls, registry):
850 """Returns the best matching implementation from *registry* for type *cls*.

Callers 1

_find_implFunction · 0.85

Calls 8

setFunction · 0.85
is_relatedFunction · 0.85
is_strict_baseFunction · 0.85
issubclassFunction · 0.85
_c3_mroFunction · 0.85
__subclasses__Method · 0.80
appendMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected