Skip to content

Abstract class inheritance regression in Python 3.13 #127967

@txgk

Description

@txgk

Bug report

Bug description:

Good day to you, dear Python maintainers! I have a little issue with the recent Python release, 3.13. Here's a snippet:

import ctypes

class EnumMetaclass(type(ctypes.c_uint)):
  def __new__(metaclass, name, bases, cls_dict):
    cls = type(ctypes.c_uint).__new__(metaclass, name, bases, cls_dict)
    if name == 'Enum':
      return cls
    for i, value in enumerate(cls_dict['_values_']):
      setattr(cls, value, cls.from_param(i))
    return cls

class Enum(ctypes.c_uint, metaclass=EnumMetaclass):
  @classmethod
  def from_param(cls, param):
    print("Calling method 'from_param' of", cls)
    return cls(param)
  def __repr__(self):
    return self._values_[self.value]

class NodeType(Enum):
  _values_ = ['DOCUMENT', 'ELEMENT', 'TEXT']

def main():
  print(NodeType(1))

if __name__ == "__main__":
  main()

When run with Python 3.12.7, I get the following output:

Calling method 'from_param' of <class '__main__.NodeType'>
Calling method 'from_param' of <class '__main__.NodeType'>
Calling method 'from_param' of <class '__main__.NodeType'>
ELEMENT

When run with Python 3.13.1, I get the following output:

Calling method 'from_param' of <class '__main__.NodeType'>
Traceback (most recent call last):
  File "/home/grisha/test.py", line 20, in <module>
    class NodeType(Enum):
      _values_ = ['DOCUMENT', 'ELEMENT', 'TEXT']
  File "/home/grisha/test.py", line 9, in __new__
    setattr(cls, value, cls.from_param(i))
                        ~~~~~~~~~~~~~~^^^
  File "/home/grisha/test.py", line 16, in from_param
    return cls(param)
TypeError: abstract class

As you can see, new TypeError detection triggers where it presumably mustn't have, because it's perfectly correct to call from_param from NodeType object.

I checked that this problem persists in both 3.13.0 and 3.13.1.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixestopic-ctypestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions