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)
| 826 | ) |
| 827 | |
| 828 | def _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 | |
| 870 | def _find_impl(cls, registry): |
| 871 | """Returns the best matching implementation from *registry* for type *cls*. |
no test coverage detected
searching dependent graphs…