Skip to content

super().__new__(...) doesn't work #778

Closed

Description

@BenLewis-Seequent

The following doesn't work, but should:

class A(type):
    def __new__(cls):
        super().__new__(cls, "A", (), {})

A()

This complains that type_new is getting too many parameters. The extra parameter is a copy of the class of A, that is getting bound within super(), which is not meant to occur.
The parameters to type.__new__ are <class "A">, <class "A">, "A", (), {}.
Replacing super() with type does solve the problem.

Activity

  1. Lynskylate commented on Aug 15, 2019

    @Lynskylate
    Contributor

    I met same trouble as you when I try to import enum.py. I simplify the code.

    class EnumMeta(type):
        def __new__(metacls, cls, bases, classdict):
            enum_class = super().__new__(metacls, cls, bases, classdict)
            return enum_class
    
    class Test(metaclass=EnumMeta):
        def __init__(self):
            pass
    """
    >>> t = Test() 
    # RustPython Output:
    Traceback (most recent call last):
      File "/tmp/reproduce.py", line 17, in <module>
        class Test(metaclass=EnumMeta):
      File "/tmp/reproduce.py", line 14, in __new__
        enum_class = super().__new__(metacls, cls, bases, classdict)
    TypeError: type() takes 1 or 3 arguments,
    """
  2. windelbouwman commented on Aug 18, 2019

    @windelbouwman
    Contributor
  3. youknowone commented on Jul 17, 2020

    @youknowone
    Member

    Both of the reported code works now.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions