Table of Content
- What causes the Error “TypeError: Cannot create a consistent method resolution order (MRO)”
- How to Fix TypeError: Cannot create a consistent method resolution order (MRO)
- Wrap Off
In this post, you will learn the cause of the error “TypeError: Cannot create a consistent method resolution order (MRO)” and how to fix it.
I was recently writing a tutorial for object-oriented programming in Python when I stumbled upon this error: Cannot create a consistent method resolution order (MRO).
Ordinarily, this could have taken me a few seconds to figure out. However, I was lost in thought for about 10 minutes!
This was my code:
At first, it might look like a no-brainer.
But if you try it out, you get:
Notice the error in the code above?
To fix it, we first need to know why this error happens?
What causes the Error “TypeError: Cannot create a consistent method resolution order (MRO)”
This error occurs because the method resolution order (MRO) of the multiple inheritance is not consistent with how Python executes class hierarchy.
Python computes the MRO of multiple inheritance using the C3 linearization algorithm.
Python needs to decide in which order to search through (direct and indirect) base classes when looking up an instance attribute/method.
It does this by linearizing the inheritance graph, that is by converting the graph of base classes into a sequence, using an algorithm called C3 or MRO.
The C3 algorithm is a unique algorithm that achieves several desirable properties.
For the methods of inherited classes to be resolved consistently, the MRO should satisfy these constraints:
- If a class inherits from multiple superclasses, the ones it lists earlier in the superclass list should come earlier in the MRO than the ones it lists later.
- Every class in the MRO should come before any of its superclasses.
- Each ancestor class appears exactly once
- A class always appears before its ancestor (“monotonicity”)
- Direct parents of the same class should appear in the same order as they are listed in the class definition (“consistent local precedence order”)
- If children of class
A
always appear before children of classB
, thenA
should appear beforeB
(“consistent extended precedence order”)
Take the code block below as an example:
class First(object):
def __init__(self):
print("first")
class Second(First):
def __init__(self):
print("second")
class Third(First, Second):
def __init__(self):
print("third")
The proposed hierarchy of the inherited superclasses does not have any possible ordering meeting these constraints.
Because the class Third
is defined to inherit from the class First
before Second
, First
should come before Second
in the MRO.
But because Second
inherits from First
, Second
should come before First
in the MRO.
This contradiction cannot be reconciled in Python.
So, how do we fix it?
How to Fix TypeError: Cannot create a consistent method resolution order (MRO)
There are three ways to fix the code:
1. Make the Second Base class not inherit from the first
If you want to use multiple inheritance from the Third
class, Simply remove the first class from the second by changing Second(First)
to Second(object)
or just Second
.
class First(object):
def __init__(self):
print("first")
class Second(object):
def __init__(self):
print("second")
class Third(First, Second):
def __init__(self):
print("third")
2. Remove the First class from the inherited list of classes
You may want to make it a multi-level inheritance.
To do this, simply, remove the First
class from the list of classes inherited in the Third
.
class First(object):
def __init__(self):
print("first")
class Second(object):
def __init__(self):
print("second")
class Third(Second):
def __init__(self):
print("third")
For my own error, I went with the first option, since my demo involved multiple inheritance.
3. Rearrange the order of Class Inheritance
One way to resolve the issue and still maintain your code is to rearrange the order the class is inherited.
By importing the Second
class before the First
.
Wrap Off
In this lesson, we learned the cause of the error “TypeError: Cannot create a consistent method resolution order (MRO)” and how to fix it.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.
Please share this post and for more insightful posts on business, technology, engineering, history, and marketing, subscribe to our newsletter.