Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused-argument for required argument in __new__ #3670

Closed
mkielg11 opened this issue Jun 8, 2020 · 8 comments · Fixed by #8542 or #9093
Closed

unused-argument for required argument in __new__ #3670

mkielg11 opened this issue Jun 8, 2020 · 8 comments · Fixed by #8542 or #9093
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Milestone

Comments

@mkielg11
Copy link

mkielg11 commented Jun 8, 2020

I have a class where I overload the new() call.

The new __new__() call is required to have the same arguments as the __init__(), so after I make some checks, I call super().__new__(cls). In this, the input arguments are implicitly passed on to __init__().
However, I get the unused-argument pylint warning, as I do not use all the arguments within the __new__().

@PCManticore
Copy link
Contributor

Can you provide an example?

@PCManticore PCManticore added the Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine label Jun 11, 2020
@mkielg11
Copy link
Author

mkielg11 commented Jun 15, 2020

class Example:
    """ Class doc string """
     __instances = dict()

    def __init__(self, name, x, y, z, test=None):
        self.name = name
        self.x = x
        self.y = y
        self.z = z
        if test is not None:
            self.test = test
        else:
            self. test = 'some default'

    def __new__(self, name, x, y, z, test=None):
        if name in self.__instances:
             assert self.__instances[name].x == x
             return self.__instances[name]
        else:
            instance = super(Example, cls).__new__(cls)
            cls.__instances[name] = instance
            return instance

@mkielg11
Copy link
Author

I just wrote the example in the editor to remove application specific details, but the above should illustrate what I meant.

@pwwang
Copy link

pwwang commented Sep 16, 2020

Or to make it simpler:

class Example:
  def __new__(cls, *args, **kwargs):
    # no need to pass args, kwargs here, 
    # but pylint raises unused-argument
    return object.__new__(cls) 

  def __init__(self, *args, **kwargs):
    # args and kwargs were passed implicitly from __new__
    # do object initialization.

@mkielg11
Copy link
Author

Yes, thank you @pwwang, I'll admit that is a better (and simpler) example, thank you :)

@hippo91
Copy link
Contributor

hippo91 commented Sep 17, 2020

Thanks for the reports and example @pwwang and @mkielg11!

@hippo91 hippo91 added Bug 🪲 and removed Needs reproduction 🔍 Need a way to reproduce it locally on a maintainer's machine labels Sep 17, 2020
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Bug 🪲 labels Jun 30, 2022
@clavedeluna
Copy link
Collaborator

I wonder if we can just do a special case for __new__ here, since it appears that even Pycharm never marks args to it as unused (in gray) while for __init__ it does

Screen Shot 2022-11-08 at 12 43 44 PM
Screen Shot 2022-11-08 at 12 44 06 PM

@Tsjerfell
Copy link

Tsjerfell commented Apr 5, 2023

Hi! I'm running into this issue now, and I'm just wondering if this is solved? Or do you have to disable the warning for all __new__ functions? (Not that you need to redefine __new__ too often)

Emplis added a commit to Emplis/pylint that referenced this issue Apr 5, 2023
One of the check for unused arguments looks for unused arguments in
`__new`. Problem, `__new__` have to match the arguments of `__init__`
even if it does not use them.

This behavior was introduced in the following commit:
pylint-dev@ebb64fc.
There is no details on why `__new__` has been added to the list.

As it's done for special methods with fixed number of parameters, there
should not be unused arguments check for `__new__`, I removed it from
the tested values.

Fixes pylint-dev#3670
Emplis added a commit to Emplis/pylint that referenced this issue Apr 5, 2023
One of the check for `unused-argument` looks for unused arguments in
`__new__`. Problem, `__new__` have to match the arguments of `__init__`
even if it does not use them.

This behavior was introduced in the following commit:
pylint-dev@ebb64fc.
There is no details on why `__new__` has been added to the list.

As it's done for special methods with fixed number of parameters, there
should not be unused arguments check for `__new__`, I removed it from
the checked methods.

Fixes pylint-dev#3670
Emplis added a commit to Emplis/pylint that referenced this issue Apr 5, 2023
One of the check for `unused-argument` looks for unused arguments in
`__new__`. Problem, `__new__` have to match the arguments of `__init__`
even if it does not use them.

This behavior was introduced in the following commit:
pylint-dev@ebb64fc.
There is no details on why `__new__` has been added to the list.

As it's done for special methods with fixed number of parameters, there
should not be unused arguments check for `__new__`, I removed it from
the checked methods.

Fixes pylint-dev#3670
Emplis added a commit to Emplis/pylint that referenced this issue Apr 5, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Fixes pylint-dev#3670
Emplis added a commit to Emplis/pylint that referenced this issue Apr 6, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Fixes pylint-dev#3670
Emplis added a commit to Emplis/pylint that referenced this issue Apr 6, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Fixes pylint-dev#3670
Emplis added a commit to Emplis/pylint that referenced this issue Apr 6, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Fixes pylint-dev#3670
Pierre-Sassoulas pushed a commit that referenced this issue Apr 6, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Update `unused-argument` test to include a check for the case of
`__init__` and `__new__` being defined in a class but `__new__` does not
use all of the argument. This is fine because `__new__` must have the
same argument of `__init__`.

Update with a second check in case of `__init__` being not defined in a
class. Then the unused arguments check must be done on `__new__`.

Fixes #3670
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.3 milestone Apr 6, 2023
github-actions bot pushed a commit that referenced this issue Apr 6, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Update `unused-argument` test to include a check for the case of
`__init__` and `__new__` being defined in a class but `__new__` does not
use all of the argument. This is fine because `__new__` must have the
same argument of `__init__`.

Update with a second check in case of `__init__` being not defined in a
class. Then the unused arguments check must be done on `__new__`.

Fixes #3670

(cherry picked from commit 156da64)
Pierre-Sassoulas pushed a commit that referenced this issue Apr 6, 2023
Problem: the special method `__new__` must match the arguments of the
`__init__` method even if `__new__` method does not use them. This
generate `unused-argument` for the `__new__` method.

Fix: the unused arguments check should not be done on the `__new__`
method if the `__init__` method is defined in the same class.

Update `unused-argument` test to include a check for the case of
`__init__` and `__new__` being defined in a class but `__new__` does not
use all of the argument. This is fine because `__new__` must have the
same argument of `__init__`.

Update with a second check in case of `__init__` being not defined in a
class. Then the unused arguments check must be done on `__new__`.

Fixes #3670

(cherry picked from commit 156da64)

Co-authored-by: Théo Battrel <theo.util@protonmail.ch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
7 participants