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

Add AttrsInstance protocol to forgotten attrs.asdict and attrs.astuple #1090

Merged
merged 4 commits into from Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1090.change.md
@@ -0,0 +1 @@
`attrs.asdict()`'s and `attrs.astuple()`'s type stubs now accept the `attrs.AttrsInstance` protocol.
4 changes: 2 additions & 2 deletions src/attrs/__init__.pyi
Expand Up @@ -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]] = ...,
Expand All @@ -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]] = ...,
Expand Down
19 changes: 19 additions & 0 deletions tests/test_mypy.yml
Expand Up @@ -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
Expand Down