[Python-ideas] Consider (one day) adding an inheritance order class precedence mechanism

Nick Coghlan ncoghlan at gmail.com
Sat Nov 18 21:29:46 EST 2017

On 19 November 2017 at 06:56, Neil Girdhar <mistersheik at gmail.com> wrote:
> Would you mind explaining why it's necessary for C3 to complain?
> In:
> S < C
> B < S, E
> R < E, C
> Z < B, R
> If Z is told to have MRO:
> (Z, B, S, R, E, C)
> then there are no conflicts with any base classes.

I don't actually know what C3 allows in principle, I only know that
CPython's resolver still complains in practice:

    >>> class C: pass
    >>> class S(C): pass
    >>> class E: pass
    >>> class B(S, E): pass
    >>> class R(E, C): pass
    >>> class Z(B, S, R, E, C): pass
    Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
    TypeError: Cannot create a consistent method resolution order
(MRO) for bases C, E

I think the problem is that the resolver isn't looking at the declared
bases of "B", and "R", it's looking at their full MRO:

    >>> B.__mro__
    (<class '__main__.B'>, <class '__main__.S'>, <class '__main__.C'>,
<class '__main__.E'>, <class 'object'>)
    >>> R.__mro__
    (<class '__main__.R'>, <class '__main__.E'>, <class '__main__.C'>,
<class 'object'>)

Changing the heuristics used to generate B's MRO such that "C" and "E"
appeared in the opposite order wouldn't really help, since that would
just flip the problematic case to be the "R(C, E)" declaration.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-ideas mailing list