-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError when inheriting from both OSError and AttributeError #89627
Comments
In Python 3.10 it is no longer possible to create an exception type that inherits from both OSError and AttributeError. This has worked in Python 3.9. Behavior in Python 3.9: Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[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'> Behavior in 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 My (very) wild guess is this being related to https://bugs.python.org/issue38530 |
You are correct. To be more precise, it's caused by #61060. The commit introduced PyAttributeErrorObject struct. Since AttributeError and OSError now have an incompatible memory layout, it is no longer possible to create a subclass that has AttributeError and OSError as parent classes. |
Unfortunately there isn't much we can do here other than document this :( |
Also, just to clarify something: 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 |
Suggestion: At the end of https://docs.python.org/3/bugs.html, add "Creating a subclass that inherits from multiple exceptions may not work and the potential conflicts may change in new versions." |
Hummm, I am not sure that page is the most adecuate for this, no? That is how to deal with bugs in general, not about specific bugs. |
More specific would be "Inheriting from multiple exceptions may fail due to instance layout conflicts. Such conflicts may depend on the Python version." This would effectively say "Don't bother reporting layout conflicts -- we know about them and they are not bugs." while not discouraging reports of other problems. |
Marking this as "wont fix" since only documentation was updated. Thanks for reporting, Marek! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: