Skip to content

Payload conflicts #1979

Closed
Closed

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'

Activity

  1. coolreader18 commented on Jul 4, 2020

    @coolreader18
    Member

    We could probably do something with a TypeId stored in PyClass

  2. arihant2math commented on Mar 25, 2025

    @arihant2math
    Contributor

    This no longer occurs:

    > python tmp.py
    Traceback (most recent call last):
      File "D:\Documents\Programming\GitHub\RustPython\tmp.py", line 1, in <module>
        class Weird(str, int):
            pass
    TypeError: multiple bases have instance lay-out conflict
    
    > ./target/release/rustpython tmp.py
    Traceback (most recent call last):
      File "tmp.py", line 1, in <module>
        class Weird(str, int):
    TypeError: multiple bases have instance layout conflict
    
  3. youknowone commented on Dec 2, 2025

    @youknowone
    Member

    @arihant2math Thank you for checking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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