Home > Mobile >  TypeError when inheriting from both OSError and AttributeError
TypeError when inheriting from both OSError and AttributeError

Time:10-17

It is no longer possible to engender an exception type that derives from both OSError and AttributeError, In Python 3.10.

This has worked in Python 3.9.

I don't optically discern anything in changelog/release notes that would suggest it being intentional.

Python 3.9:

Python 3.9.2 [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information.

>>> class C(OSError, AttributeError): pass
... 
>>> C
<class '__main__.C'>

Python 3.10:

Python 3.10.0 (default, Oct  4 2021, 00:00:00) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.

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

CodePudding user response:

There is no guarantee that multiple build-in, unrelated exceptions can be inherited and this is not supported. And this is not unique to this case. For example:

>>> class A(StopIteration, OSError):
...    ...
...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: multiple bases have instance lay-out conflict

>>> class A(SyntaxError, OSError):
...    pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: multiple bases have instance lay-out conflict
>>> class A(ModuleNotFoundError, OSError):
...    ...
...

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: multiple bases have instance lay-out conflict
  • Related