Skip to content

False positive unused-argument in dataclass __new__ #9843

New issue

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

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

Already on GitHub? # to your account

Open
michele-riva opened this issue Jul 30, 2024 · 0 comments
Open

False positive unused-argument in dataclass __new__ #9843

michele-riva opened this issue Jul 30, 2024 · 0 comments
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling

Comments

@michele-riva
Copy link

michele-riva commented Jul 30, 2024

Bug description

Looks like #3670 is not quite solved for classes that dynamically define an __init__. While this is probably complex to handle in the general case, dataclasses are a common use case that could be handled in a specific manner. Here a minimal example:

"""File unused_arg_dataclass.py for testing unused-argument on __new__ for dataclasses."""

from dataclasses import dataclass

@dataclass
class Data:  # pylint: disable=missing-class-docstring
    arg: ...

    def __new__(cls, *args, **kwargs):   # unused-argument for args, kwargs
        instance = super().__new__(cls)
        # ... do stuff with instance without using args, kwargs...
        return instance

The current solution for not emitting unused-argument for __new__ (added in #8542) is at

# Care about functions with unknown argument (builtins)
if name in argnames:
if node.name == "__new__":
is_init_def = False
# Look for the `__init__` method in all the methods of the same class.
for n in node.parent.get_children():
is_init_def = hasattr(n, "name") and (n.name == "__init__")
if is_init_def:
break
# Ignore unused arguments check for `__new__` if `__init__` is defined.
if is_init_def:
return
self._check_unused_arguments(name, node, stmt, argnames, nonlocal_names)

Configuration

No response

Command used

pylint unused_arg_dataclass.py

Pylint output

************* Module unused_arg_dataclass
unused_arg_dataclass.py:9:0: W0613: Unused argument 'args' (unused-argument)
unused_arg_dataclass.py:9:0: W0613: Unused argument 'kwargs' (unused-argument)

Expected behavior

No unused-argument for dataclass __new__.

Pylint version

pylint 3.2.6
astroid 3.2.4
Python 3.12.0

OS / Environment

No response

Additional dependencies

No response

@michele-riva michele-riva added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jul 30, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling
Projects
None yet
Development

No branches or pull requests

1 participant