__new__ method giving error object.__new__() takes exactly one argument (the type to instantiate)

why the following code is giving error?

class Foo:
    def __new__(cls, *args, **kwargs):
        print("Creating Instance")
        instance = super(Foo, cls).__new__(cls,*args, **kwargs)
        return instance

    def __init__(self, a, b):
        self.a = a
        self.b = b

z= Foo(2,3)

it is giving the following error

TypeError: object.__new__() takes exactly one argument (the type to instantiate)

Solution 1:

    instance = super(Foo, cls).__new__(cls,*args, **kwargs)

is correct. However, you are responsible for first removing arguments that your class introduces, so that when object.__new__ is ultimately called, both *args and **kwargs are empty.

Your code should be something like

class Foo:
    def __new__(cls, a, b, *args, **kwargs):
        print("Creating Instance")
        instance = super(Foo, cls).__new__(cls, *args, **kwargs)
        return instance

    def __init__(self, a, b, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.a = a
            self.b = b

This definition removes your new parameters a and b from args before passing it on to whoever is next on the MRO. Likewise for __init__.

Solution 2:

object.__new__() signature is (*args, **kwargs), you may check this by using inspect.signature function.

But why then you have this error? TLDR: because you defined custom __new__ method.

Small research

All tests were done on Python 3.9.1.


Consider next class.

class MyClass:
    def __init__(self): pass

Let's call object.__new__() on it:

>>> object.__new__(MyClass, *range(10), **{f'a{i}': i for i in range(10)})
<__main__.MyClass object at 0x000001E7B15B3550>

No problems at all. This class has only custom __init__ and no custom __new__. Now try to do the same call for your Foo:

>>> object.__new__(Foo, *range(10), **{f'a{i}': i for i in range(10)})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

An exception about object.__new__(). This class has both custom __init__ and __new__. You will see the same error when only custom __new__ is defined:

>>> class OnlyNew:
...     def __new__(cls, *args, **kwargs): return super().__new__(cls)
>>> object.__new__(OnlyNew, *range(10), **{f'a{i}': i for i in range(10)})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

Let's check a class with no custom __init__ and __new__.

>>> class A: pass
>>> object.__new__(A, *range(10), **{f'a{i}': i for i in range(10)})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: A() takes no arguments

Absolutely different error.


Let's check how it works with inheritance. Derive from A and define __init__.

>>> class B(A):
...     def __init__(self): pass
>>> object.__new__(B, *range(10), **{f'a{i}': i for i in range(10)})
<__main__.B object at 0x000001E7B15D23A0>

Derive from MyClass and define nothing.

>>> class MC(MyClass): pass
>>> object.__new__(MC, *range(10), **{f'a{i}': i for i in range(10)})
<__main__.MC object at 0x000001E7B15D2CA0>

Derive from MyClass and define __new__.

>>> class Sub(MyClass):
    def __new__(cls, *args, **kwargs): return super().__new__(cls)
>>> object.__new__(Sub, *range(10), **{f'a{i}': i for i in range(10)})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

Derive from Foo and define nothing.

>>> class F(Foo): pass
>>> object.__new__(F, *range(10), **{f'a{i}': i for i in range(10)})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

And now let's look on an absolutely exotic case:

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


class Sub(Base):
    def __init__(self): pass
    __new__ = object.__new__


class Free:
    def __init__(self): pass
    __new__ = object.__new__
>>> object.__new__(Free, *range(10), **{f'a{i}': i for i in range(10)})
<__main__.Free object at 0x000001E7B15C5A90>
>>> object.__new__(Sub, *range(10), **{f'a{i}': i for i in range(10)})
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: object.__new__() takes exactly one argument (the type to instantiate)

Both Sub and Free do not have a custom __new__ method - in both classes __new__ is object.__new__(). But creating Sub raises an error while creating Free does not. Seems like object.__new__() checks not getattr(A_Class, '__new__', object.__new__) is object.__new__ but all(getattr(cls, '__new__', object.__new__) is object.__new__ for cls in A_Class.mro()).


Conclusion

  1. If a class has custom __new__ in its MRO, calling object.__new__() with >1 arguments raises TypeError.
  2. If a class has only custom __init__ and does not have custom __new__ in its MRO, calling object.__new__() with >1 arguments creates a proper instance.
  3. If a class does not have both custom __init__ and __new__ in its MRO, calling object.__new__() with >1 arguments raises TypeError.