Skip to content

A class with __abstractmethods__ can be instantiated once its call is specialized to CALL_ALLOC_AND_ENTER_INIT #162600

Backlog

Description

@Robertboy18

Bug report

Bug description:

For a plain class with a Python __init__ and a non-empty __abstractmethods__, the first evaluation of A() raises TypeError: Can't instantiate abstract class, and the later ones of the same call return instances:

class A:
    def __init__(self):
        pass
A.__abstractmethods__ = frozenset({"f"})
def make():
    try:
        return type(A()).__name__
    except TypeError:
        return "TypeError"
print([make() for i in range(4)])

CPython 3.15.2 and main (639c34b):

['TypeError', 'A', 'A', 'A']

On 3.12 every call raises. specialize_class_call (Python/specialize.c) picks CALL_ALLOC_AND_ENTER_INIT, which skips object_new and so its check of Py_TPFLAGS_IS_ABSTRACT; checking the flag there would keep the error. It needs __abstractmethods__ set by hand (classes with ABCMeta are not specialized this way), but the result of the same expression shouldn't change after a few calls. I found it while writing a Lean spec of Python.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux

Activity

  1. added
    AI-needs-explanation
    on Nov 25, 2026
  2. bedevere-app commented on Nov 25, 2026

    @bedevere-app

    Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

    This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.

    Please read our guidelines for using AI tools.

  3. added
    type-bug
    interpreter-core
    pending
    and removed
    AI-needs-explanation
    on Nov 25, 2026
  4. picnixz commented on Nov 25, 2026

    @picnixz
    Member

    It needs __abstractmethods__ set by hand (classes with ABCMeta are not specialized this way), but the result of the same expression shouldn't change after a few calls.

    I believe this is where we disagree. I don't think this is meant to be the case. Users are not meant to write this by hand unless they know what they are doing and thus I believe it's something that doesn't qualify as a real bug.

    OTOH, if the fix does not reduce performance, we can accept it. However we definitely don't want to slow down others just because of singular cases. How much performance change are we having if we were to to fix this? (on a PGO+LTO build with say pydantic models and dataclasses, as well as regular simple classes).

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

    interpreter-corependingtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions