Uh oh!
There was an error while loading. Please reload this page.
Use a TypeGuard for dataclasses.is_dataclass(); refine asdict(), astuple(), fields(), replace() - #9362
Conversation
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Uh oh!
There was an error while loading. Please reload this page.
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 |
AlexWaygood
commented
Dec 18, 2022
@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? |
rchen152
commented
Dec 19, 2022
Yep, pytype synthesizes the |
tmke8
commented
Dec 19, 2022
Should |
AlexWaygood
commented
Dec 19, 2022
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 |
pradeep90
commented
Dec 20, 2022
Yes, feel free to merge this change. Pyre doesn't currently synthesize a |
…, `astuple()`, `fields()`, `replace()` (python#9362)
cdce8p
commented
Feb 4, 2023
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, importdataclassesfrompydantic.dataclassesimportdataclass@dataclassclassInfo:
var: strdeffunc(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). |
AlexWaygood
commented
Feb 4, 2023
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 |
AlexWaygood
commented
Feb 4, 2023
(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.) |
cdce8p
commented
Feb 4, 2023
Small update regarding pydantic. Enabling it's mypy plugin does resolve the issue [mypy]plugins = pydantic.mypy |
Fixes#9345. Refs python/mypy#14215