Skip to content

kw_only dataclass fields confuse mypy on argument order #19017

Closed
@inducer

Description

@inducer

Bug Report

This example (derived from #17731) runs flawlessly on Python 3.13:

from dataclasses import dataclass, field

@dataclass
class User:
    id: int = field(kw_only=True)
    name: str

u = User("Foo", id=0)
assert u.name == "Foo"

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&flags=strict&gist=ce6c431acd9f161f68d395b12cc966e9

Expected Behavior

Mypy should accept the code. Its output suggests it is confused about positional argument order (and their number).

Actual Behavior

Mypy 1.15 says:

main.py:8: error: "User" gets multiple values for keyword argument "id"  [misc]
main.py:8: error: Too few arguments for "User"  [call-arg]
main.py:8: error: Argument 1 to "User" has incompatible type "str"; expected "int"  [arg-type]
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.15
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.13

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions