Skip to content
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

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

Closed
tgallant opened this issue Jul 24, 2021 · 1 comment · Fixed by #10867
Closed
Labels
bug mypy got something wrong

Comments

@tgallant
Copy link
Contributor

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
@tgallant tgallant added the bug mypy got something wrong label Jul 24, 2021
@tgallant
Copy link
Contributor Author

I took a shot at implementing this change here #10867

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant