Skip to content
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

Closed
marmarek mannequin opened this issue Oct 13, 2021 · 11 comments
Closed

TypeError when inheriting from both OSError and AttributeError #89627

marmarek mannequin opened this issue Oct 13, 2021 · 11 comments
Labels
3.10 only security fixes 3.11 bug and security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@marmarek
Copy link
Mannequin

marmarek mannequin commented Oct 13, 2021

BPO 45464
Nosy @terryjreedy, @tiran, @ambv, @marmarek, @pablogsal, @miss-islington
PRs
  • bpo-45464: Add a warning about subclassing built-in exceptions #28995
  • bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile #29094
  • [3.10] bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile (GH-29094) #29104
  • [3.9] bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile (GH-29094) #29105
  • 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:

    assignee = None
    closed_at = <Date 2021-10-20.18:51:34.406>
    created_at = <Date 2021-10-13.20:05:16.904>
    labels = ['interpreter-core', '3.11', 'type-bug', '3.10', 'docs']
    title = 'TypeError when inheriting from both OSError and AttributeError'
    updated_at = <Date 2021-10-20.18:51:34.405>
    user = 'https://github.com/marmarek'

    bugs.python.org fields:

    activity = <Date 2021-10-20.18:51:34.405>
    actor = 'lukasz.langa'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2021-10-20.18:51:34.406>
    closer = 'lukasz.langa'
    components = ['Documentation', 'Interpreter Core']
    creation = <Date 2021-10-13.20:05:16.904>
    creator = 'marmarek'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45464
    keywords = ['patch', '3.10regression']
    message_count = 11.0
    messages = ['403870', '403906', '403914', '403918', '404075', '404107', '404469', '404498', '404518', '404519', '404520']
    nosy_count = 7.0
    nosy_names = ['terry.reedy', 'christian.heimes', 'docs@python', 'lukasz.langa', 'marmarek', 'pablogsal', 'miss-islington']
    pr_nums = ['28995', '29094', '29104', '29105']
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45464'
    versions = ['Python 3.10', 'Python 3.11']

    @marmarek
    Copy link
    Mannequin Author

    marmarek mannequin commented Oct 13, 2021

    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.
    I don't see anything in changelog/release notes that would suggest it being intentional.

    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

    @marmarek marmarek mannequin added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Oct 13, 2021
    @tiran
    Copy link
    Member

    tiran commented Oct 14, 2021

    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.

    @tiran tiran added 3.11 bug and security fixes labels Oct 14, 2021
    @pablogsal
    Copy link
    Member

    Unfortunately there isn't much we can do here other than document this :(

    @pablogsal
    Copy link
    Member

    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

    @terryjreedy
    Copy link
    Member

    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."

    @terryjreedy terryjreedy added the docs Documentation in the Doc dir label Oct 16, 2021
    @terryjreedy terryjreedy added the docs Documentation in the Doc dir label Oct 16, 2021
    @pablogsal
    Copy link
    Member

    Suggestion: At the end of https://docs.python.org/3/bugs.html, add

    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.

    @terryjreedy
    Copy link
    Member

    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.

    @ambv
    Copy link
    Contributor

    ambv commented Oct 20, 2021

    New changeset dff0b71 by Łukasz Langa in branch 'main':
    bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile (GH-29094)
    dff0b71

    @ambv
    Copy link
    Contributor

    ambv commented Oct 20, 2021

    New changeset b2a9899 by Miss Islington (bot) in branch '3.10':
    bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile (GH-29094) (GH-29104)
    b2a9899

    @ambv
    Copy link
    Contributor

    ambv commented Oct 20, 2021

    New changeset 427ab12 by Miss Islington (bot) in branch '3.9':
    bpo-45464: [doc] Explain that subclassing multiple exceptions is fragile (GH-29094) (GH-29105)
    427ab12

    @ambv
    Copy link
    Contributor

    ambv commented Oct 20, 2021

    Marking this as "wont fix" since only documentation was updated. Thanks for reporting, Marek!

    @ambv ambv closed this as completed Oct 20, 2021
    @ambv ambv closed this as completed Oct 20, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 bug and security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants