MCPcopy Index your code
hub / github.com/python/cpython / _compose_mro

Function _compose_mro

Lib/functools.py:828–868  ·  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

826 )
827
828def _compose_mro(cls, types):
829 """Calculates the method resolution order for a given class *cls*.
830
831 Includes relevant abstract base classes (with their respective bases) from
832 the *types* iterable. Uses a modified C3 linearization algorithm.
833
834 """
835 bases = set(cls.__mro__)
836 # Remove entries which are already present in the __mro__ or unrelated.
837 def is_related(typ):
838 return (typ not in bases and hasattr(typ, '__mro__')
839 and not isinstance(typ, GenericAlias)
840 and issubclass(cls, typ))
841 types = [n for n in types if is_related(n)]
842 # Remove entries which are strict bases of other entries (they will end up
843 # in the MRO anyway.
844 def is_strict_base(typ):
845 for other in types:
846 if typ != other and typ in other.__mro__:
847 return True
848 return False
849 types = [n for n in types if not is_strict_base(n)]
850 # Subclasses of the ABCs in *types* which are also implemented by
851 # *cls* can be used to stabilize ABC ordering.
852 type_set = set(types)
853 mro = []
854 for typ in types:
855 found = []
856 for sub in typ.__subclasses__():
857 if sub not in bases and issubclass(cls, sub):
858 found.append([s for s in sub.__mro__ if s in type_set])
859 if not found:
860 mro.append(typ)
861 continue
862 # Favor subclasses with the biggest number of useful bases
863 found.sort(key=len, reverse=True)
864 for sub in found:
865 for subcls in sub:
866 if subcls not in mro:
867 mro.append(subcls)
868 return _c3_mro(cls, abcs=mro)
869
870def _find_impl(cls, registry):
871 """Returns the best matching implementation from *registry* for type *cls*.

Callers 1

_find_implFunction · 0.85

Calls 6

setFunction · 0.85
is_relatedFunction · 0.85
is_strict_baseFunction · 0.85
_c3_mroFunction · 0.85
appendMethod · 0.45
sortMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…