Skip to content

kw_only dataclass fields confuse mypy on argument order #19017

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

Closed
inducer opened this issue May 2, 2025 · 0 comments · Fixed by #19018
Closed

kw_only dataclass fields confuse mypy on argument order #19017

inducer opened this issue May 2, 2025 · 0 comments · Fixed by #19018
Labels
bug mypy got something wrong topic-dataclasses

Comments

@inducer
Copy link

inducer commented May 2, 2025

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
@inducer inducer added the bug mypy got something wrong label May 2, 2025
sterliakov added a commit to sterliakov/mypy that referenced this issue Jun 4, 2025
JukkaL pushed a commit that referenced this issue Jun 5, 2025
Fixes #19017. Fixes #17731.

This is a rather naive change: python does that at runtime. `kw_only`
args can be in any order, and non-kwonly args should remain sorted as-is
(stable sort). I don't understand why this was only done in presence of
a parent dataclass - AFAIC kwonly fields work that way since `kw_only`
was introduced in py3.10.

The test I changed was invalid and asserted a false positive to the best
of my knowledge.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-dataclasses
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants