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

Allow attrs kw_only arguments at any position #8803

Merged
merged 3 commits into from
May 19, 2020

Conversation

markusschmaus
Copy link
Contributor

This PR fixes #7489

import inspect
from typing import Optional

import attr


@attr.s()
class Valid:
    kw_only: Optional[str] = attr.ib(default=None, kw_only=True)
    param: int = attr.ib()
    optional: bool = attr.ib(default=False)


valid1 = Valid(1)
print(valid1)
valid2 = Valid(2, kw_only='something')
print(valid2)
invalid2 = Valid(2, 'something')
print(invalid2)
invalid3 = Valid('something')
print(invalid3)


@attr.s()
class Valid2:
    param: int = attr.ib()
    kw_only: Optional[str] = attr.ib(default=None, kw_only=True)


valid3 = Valid2(3)
print(valid3)
valid4 = Valid2(4, kw_only='something')
print(valid4)

try:
    invalid = Valid2(4, 'something')
except TypeError:
    pass

produces the expected output:

mypy my_example.py
my_example.py:18: error: Argument 2 to "Valid" has incompatible type "str"; expected "bool"
my_example.py:20: error: Argument 1 to "Valid" has incompatible type "str"; expected "int"
my_example.py:36: error: Too many positional arguments for "Valid2"
Found 3 errors in 1 file (checked 1 source file)

runtests.py Outdated Show resolved Hide resolved
@oakkitten
Copy link

i tested this and found that it perfectly resolves the issue for me. thanks!

Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Thanks.

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

Successfully merging this pull request may close these issues.

Don't warn about non keyword-only attributes after keyword-only attributes in attrs
4 participants