diff --git a/changelog.d/1090.change.md b/changelog.d/1090.change.md new file mode 100644 index 000000000..5452d16a1 --- /dev/null +++ b/changelog.d/1090.change.md @@ -0,0 +1 @@ +`attrs.asdict()`'s and `attrs.astuple()`'s type stubs now accept the `attrs.AttrsInstance` protocol. 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]] = ..., 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