TypeError: multiple bases have instance lay-out conflict

Hi, I’m trying to figure out all pairs of builtin types that have instance lay-out conflicts.

Is that easy to determine from the CPython sources? I tried grepping for the error message and I can’t see exactly where it’s coming from.

Have you tried subclassing all pairs, and seeing when you get an exception? That should take a few seconds at most.
Getting a list of all builtin types sounds like a harder problem.

1 Like

Great idea! I’m not sure how to generate all the types, though.

I don’t think there’s an easy way.
Search for PyType_Spec and PyTypeObject. That’ll give you thousands of false positives, so you’ll need to figure out how to filter out enough to make the rest manageable.

1 Like

Thanks, that’s enough to go on.

I don’t actually need to catch all possible such cases, just enough to be useful (for static analysis). So it’s OK to be somewhat ad hoc.

1 Like

Or add logging to PyType_Ready and run the test suite.

What is a lay-out conflict? Do you have an example?

I don’t know if it would help, but there is a list of static built-in types here: static_types array. I tried to write the base class for each type.

1 Like
>>> class Nope(str, int):
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: multiple bases have instance lay-out conflict

You can do object.__subclasses__() in a program that doesn’t have too much other code imported. In 3.11 I get 212 classes that way. That won’t include extension types in non-builtin modules though.

1 Like

You can look at the __module__ attribute of the type to check that it is a builtin type. It is up to you where you draw a line between builtin and extension types.

1 Like