MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _compose_mro

Function _compose_mro

tools/python-3.11.9-amd64/Lib/functools.py:732–772  ·  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

730 )
731
732def _compose_mro(cls, types):
733 """Calculates the method resolution order for a given class *cls*.
734
735 Includes relevant abstract base classes (with their respective bases) from
736 the *types* iterable. Uses a modified C3 linearization algorithm.
737
738 """
739 bases = set(cls.__mro__)
740 # Remove entries which are already present in the __mro__ or unrelated.
741 def is_related(typ):
742 return (typ not in bases and hasattr(typ, '__mro__')
743 and not isinstance(typ, GenericAlias)
744 and issubclass(cls, typ))
745 types = [n for n in types if is_related(n)]
746 # Remove entries which are strict bases of other entries (they will end up
747 # in the MRO anyway.
748 def is_strict_base(typ):
749 for other in types:
750 if typ != other and typ in other.__mro__:
751 return True
752 return False
753 types = [n for n in types if not is_strict_base(n)]
754 # Subclasses of the ABCs in *types* which are also implemented by
755 # *cls* can be used to stabilize ABC ordering.
756 type_set = set(types)
757 mro = []
758 for typ in types:
759 found = []
760 for sub in typ.__subclasses__():
761 if sub not in bases and issubclass(cls, sub):
762 found.append([s for s in sub.__mro__ if s in type_set])
763 if not found:
764 mro.append(typ)
765 continue
766 # Favor subclasses with the biggest number of useful bases
767 found.sort(key=len, reverse=True)
768 for sub in found:
769 for subcls in sub:
770 if subcls not in mro:
771 mro.append(subcls)
772 return _c3_mro(cls, abcs=mro)
773
774def _find_impl(cls, registry):
775 """Returns the best matching implementation from *registry* for type *cls*.

Callers 1

_find_implFunction · 0.85

Calls 6

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

Tested by

no test coverage detected