From 79d230f946ccf31930cd2092c31933b1fe60f623 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tverskoy Date: Thu, 19 Jan 2023 18:03:41 +0000 Subject: [PATCH 1/4] Add AttrsInstance protocol to forgotten attrs.asdict and attrs.astuple --- src/attrs/__init__.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/attrs/__init__.pyi b/src/attrs/__init__.pyi index 4ea64d8ea..9372cfea1 100644 --- a/src/attrs/__init__.pyi +++ b/src/attrs/__init__.pyi @@ -46,7 +46,7 @@ from attr import validators as validators # TODO: see definition of attr.asdict/astuple def asdict( - inst: Any, + inst: AttrsInstance, recurse: bool = ..., filter: Optional[_FilterType[Any]] = ..., dict_factory: Type[Mapping[Any, Any]] = ..., @@ -59,7 +59,7 @@ def asdict( # TODO: add support for returning NamedTuple from the mypy plugin def astuple( - inst: Any, + inst: AttrsInstance, recurse: bool = ..., filter: Optional[_FilterType[Any]] = ..., tuple_factory: Type[Sequence[Any]] = ..., From 300e1e970e5dd55c0515a277c5a10de0ac678241 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tverskoy Date: Thu, 19 Jan 2023 18:17:51 +0000 Subject: [PATCH 2/4] Add mypy test for attrs.asdict --- tests/test_mypy.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_mypy.yml b/tests/test_mypy.yml index f0603a666..37b2ead2b 100644 --- a/tests/test_mypy.yml +++ b/tests/test_mypy.yml @@ -1372,6 +1372,25 @@ fields(A) # E: Argument 1 to "fields" has incompatible type "Type[A]"; expected "Type[AttrsInstance]" [arg-type] +- case: testAsDict + main: | + from attrs import asdict, define + + @define + class A: + a: int + + asdict(A(1)) + +- case: testAsDictError + main: | + from attrs import asdict + + class A: + a: int + + asdict(A()) # E: Argument 1 to "asdict" has incompatible type "A"; expected "AttrsInstance" [arg-type] + - case: testHasTypeGuard main: | from attrs import define, has From b3ee104f82a4b41646cf076d28cb2ed047cac3fe Mon Sep 17 00:00:00 2001 From: Vyacheslav Tverskoy Date: Sun, 22 Jan 2023 16:41:57 +0000 Subject: [PATCH 3/4] Add changelog entry for attrs.asdict --- changelog.d/1090.change.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/1090.change.md diff --git a/changelog.d/1090.change.md b/changelog.d/1090.change.md new file mode 100644 index 000000000..046538647 --- /dev/null +++ b/changelog.d/1090.change.md @@ -0,0 +1 @@ +`attrs.asdict` and `attrs.astuple` now accept `attrs.AttrsInstance` protocol. From 0d693f6439dfb804e575196b7892e63a2fa9103e Mon Sep 17 00:00:00 2001 From: Hynek Schlawack Date: Mon, 23 Jan 2023 10:17:54 +0100 Subject: [PATCH 4/4] Clarify it's about type stubs --- changelog.d/1090.change.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.d/1090.change.md b/changelog.d/1090.change.md index 046538647..5452d16a1 100644 --- a/changelog.d/1090.change.md +++ b/changelog.d/1090.change.md @@ -1 +1 @@ -`attrs.asdict` and `attrs.astuple` now accept `attrs.AttrsInstance` protocol. +`attrs.asdict()`'s and `attrs.astuple()`'s type stubs now accept the `attrs.AttrsInstance` protocol.