Export DataclassInstance protocol from _typeshed#9676
Merged
AlexWaygood merged 2 commits intopython:mainfrom Feb 4, 2023
Merged
Export DataclassInstance protocol from _typeshed#9676AlexWaygood merged 2 commits intopython:mainfrom
DataclassInstance protocol from _typeshed#9676AlexWaygood merged 2 commits intopython:mainfrom
Conversation
AlexWaygood
reviewed
Feb 4, 2023
Member
AlexWaygood
left a comment
There was a problem hiding this comment.
Any reason not to just put it in _typeshed/__init__.pyi? It feels a bit small to go in its own file. We can always move it to a dataclasses-specific file later if we add more dataclasses-specific protocols or aliases.
Also: I'd like to add a comment noting that the protocol is quasi-experimental, and linking to the pyright issue. The type won't work as intended for pyright users unless and until the pyright issue is reconsidered (or we think of another way of doing this in the stubs).
This comment has been minimized.
This comment has been minimized.
AlexWaygood
approved these changes
Feb 4, 2023
DataclassInstance protocolDataclassInstance protocol from _typeshed
Member
|
Thanks! |
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: porcupine (https://github.com/Akuli/porcupine)
- porcupine/utils.py:408: note: def asdict(obj: _DataclassInstance) -> Dict[str, Any]
+ 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
+ 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:1862: 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]
+ src/hydra_zen/structured_configs/_implementations.py:1862: 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]
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/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: note: def asdict(obj: _DataclassInstance) -> Dict[str, Any]
+ 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
+ homeassistant/components/zwave_js/discovery.py:78: note: def [_T] asdict(obj: DataclassInstance, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) -> _T
|
This was referenced Feb 14, 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.
Add
DataclassInstanceprotocol to_typeshed. Especially when dealing with mixin classes, it's quite common to have to use it. Moving it to_typeshedmeans users can import it from there without having to reimplement it themselves.Followup to #9362 (comment)
/CC @AlexWaygood