Use Mapping[str, Any] instead of dict in Entity#48421
Use Mapping[str, Any] instead of dict in Entity#48421frenck merged 3 commits intohome-assistant:devfrom
Conversation
|
I am confused, the consumer of these return values is Home Assistant, which doesn't expect any typedict bound result. What would it add? Edit: nevermind, got it. |
|
Yeah, it's for components which produce these values not for consumers. |
|
@frenck do you have any other concerns about this? |
frenck
left a comment
There was a problem hiding this comment.
I'm ok with this change, but I do want a second opinion on that (as I expect this to be happing in more places over time if we accept this).
|
Btw, typing documentation suggests using |
|
Also it says that I can make this change. |
|
@cdce8p I saw you were working on changing type hints to modern ones from PEP 585. Maybe you'll be also interested in migrating |
|
@KapJI Technically you're right. However changing imports and figuring out if the You're, of cause, free to start using them today. |
cdce8p
left a comment
There was a problem hiding this comment.
I think this will work! It just depends on if the results are going to be change later. Since this isn't the case Mapping will allow both TypedDict and dict as return values.
@frenck I just think state_attributes should probably be updated as well. It might not be overwritten by the individual integration but will be by the component base class.
|
@frenck is it good to go now? Should I update |
In that case, it can still not be overridden and thus types, as even the individual component overriding it, doesn't know what the upstream integration will bring in. |
|
Ok, let's keep it as is. |
|
Thanks, this will help to use stricter type checks in our component! |
Proposed change
This will allow components to use strictly typed
TypedDictfor return values of these methods.Mappingis covariant andTypedDictis compatible withMapping[str, Any]but not withdict[str, Any].There was discussion in mypy about if
TypedDictshould be compatible withdict[str, Any]and the outcome of that discussion is thatMappingshould be used instead: python/mypy#4976The idea is to make it possible to annotate components with types without using
Any. So, for example, overrideddevice_infoin component can returnTypedDictinstead ofdict[str, Any]. This will allow making components a bit safer.Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: