Skip to content

Don't warn about non keyword-only attributes after keyword-only attributes in dataclasses #10865

Closed
@tgallant

Description

@tgallant

Bug Report

If all fields in a dataclass are keyword only then mypy should not raise errors regarding the order of attributes with default values.

Python 3.10.0 introduces a new dataclass parameter kw_only.

When this parameter is set to true then all fields in the dataclass will be marked as keyword only. If all fields in a dataclass are keyword only then checking the order of attributes with default values is not relevant.

Prior art:

[attrs plugin] Support kw_only=True #6107
Allow attrs kw_only arguments at any position #8803

bpo-43532: Add keyword-only fields to dataclasses. python/cpython#25608
Sync typeshed #10537 (introduced the kw_only dataclass parameter to mypy)

To Reproduce

  1. Ensure you are using python version 3.10.0b4 (although should be reproducible with other 3.10 beta versions)
  2. Ensure you are using a dev build of mypy off of master
  3. Create a file test_dataclass.py which contains the following
from dataclasses import dataclass


@dataclass(kw_only=True)
class Example:
    name: str = 'hi'
    email: str
  1. Run mypy and observe that it fails
$ mypy test_dataclass.py 
test_dataclass.py:7: error: Attributes without a default cannot follow attributes with one
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

This should not cause a mypy error. Checking the ordering of attributes with default parameters is not necessary when kw_only=True is specified.

Your Environment

  • Mypy version used: 0.920+dev.97b3b90356893e2a9f6947ebe52b41cd54ab1cb7
  • Mypy command-line flags: mypy test_dataclass.py
  • Mypy configuration options from mypy.ini (and other config files): n/a; default
  • Python version used: 3.10.0b4
  • Operating system and version: OS X 10.15.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions