How to Fix TypeError: Cannot create a consistent method resolution order (MRO)

Learn the cause of TypeError: Cannot create a consistent method resolution order (MRO) and how to fix it.

Picture of Nsikak Imoh, author of Macsika Blog
The text How to Fix TypeError: Cannot create a consistent method resolution order (MRO) on a blank image
The text How to Fix TypeError: Cannot create a consistent method resolution order (MRO) on a blank image

Table of Content

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:

# Base class 1
class  Vehicle:
    def  vehicle_info(self):
	    print('Print from Vehicle class')

# Base class 2
class  Maker(Vehicle):
    def  maker_info(self):
	    print('Print from Maker class')
	    
# Child class
class  Car(Vehicle, Maker): 
    def  info(self):
	    print('Print from Car class')
Highlighted code sample.

At first, it might look like a no-brainer.

But if you try it out, you get:

TypeError: Error when calling the metaclass bases
    Cannot create a consistent method resolution
order (MRO) for bases First, Second
Highlighted code sample.

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:

  1. 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.
  2. Every class in the MRO should come before any of its superclasses.
  3. Each ancestor class appears exactly once
  4. A class always appears before its ancestor (“monotonicity”)
  5. Direct parents of the same class should appear in the same order as they are listed in the class definition (“consistent local precedence order”)
  6. If children of class A always appear before children of class B, then A should appear before B (“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")

Highlighted code sample.

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")
Highlighted code sample.

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")
Highlighted code sample.

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.

class Third(Second, First):
    def __init__(self):
        print("third")
Highlighted code sample.

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.

Get the Complete Code of Python Code Snippets on Github.

Connect with me.

Need an engineer on your team to grease an idea, build a great product, grow a business or just sip tea and share a laugh?