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 keywords-only fields of dataclasses #5767

Closed
NeilGirdhar opened this issue Feb 4, 2022 · 4 comments · Fixed by #7333
Closed

False positives with keywords-only fields of dataclasses #5767

NeilGirdhar opened this issue Feb 4, 2022 · 4 comments · Fixed by #7333
Labels
dataclasses False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.10
Milestone

Comments

@NeilGirdhar
Copy link

NeilGirdhar commented Feb 4, 2022

Bug description

from dataclasses import dataclass, KW_ONLY

@dataclass
class C:
    _: KW_ONLY
    c: int

c = C(c=1)  #  No value for argument '_' in constructor call (no-value-for-parameter)

Pylint version

pylint 2.12.2
astroid 2.9.3
Python 3.10.0 (default, Nov  6 2021, 07:41:13) [Clang 12.0.5 (clang-1205.0.22.9)]
@NeilGirdhar NeilGirdhar added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Feb 4, 2022
@NeilGirdhar
Copy link
Author

Related #3492

@DanielNoord DanielNoord 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 Feb 4, 2022
@Infernio
Copy link

Infernio commented Feb 20, 2022

Happens with the kw_only parameter of dataclasses.field too:

"""Quick test to demonstrate pylint errors when using kw_only dataclass
fields."""
from dataclasses import dataclass, field

@dataclass
class Creature:
    """Defines a creature with one or more names (first names, middle names,
    last names, etc.)"""
    names: list[str] = field(kw_only=True)

@dataclass
class Human(Creature):
    """Defines a human who has a home address."""
    address: str

donna = Human('1858 Hidden Meadow Drive, East Grand Forks, ND 56721',
              names=['Donna', 'Jennings'])
print(donna)

Running pylint (2.12.2):

$ python -m pylint dc_kw_only.py 
************* Module dc_kw_only
dc_kw_only.py:16:8: E1124: Argument 'names' passed by position and keyword in constructor call (redundant-keyword-arg)
dc_kw_only.py:16:8: E1120: No value for argument 'address' in constructor call (no-value-for-parameter)

--------------------------------------------------------------------
Your code has been rated at -4.29/10

Running Python 3.10.2:

$ python dc_kw_only.py 
Human(names=['Donna', 'Jennings'], address='1858 Hidden Meadow Drive, East Grand Forks, ND 56721')

@NeilGirdhar NeilGirdhar changed the title Dataclass and attrs don't understand special KW_ONLY annotation False positives with keywords-only fields of dataclasses Mar 2, 2022
@jacobtylerwalls
Copy link
Member

A valuable test case was given in #7290 that should pass when fixing this issue.

@DanielNoord
Copy link
Collaborator

I opened #7333 to fix the issue with KW_ONLY.

@Infernio I have decided to track your issue in #7333 as it is not actually related in terms of what needs to be done. It seems related but sadly isn't. I might be able to take a look though after we land #7333.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dataclasses False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.10
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants