More accurate dataclasses.dataclass overloads#14095
Merged
JelleZijlstra merged 6 commits intopython:mainfrom May 21, 2025
Merged
More accurate dataclasses.dataclass overloads#14095JelleZijlstra merged 6 commits intopython:mainfrom
dataclasses.dataclass overloads#14095JelleZijlstra merged 6 commits intopython:mainfrom
Conversation
dataclasses.dataclass overloads
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
For cls = dataclasses.dataclass( # type: ignore[call-overload]
cls,
# the value of init here doesn't affect anything except that it makes it easier to generate a signature
init=True,
repr=repr,
eq=eq,
order=order,
unsafe_hash=unsafe_hash,
frozen=frozen_,
**kwargs,
) |
Contributor
Author
In if should_use_proxy:
dc_cls_doc = ''
dc_cls = DataclassProxy(cls)
default_validate_on_init = False
else:
dc_cls_doc = cls.__doc__ or '' # needs to be done before generating dataclass
if sys.version_info >= (3, 10):
dc_cls = dataclasses.dataclass(
cls,
init=init,
repr=repr,
eq=eq,
order=order,
unsafe_hash=unsafe_hash,
frozen=frozen,
kw_only=kw_only,
)
else:
dc_cls = dataclasses.dataclass( # type: ignore
cls, init=init, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen
)
default_validate_on_init = TrueThe new error is just the result of overriding a variable that's typed differently from |
Contributor
Author
|
@JelleZijlstra Maybe you'd be interested in taking a look? |
JelleZijlstra
approved these changes
May 21, 2025
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pydantic (https://github.com/pydantic/pydantic)
+ pydantic/v1/dataclasses.py:214: error: Incompatible types in assignment (expression has type "type[Any]", variable has type "DataclassProxy") [assignment]
- pydantic/v1/dataclasses.py:214: error: No overload variant of "dataclass" matches argument types "type[Any]", "bool", "bool", "bool", "bool", "bool", "bool", "bool" [call-overload]
- pydantic/v1/dataclasses.py:214: note: Possible overload variants:
- pydantic/v1/dataclasses.py:214: note: def dataclass(None, /) -> Callable[[type[_T]], type[_T]]
- pydantic/v1/dataclasses.py:214: note: def [_T] dataclass(type[_T], /) -> type[_T]
- pydantic/v1/dataclasses.py:214: note: def dataclass(*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ..., match_args: bool = ..., kw_only: bool = ..., slots: bool = ..., weakref_slot: bool = ...) -> Callable[[type[_T]], type[_T]]
+ pydantic/dataclasses.py:257: error: Unused "type: ignore" comment [unused-ignore]
|
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.
At runtime, you're additional able to pass in supported key-word arguments to
dataclassinstead of, as the the stubs would suggest either pass inclsor just pass in key-word arguments. This is clearly visible from the cpython implementation:Perhaps there's some valid reason we're not doing this relating to type-checker implementations, but would be curious to know if that's the case.