Skip to content

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)

@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