-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Use a TypeGuard
for dataclasses.is_dataclass()
; refine asdict()
, astuple()
, fields()
, replace()
#9362
Conversation
TypeGuard
for dataclasses.is_dataclass()
; refine as_dict()
, as_tuple()
TypeGuard
for dataclasses.is_dataclass()
; refine as_dict()
, as_tuple()
, fields()
, replace()
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
TypeGuard
for dataclasses.is_dataclass()
; refine as_dict()
, as_tuple()
, fields()
, replace()
TypeGuard
for dataclasses.is_dataclass()
; refine asdict()
, astuple()
, fields()
, replace()
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: porcupine (https://github.com/Akuli/porcupine)
+ porcupine/utils.py:408: error: No overload variant of "asdict" matches argument type "EventDataclass" [call-overload]
+ porcupine/utils.py:408: note: Possible overload variants:
+ porcupine/utils.py:408: note: def asdict(obj: _DataclassInstance) -> Dict[str, Any]
+ porcupine/utils.py:408: note: def [_T] asdict(obj: _DataclassInstance, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) -> _T
hydra-zen (https://github.com/mit-ll-responsible-ai/hydra-zen)
- src/hydra_zen/structured_configs/_implementations.py:1666: error: Incompatible types in assignment (expression has type "Union[Type[Any], Callable[P, R], Callable[..., Any]]", variable has type overloaded function) [assignment]
+ src/hydra_zen/structured_configs/_implementations.py:1666: error: Incompatible types in assignment (expression has type "Union[Type[_DataclassInstance], Type[Any], Callable[P, R], Callable[..., Any], DataClass_]", variable has type overloaded function) [assignment]
xarray-dataclasses (https://github.com/astropenguin/xarray-dataclasses)
+ xarray_dataclasses/v2/specs.py:115: error: Argument 1 to "fields" has incompatible type "Type[DataClass[P]]"; expected "Union[_DataclassInstance, Type[_DataclassInstance]]" [arg-type]
+ xarray_dataclasses/v2/specs.py:115: note: Only class variables allowed for class object access on protocols, __dataclass_fields__ is an instance variable of "DataClass"
+ xarray_dataclasses/v2/specs.py:115: note: ClassVar protocol member _DataclassInstance.__dataclass_fields__ can never be matched by a class object
+ xarray_dataclasses/v2/specs.py:158: error: Argument 1 to "fields" has incompatible type "Type[DataClass[P]]"; expected "Union[_DataclassInstance, Type[_DataclassInstance]]" [arg-type]
+ xarray_dataclasses/v2/specs.py:158: note: Only class variables allowed for class object access on protocols, __dataclass_fields__ is an instance variable of "DataClass"
+ xarray_dataclasses/v2/specs.py:158: note: ClassVar protocol member _DataclassInstance.__dataclass_fields__ can never be matched by a class object
core (https://github.com/home-assistant/core)
+ homeassistant/components/p1_monitor/diagnostics.py:45: error: Argument 1 to "asdict" has incompatible type "Optional[Any]"; expected "_DataclassInstance" [arg-type]
+ homeassistant/components/zwave_js/discovery.py:78: error: No overload variant of "asdict" matches argument type "DataclassMustHaveAtLeastOne" [call-overload]
+ homeassistant/components/zwave_js/discovery.py:78: note: Possible overload variants:
+ homeassistant/components/zwave_js/discovery.py:78: note: def asdict(obj: _DataclassInstance) -> Dict[str, Any]
+ homeassistant/components/zwave_js/discovery.py:78: note: def [_T] asdict(obj: _DataclassInstance, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) -> _T
|
@stroxler / @pradeep90: from @thomkeh's experiments in #9345, it looks like pyre doesn't currently synthesize a Same question for @rchen152 -- not sure if pytype synthesizes this attribute for dataclasses or not? Would merging this cause issues for pytype? |
Yep, pytype synthesizes the |
Should |
Brilliant, thanks @rchen152!
Maybe. But it's probably better to add it to typeshed first and wait a while (a few months, at least) to see if it's stable before adding it to |
Yes, feel free to merge this change. Pyre doesn't currently synthesize a |
…, `astuple()`, `fields()`, `replace()` (python#9362)
I've been working on updating Home Assistant for these changes. Some notes There are a couple of places where I needed to use the dataclass protocol itself. One example are dataclass mixin classes. From that I think it would make sense to either include it in At least for mypy, import dataclasses
from pydantic.dataclasses import dataclass
@dataclass
class Info:
var: str
def func(info: Info) -> None:
dataclasses.asdict(info)
Tested with |
As long as pydantic's dataclass has the EDIT: oh wait, nevermind. The type checker needs to know that it's going to be synthesized. So maybe |
I think pydantic has a custom mypy plugin, so it's probably an issue with the plugin not synthesising the attribute (haven't dug in to confirm my suspicion). |
Anyway, thanks (as ever) for the very early testing here @cdce8p! Given how new/experimental the protocol is, and how it doesn't work particularly well with pyright due to microsoft/pyright#4339, I think it definitely isn't a suitable candidate to be added to |
(To be clear, this PR shouldn't cause any regressions for pyright users, the pyright issue just means that this PR doesn't provide quite as much of a type-safety improvement for pyright users as it does for mypy users.) |
Small update regarding pydantic. Enabling it's mypy plugin does resolve the issue [mypy]
plugins = pydantic.mypy |
Fixes #9345. Refs python/mypy#14215