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

Function _c3_mro

Lib/functools.py:762–805  ·  view source on GitHub ↗

Computes the method resolution order using extended C3 linearization. If no *abcs* are given, the algorithm works exactly like the built-in C3 linearization used for method resolution. If given, *abcs* is a list of abstract base classes that should be inserted into the resulting MR

(cls, abcs=None)

Source from the content-addressed store, hash-verified

760 del seq[0]
761
762def _c3_mro(cls, abcs=None):
763 """Computes the method resolution order using extended C3 linearization.
764
765 If no *abcs* are given, the algorithm works exactly like the built-in C3
766 linearization used for method resolution.
767
768 If given, *abcs* is a list of abstract base classes that should be inserted
769 into the resulting MRO. Unrelated ABCs are ignored and don't end up in the
770 result. The algorithm inserts ABCs where their functionality is introduced,
771 i.e. issubclass(cls, abc) returns True for the class itself but returns
772 False for all its direct base classes. Implicit ABCs for a given class
773 (either registered or inferred from the presence of a special method like
774 __len__) are inserted directly after the last ABC explicitly listed in the
775 MRO of said class. If two implicit ABCs end up next to each other in the
776 resulting MRO, their ordering depends on the order of types in *abcs*.
777
778 """
779 for i, base in enumerate(reversed(cls.__bases__)):
780 if hasattr(base, '__abstractmethods__'):
781 boundary = len(cls.__bases__) - i
782 break # Bases up to the last explicit ABC are considered first.
783 else:
784 boundary = 0
785 abcs = list(abcs) if abcs else []
786 explicit_bases = list(cls.__bases__[:boundary])
787 abstract_bases = []
788 other_bases = list(cls.__bases__[boundary:])
789 for base in abcs:
790 if issubclass(cls, base) and not any(
791 issubclass(b, base) for b in cls.__bases__
792 ):
793 # If *cls* is the class that introduces behaviour described by
794 # an ABC *base*, insert said ABC to its MRO.
795 abstract_bases.append(base)
796 for base in abstract_bases:
797 abcs.remove(base)
798 explicit_c3_mros = [_c3_mro(base, abcs=abcs) for base in explicit_bases]
799 abstract_c3_mros = [_c3_mro(base, abcs=abcs) for base in abstract_bases]
800 other_c3_mros = [_c3_mro(base, abcs=abcs) for base in other_bases]
801 return _c3_merge(
802 [[cls]] +
803 explicit_c3_mros + abstract_c3_mros + other_c3_mros +
804 [explicit_bases] + [abstract_bases] + [other_bases]
805 )
806
807def _compose_mro(cls, types):
808 """Calculates the method resolution order for a given class *cls*.

Callers 1

_compose_mroFunction · 0.85

Calls 10

enumerateFunction · 0.85
reversedFunction · 0.85
hasattrFunction · 0.85
lenFunction · 0.85
listClass · 0.85
issubclassFunction · 0.85
_c3_mergeFunction · 0.85
anyFunction · 0.70
appendMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected