[Bug]: After `obj.__class__ = C`, calling `C(arg)` for a class with a metaclass such as `ABCMeta` fails with `object.__new__() takes exactly one argument` from the third call on

Abierto
#1,342 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
22/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
java, python
Área
backend

Línea de trabajo

The failure sits in how GraalPy resolves __new__ after __class__ is reassigned, so the search starts at the __class__ setter and the type call path for classes with a metaclass. Run the reproduction script from the issue; the fault appears from the third call at the cls(*args) line. Done when all four rounds print C and the metaclass-as-type-subclass variant also passes.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Describe the bug

C inherits a __new__ that accepts extra arguments and has a metaclass (here abc.ABCMeta). Once some object's __class__ has been set to C, a call site that has already called both the base class and C starts raising TypeError: object.__new__() takes exactly one argument (the type to instantiate) for C(arg), as if C had no __new__ of its own. The first two rounds work; every later round fails. CPython constructs the object every time.

Operating system

macOS

CPU architecture

ARM64

GraalPy version

25.6.5.1 (Python 3.13.14), community build from the GitHub release. Same on master 2e574af1d8.

JDK version

No response

Context configuration

No response

Steps to reproduce
import abc

class Base:
    def __new__(cls, *args):
        return object.__new__(cls)
    def __init__(self, *args):
        pass

class C(Base, metaclass=abc.ABCMeta):
    pass

def make(cls, *args):
    return cls(*args)

for i in range(4):
    try:
        make(Base, "a")
        Base().__class__ = C
        print(i, type(make(C, "b")).__name__)
    except TypeError as e:
        print(i, "TypeError:", e)

GraalPy:

0 C
1 C
2 TypeError: object.__new__() takes exactly one argument (the type to instantiate)
3 TypeError: object.__new__() takes exactly one argument (the type to instantiate)
Expected behavior

CPython 3.13.18:

0 C
1 C
2 C
3 C
Additional context

All three ingredients are needed on GraalPy:

  • Without the __class__ assignment, all four rounds print C.
  • With class C(Base) (metaclass type), all four rounds print C. Any type subclass as metaclass fails, not only ABCMeta.
  • Without the make(Base, "a") call, so that the call site only sees C, all four rounds print C.

The same happens when the call is type(self)(*args) inside a method and self is the object whose __class__ was assigned. The traceback ends at the cls(*args) call, not inside Base.__new__, so the __new__ that gets called is object.__new__ itself.

Lenguaje dominante
Python
Estrellas
1.7k
Forks
167
Merge medio
3 d 11 h
PR fusionados (30 d)
68

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de oracle/graalpython

Todos los issues de oracle/graalpython

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.