Skip to content

Valid combinations of *args and **kwargs with dataclasses give spurious "multiple values" errors #7657

@cbowdon

Description

@cbowdon
  • Are you reporting a bug, or opening a feature request?
    Bug

  • Please insert below the code you are checking with mypy

from dataclasses import dataclass, asdict, astuple

@dataclass
class A:
    x: int = 0
    y: int = 0

@dataclass
class B(A):
    z: int = 0

a = A(1, 1)

# tuple unpacking -- fails
b = B(*astuple(a), z=1)  # error: "B" gets multiple values for keyword argument "z"
assert b == B(x=1, y=1, z=1)

# tuple unpacking -- fails
b = B(*astuple(a), 1)  # Too many arguments for "B"
assert b == B(x=1, y=1, z=1)

# dict unpacking -- succeeds
b = B(z=1, **asdict(a))
assert b == B(x=1, y=1, z=1)

# dict unpacking -- fails
b = B(**asdict(a), z=1)  # "B" gets multiple values for keyword argument "z"
assert b == B(x=1, y=1, z=1)
  • What is the actual behavior/output?
mypy example.py
example.py:16: error: "B" gets multiple values for keyword argument "z"
example.py:20: error: Too many arguments for "B"
example.py:28: error: "B" gets multiple values for keyword argument "z"
Found 3 errors in 1 file (checked 1 source file)

But python3 example.py succeeds with no errors.

  • What is the behavior/output you expect?

Ideally this should typecheck successfully. At least the error message should be more accurate.

  • What are the versions of mypy and Python you are using?
  • mypy 0.730
  • Python 3.7.3

Same results with: mypy 0.740+dev.beec11a28fd835acb1e4ee8b2bf14d969c4e5922

  • What are the mypy flags you are using? (For example --strict-optional)
    None

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions