-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
More accurate dataclasses.dataclass
overloads
#14095
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
More accurate dataclasses.dataclass
overloads
#14095
Conversation
dataclasses.dataclass
overloads
This comment has been minimized.
This comment has been minimized.
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,
) |
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 = True The new error is just the result of overriding a variable that's typed differently from |
@JelleZijlstra Maybe you'd be interested in taking a look? |
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]
|
At runtime, you're additional able to pass in supported key-word arguments to
dataclass
instead of, as the the stubs would suggest either pass incls
or 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.