-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Use a TypeGuard
for dataclasses.is_dataclass()
; refine asdict()
, astuple()
, fields()
, replace()
#9362
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
Merged
+100
−9
Merged
Use a TypeGuard
for dataclasses.is_dataclass()
; refine asdict()
, astuple()
, fields()
, replace()
#9362
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3f00513
Use a `TypeGuard` for `dataclasses.is_dataclass()`; refine `as_dict()…
AlexWaygood 4ec190e
Also do `replace()`
AlexWaygood 0ae681a
Be more principled (let's see what primer says)
AlexWaygood d35e3a3
Also `fields()`
AlexWaygood cfaffb0
Make protocol more precise
AlexWaygood 2a0b0ba
Add test cases
AlexWaygood 110cd4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d41f9ee
Improve test cases
AlexWaygood 71d4d20
Update test_cases/stdlib/check_dataclasses.py
AlexWaygood 693dc76
Update test_cases/stdlib/check_dataclasses.py
AlexWaygood cc404fb
Update test_cases/stdlib/check_dataclasses.py
AlexWaygood 4cbd58c
Merge branch 'main' into dataclasses
AlexWaygood c291f63
Update stdlib/dataclasses.pyi
AlexWaygood 888fc4b
Improve comments
AlexWaygood a564082
Comment out tests that fail on pyright
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from __future__ import annotations | ||
|
||
import dataclasses as dc | ||
from typing import Any, Dict, Tuple, Type | ||
from typing_extensions import assert_type | ||
|
||
|
||
@dc.dataclass | ||
class Foo: | ||
attr: str | ||
|
||
|
||
assert_type(dc.fields(Foo), Tuple[dc.Field[Any], ...]) | ||
|
||
# Mypy correctly emits errors on these | ||
# due to the fact it's a dataclass class, not an instance. | ||
# Pyright, however, handles ClassVar members in protocols differently. | ||
# See https://github.com/microsoft/pyright/issues/4339 | ||
# | ||
# dc.asdict(Foo) | ||
# dc.astuple(Foo) | ||
# dc.replace(Foo) | ||
|
||
if dc.is_dataclass(Foo): | ||
# The inferred type doesn't change | ||
# if it's already known to be a subtype of type[_DataclassInstance] | ||
assert_type(Foo, Type[Foo]) | ||
|
||
f = Foo(attr="attr") | ||
|
||
assert_type(dc.fields(f), Tuple[dc.Field[Any], ...]) | ||
assert_type(dc.asdict(f), Dict[str, Any]) | ||
assert_type(dc.astuple(f), Tuple[Any, ...]) | ||
assert_type(dc.replace(f, attr="new"), Foo) | ||
|
||
if dc.is_dataclass(f): | ||
# The inferred type doesn't change | ||
# if it's already known to be a subtype of _DataclassInstance | ||
assert_type(f, Foo) | ||
|
||
|
||
def test_other_isdataclass_overloads(x: type, y: object) -> None: | ||
# TODO: pyright correctly emits an error on this, but mypy does not -- why? | ||
# dc.fields(x) | ||
|
||
dc.fields(y) # type: ignore | ||
|
||
dc.asdict(x) # type: ignore | ||
dc.asdict(y) # type: ignore | ||
|
||
dc.astuple(x) # type: ignore | ||
dc.astuple(y) # type: ignore | ||
|
||
dc.replace(x) # type: ignore | ||
dc.replace(y) # type: ignore | ||
|
||
if dc.is_dataclass(x): | ||
assert_type(dc.fields(x), Tuple[dc.Field[Any], ...]) | ||
# These should cause type checkers to emit errors | ||
# due to the fact it's a dataclass class, not an instance | ||
dc.asdict(x) # type: ignore | ||
dc.astuple(x) # type: ignore | ||
dc.replace(x) # type: ignore | ||
|
||
if dc.is_dataclass(y): | ||
assert_type(dc.fields(y), Tuple[dc.Field[Any], ...]) | ||
|
||
# Mypy corrextly emits an error on these due to the fact we don't know | ||
# whether it's a dataclass class or a dataclass instance. | ||
# Pyright, however, handles ClassVar members in protocols differently. | ||
# See https://github.com/microsoft/pyright/issues/4339 | ||
# | ||
# dc.asdict(y) | ||
# dc.astuple(y) | ||
# dc.replace(y) | ||
|
||
if dc.is_dataclass(y) and not isinstance(y, type): | ||
assert_type(dc.fields(y), Tuple[dc.Field[Any], ...]) | ||
assert_type(dc.asdict(y), Dict[str, Any]) | ||
assert_type(dc.astuple(y), Tuple[Any, ...]) | ||
dc.replace(y) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.