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)
| 760 | del seq[0] |
| 761 | |
| 762 | def _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 | |
| 807 | def _compose_mro(cls, types): |
| 808 | """Calculates the method resolution order for a given class *cls*. |