[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

Ouverte
#1,342 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
22/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
java, python
Domaine
backend

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
Python
Étoiles
1.7k
Forks
167
Merge moyen
3 j 11 h
PR mergées (30 j)
68

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de oracle/graalpython

Toutes les issues de oracle/graalpython

Issues similaires

Plus d'issues Python

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.