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

Nick Coghlan ncoghlan at gmail.com
Mon Nov 20 21:34:10 EST 2017


On 21 November 2017 at 11:09, Neil Girdhar <mistersheik at gmail.com> wrote:
>
> On Sat, Nov 18, 2017 at 9:29 PM Nick Coghlan <ncoghlan at gmail.com> wrote:
>>
>>     >>> 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

> Sorry, I wrote back too quickly.  I meant also to change B's requested MRO
> to be:
>
> (B, S, E, C)
>
> It works with that change.

Right, but once you do that, then the existing resolver is already
able to handle things:

    >>> class C: pass
    ...
    >>> class S(C): pass
    ...
    >>> class E: pass
    ...
    >>> class B(S, E, C): pass
    ...
    >>> class R(E, C): pass
    ...
    >>> class Z(B, R): pass
    ...
    >>>

If you wanted to pick up cases where two classes generate inconsistent
MROs that will prevent mutual subclasses (like "class B(S, E)" vs
"class R(E, C)"), that feels like a job for a type checker, since it
isn't obvious whether it's the definition of B or the definition of R
that should be changed to reorder their MRO.

Cheers,
Nick.

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


More information about the Python-ideas mailing list