Python 2.7 warning: __init__ not compatible to __new__
Asked Answered
I

2

11

I have a class defined in Python 2.7 like this:

from future.builtins import object


class Point(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

In PyCharm, this gives a warning in the __init__ line:

Signature is not compatible to __new__.

I don't understand what this warning is telling me. Can someone give an example where this warning would rightfully catch an error or can this warning be turned off?

There's a PyCharm thread for this, but it doesn't help me: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000254530-PyCharm-init-Signature-is-not-compatible-to-new-

Injudicious answered 31/8, 2018 at 8:22 Comment(4)
The only future.builtins module I could find does not list an object in its API, so what exactly are you importing there? Do you get the same warning if you use the standard object? Anyway, the reason behind this warning is that if a class defines both __new__ and __init__, they have to have compatible parameters, since the same parameters will normally be passed to both (the one exception being the obscure possibility that __new__() returns an instance of an unrelated class).Koerner
"The only future.builtins module I could find does not list an object in its API": This one github.com/PythonCharmers/python-future/blob/… "Anyway, the reason behind this warning is that if a class defines both new and init" that makes sense theoretically, but as far as I see no one overwrites __new__.Injudicious
I guess you are simply trying to create a new style class, which became standard in Python 3. For that you may simply use object without need to import anythingFritts
@Injudicious If no one overwrites __new__, how is it supposed to know that it should accept arguments x and y to be compatible with your __init__? Like guidot wrote, you can use object to define new-style classes without importing anything. I suspect that your future import causes these problems.Ryswick
I
2

I suffered from the same problem and found the solution in here.

As discussed in the above link, a direct solution is to remove your Pycharm config directory ("/home/username/.PyCharm2018.3" for me). However, it would remove all your other configs too. Finally, I just fix the problem by removing the rule from inspections. You can find the rule in the Pycharm Setting window (see the figure as follow). enter image description here

Itemized answered 15/1, 2021 at 13:53 Comment(0)
H
1

This is a PyCharm bug; you can disable the warning via Xiong-Hui's post. Basically __new__ is a method that is called before __init__ with the same arguments when a class is constructed, so their signatures must be compatible (both functions must be able to be invoked with the same arguments) or the class cannot be instantiated. For example, here is a code snippet for which PyCharm correctly applies the warning:

class Test(object):
    def __new__(cls, arg1):
        return super().__new__(cls)

    def __init__(self):
        pass

Attempting to create the class with Test() will throw a TypeError since __new__ expects an argument, but creating the class with Test('something') will also throw a TypeError since __init__ doesn't expect any arguments, making it impossible to construct the class. Normally this is never an issue because the default implementation of __new__ in object accepts an arbitrary number of arguments, but if you define __new__ yourself you will need to be careful that the signatures remain compatible so the class can actually be constructed, which is the purpose of the warning.

Handcraft answered 11/4, 2021 at 0:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.