Skip to content

[attrs plugin] Field level kw_only=False ignored with kw_only=True at class level #20947

Description

@getzze

Bug Report

When setting kw_only=True at class level with attrs, all fields are set to be keyword only. Since attrs=v25.4.0, individual attributes with kw_only=False are treated as positional (to be consistent with dataclasses, see python-attrs/attrs#1457).
But Mypy still considers attributes with kw_only=False to be keyword only.

To Reproduce

from attrs import define, field

@define(kw_only=True)
class Parent:
    param: int = field(kw_only=False)
    optional: bool = True


# Valid
p1 = Parent(1)
p2 = Parent(2, optional=False)

@define
class A:
    a: int
    b: int = field(kw_only=True, default=1)

@define(kw_only=True)
class B(A):
    c: int = field(kw_only=False)
    d: int = 3

# Valid
B(0, 1)
B(0, 1, b=3, d=4)

Expected Behavior

Mypy should not return any error as it's valid behavior with attrs>=v25.4.0

Actual Behavior

test_mypy.py:12: error: Too many positional arguments for "Parent"  [misc]
    p1 = Parent(1)
         ^~~~~~~~~
test_mypy.py:13: error: Too many positional arguments for "Parent"  [misc]
    p2 = Parent(2, optional=False)
         ^~~~~~~~~~~~~~~~~~~~~~~~~
test_mypy.py:26: error: Too many positional arguments for "B"  [misc]
    B(0, 1)
    ^~~~~~~
test_mypy.py:26: error: Missing named argument "c" for "B"  [call-arg]
    B(0, 1)
    ^~~~~~~
test_mypy.py:27: error: "B" gets multiple values for keyword argument "b"  [misc]
    B(0, 1, b=3, d=4)
    ^~~~~~~~~~~~~~~~~
test_mypy.py:27: error: Missing named argument "c" for "B"  [call-arg]
    B(0, 1, b=3, d=4)
    ^~~~~~~~~~~~~~~~~
Found 6 errors in 1 file (checked 1 source file)

Your Environment

Python 3.14.3
mypy 1.19.1 (compiled: no)
attrs 25.4.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions