dataclass.replace: allow transformed classes#15915
Merged
ilevkivskyi merged 2 commits intopython:masterfrom Sep 17, 2023
Merged
dataclass.replace: allow transformed classes#15915ilevkivskyi merged 2 commits intopython:masterfrom
ilevkivskyi merged 2 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
@AlexWaygood WDYT? |
AlexWaygood
reviewed
Aug 24, 2023
Member
There was a problem hiding this comment.
This makes sense to me, but I don't think it fixes the commented-out test in typeshed, which is trying to check that you can use is_dataclass in a typeguard-y way, i.e.:
from dataclasses import is_dataclass, replace
def foo(x: object):
if is_dataclass(x) and not isinstance(x, type):
y = replace(x)I think mypy is still going to complain about ^that kind of usage, but it's idiomatic code according to the dataclasses documentation.
For reference, the signature that we have for is_dataclass in typeshed is:
@overload
def is_dataclass(obj: DataclassInstance) -> Literal[True]: ...
@overload
def is_dataclass(obj: type) -> TypeGuard[type[DataclassInstance]]: ...
@overload
def is_dataclass(obj: object) -> TypeGuard[DataclassInstance | type[DataclassInstance]]: ...And the signature we have for dataclasses.replace is:
def replace(__obj: _DataclassT, **changes: Any) -> _DataclassT: ...Where DataclassInstance and _DataclassT are:
class DataclassInstance(Protocol):
__dataclass_fields__: ClassVar[dict[str, dataclasses.Field[Any]]]
_DataclassT = TypeVar("_DataclassT", bound=DataclassInstance)
Contributor
Author
|
Thanks for explaining so thoroughly. |
Contributor
Author
|
👉 #15962 |
1 task
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Contributor
Author
|
@AlexWaygood shall we go ahead with this? |
ilevkivskyi
approved these changes
Sep 17, 2023
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
We already synthesize
__dataclass_fields__for all classes including@dataclass_transform'd ones, thus assume more than PEP-681 says. We might as well assumedataclasses.replaceapplies on all same classes. This way we risk false positive since it'll raise in runtime.Fixes #15843.