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

False positives with dataclasses #5857

Closed
bretttully opened this issue Mar 3, 2022 · 4 comments · Fixed by #7413
Closed

False positives with dataclasses #5857

bretttully opened this issue Mar 3, 2022 · 4 comments · Fixed by #7413
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@bretttully
Copy link

bretttully commented Mar 3, 2022

Bug description

"""repr=False causes false positives"""
import dataclasses


@dataclasses.dataclass(frozen=True)
class ExampleA:
    """Base class"""

    var_a: int


@dataclasses.dataclass(frozen=True, repr=False)
class ExampleB(ExampleA):
    """Child class with no repr"""

    var_b: int = 10


a = ExampleA(1)
assert a.var_a == 1

b = ExampleB(2)
assert b.var_a == 2
assert b.var_b == 10

b = ExampleB(2, 3)  # Too many positional arguments for constructor call
assert b.var_a == 2
assert b.var_b == 3

b = ExampleB(2, var_b=3)  # Unexpected keyword argument 'var_b' in constructor call
assert b.var_a == 2
assert b.var_b == 3

Configuration

No response

Command used

pylint tmp.py

Pylint output

************* Module tmp
tmp.py:26:4: E1121: Too many positional arguments for constructor call (too-many-function-args)
tmp.py:30:4: E1123: Unexpected keyword argument 'var_b' in constructor call (unexpected-keyword-arg)

------------------------------------------------------------------
Your code has been rated at 3.75/10 (previous run: 2.50/10, +1.25)

Expected behavior

No warnings produced

Pylint version

pylint 2.12.2
astroid 2.9.3
Python 3.8.12 | packaged by conda-forge | (default, Jan 30 2022, 23:42:07) 
[GCC 9.4.0]

OS / Environment

Ubuntu 20.04, clean conda env

Additional dependencies

No response

@bretttully bretttully added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Mar 3, 2022
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Mar 3, 2022
@Pierre-Sassoulas
Copy link
Member

Closing as it's been fixed at some point before 2.15.0-dev0 or I can't reproduce.

@Pierre-Sassoulas
Copy link
Member

My bad it still reproduce.

@DanielNoord
Copy link
Collaborator

Fixed in pylint-dev/astroid#1764 😄

@alexey-pelykh
Copy link
Contributor

A false-positive is happening when class is being decorated with a decorator like this:

def my_decorator(cls):
    # Check if called as @my_decorator()
    if cls is None:
        return my_decorator

    # Wrap as a dataclass
    cls = dataclass(
        cls,
        kw_only=True,
        frozen=True,  # NOTE: avoid dataclasses._field_assign using regular assignment
    )

    return cls

Though I'm not sure if such cases can even be handled.

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants