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 result
(cls, abcs=None)
| 685 | del seq[0] |
| 686 | |
| 687 | def _c3_mro(cls, abcs=None): |
| 688 | """Computes the method resolution order using extended C3 linearization. |
| 689 | |
| 690 | If no *abcs* are given, the algorithm works exactly like the built-in C3 |
| 691 | linearization used for method resolution. |
| 692 | |
| 693 | If given, *abcs* is a list of abstract base classes that should be inserted |
| 694 | into the resulting MRO. Unrelated ABCs are ignored and don't end up in the |
| 695 | result. The algorithm inserts ABCs where their functionality is introduced, |
| 696 | i.e. issubclass(cls, abc) returns True for the class itself but returns |
| 697 | False for all its direct base classes. Implicit ABCs for a given class |
| 698 | (either registered or inferred from the presence of a special method like |
| 699 | __len__) are inserted directly after the last ABC explicitly listed in the |
| 700 | MRO of said class. If two implicit ABCs end up next to each other in the |
| 701 | resulting MRO, their ordering depends on the order of types in *abcs*. |
| 702 | |
| 703 | """ |
| 704 | for i, base in enumerate(reversed(cls.__bases__)): |
| 705 | if hasattr(base, '__abstractmethods__'): |
| 706 | boundary = len(cls.__bases__) - i |
| 707 | break # Bases up to the last explicit ABC are considered first. |
| 708 | else: |
| 709 | boundary = 0 |
| 710 | abcs = list(abcs) if abcs else [] |
| 711 | explicit_bases = list(cls.__bases__[:boundary]) |
| 712 | abstract_bases = [] |
| 713 | other_bases = list(cls.__bases__[boundary:]) |
| 714 | for base in abcs: |
| 715 | if issubclass(cls, base) and not any( |
| 716 | issubclass(b, base) for b in cls.__bases__ |
| 717 | ): |
| 718 | # If *cls* is the class that introduces behaviour described by |
| 719 | # an ABC *base*, insert said ABC to its MRO. |
| 720 | abstract_bases.append(base) |
| 721 | for base in abstract_bases: |
| 722 | abcs.remove(base) |
| 723 | explicit_c3_mros = [_c3_mro(base, abcs=abcs) for base in explicit_bases] |
| 724 | abstract_c3_mros = [_c3_mro(base, abcs=abcs) for base in abstract_bases] |
| 725 | other_c3_mros = [_c3_mro(base, abcs=abcs) for base in other_bases] |
| 726 | return _c3_merge( |
| 727 | [[cls]] + |
| 728 | explicit_c3_mros + abstract_c3_mros + other_c3_mros + |
| 729 | [explicit_bases] + [abstract_bases] + [other_bases] |
| 730 | ) |
| 731 | |
| 732 | def _compose_mro(cls, types): |
| 733 | """Calculates the method resolution order for a given class *cls*. |