Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Payload conflicts #1979

Open
Open
@BenLewis-Seequent

Description

@BenLewis-Seequent

Currently you are allowed to have the following:

class Weird(str, int):
    pass

But this causes issues using using instances of this class as both str and int expect the payload to be PyString and PyInt respectively, but the object can only have a single payload.

>>>>> 2 + Weird()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: Unexpected payload for type "Weird"

CPython deals with this by raising an error during class construction time:

>>> class Weird(str, int):
...     pass

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

I feel we should do a similar thing by detecting when bases have different payloads.

This also comes up in __class__ assignment, the following is CPython behavior:

>>> class A(str): pass
... 
>>> class B(int): pass
... 
>>> A().__class__ = B
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __class__ assignment: 'B' object layout differs from 'A'

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions