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

unexpected-keyword-arg in dataclasses with kw_only=True, inheritance and defaults #6550

Closed
sharhalakis opened this issue May 8, 2022 · 7 comments · Fixed by pylint-dev/astroid#1764 or #7413
Labels
Astroid Related to astroid dataclasses False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation python 3.10

Comments

@sharhalakis
Copy link

sharhalakis commented May 8, 2022

Bug description

This is practically #6275 when using the new kw_only=True

The following code causes the bogus error. The key points are:

  • the kw_only argument on class A
  • the presence of a default in class A
  • that class B includes a value without a default.

The problem doesn't happen if there's no default in-between or if inheritance isn't used.

# pylint: disable=missing-module-docstring,invalid-name,too-few-public-methods,missing-class-docstring

import dataclasses as dc

@dc.dataclass(kw_only=True)
class A:
    a: str
    b: int = 1

@dc.dataclass(kw_only=True)
class B(A):
    c: str

c = B(a='aaa', c='ccc')

Command used

pylint b.py

Pylint output

$ pylint b.py 
************* Module b
b.py:15:4: E1123: Unexpected keyword argument 'c' in constructor call (unexpected-keyword-arg)

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

Expected behavior

No pylint errors. This is valid code.

Pylint version

$ pylint --version
pylint 2.12.2
astroid 2.9.3
Python 3.10.4 (main, Mar 24 2022, 13:07:27) [GCC 11.2.0]

OS / Environment

$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux bookworm/sid
Release:        testing
Codename:       bookworm
@sharhalakis sharhalakis added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label May 8, 2022
@jacobtylerwalls jacobtylerwalls added Bug 🪲 False Positive 🦟 A message is emitted but nothing is wrong with the code python 3.10 and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels May 9, 2022
@cdce8p cdce8p added Astroid Related to astroid dataclasses labels Jun 6, 2022
@anis-campos
Copy link

anis-campos commented Jul 18, 2022

+1. kw_only is very useful and the completely disabling unexpected-keyword-arg seems counter productive.

So for now we are disabling the linter on every instantiation.

@Pierre-Sassoulas Pierre-Sassoulas added Needs PR This issue is accepted, sufficiently specified and now needs an implementation Help wanted 🙏 Outside help would be appreciated, good for new contributors and removed Bug 🪲 labels Jul 18, 2022
@smrnff
Copy link

smrnff commented Jul 21, 2022

+1 We really need the feature too.

So for now we are disabling the linter on every instantiation.

We do the same things. It freaks me out.

@DanielNoord
Copy link
Collaborator

Fixed with in pylint-dev/astroid#1764

@jacobtylerwalls Do we want test for this and for the original issue that that PR targeted in pylint? Or is astroid enough in your opinion?

@jacobtylerwalls
Copy link
Member

Let's merge at least one test case in pylint, since the astroid one is a little convoluted (on purpose, to catch the string contains issue.)

@DanielNoord
Copy link
Collaborator

Sure! I'll open a PR for you to review 😄

@yarinbar
Copy link

yarinbar commented Nov 20, 2023

I am experiencing the same issue even when i dont use default values and I even tried defining my own init function:

@dataclass(kw_only=True)
class Alert:
    """of alerts"""
    alert_type: str
    location: torch.Tensor
    # score: float = 1.

    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            if k != 'self':
                setattr(self, k, v)


@dataclass(kw_only=True)
class RoofingAlert(Alert):
    # type = 'roofing'
    # location: torch.Tensor
    angle: float
    cath_direction: torch.Tensor
    vess_direction: torch.Tensor

    def __init__(self, **kwargs):
        super().__init__(alert_type=kwargs.get('alert_type'), location=kwargs.get('location'))
        for k, v in kwargs.items():
            if k != 'self':
                setattr(self, k, v)

and the error:

     80     if point_res:
     81         alerts.append(
---> 82             RoofingAlert(
     83                 alert_type='roofing',
     84                 location=torch.tensor(point),
     85                 angle=angle,
     86                 cath_direction=cath_dir,
     87                 vess_direction=vess_dir
     88             )
     89         )
     91 return alerts

TypeError: RoofingAlert.__init__() got an unexpected keyword argument 'alert_type'

@Pierre-Sassoulas
Copy link
Member

Hello and thank you for reporting an issue @yarinbar. Could you open a new issue, this one is closed and the comment is going to get lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astroid Related to astroid dataclasses False Positive 🦟 A message is emitted but nothing is wrong with the code Help wanted 🙏 Outside help would be appreciated, good for new contributors Needs PR This issue is accepted, sufficiently specified and now needs an implementation python 3.10
Projects
None yet
8 participants