Skip to content

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

Closed
pylint-dev/astroid
#1764
@sharhalakis

Description

@sharhalakis

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    AstroidRelated to astroidFalse Positive 🦟A message is emitted but nothing is wrong with the codeHelp wanted 🙏Outside help would be appreciated, good for new contributorsNeeds PRThis issue is accepted, sufficiently specified and now needs an implementationdataclassespython 3.10

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions