-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
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 Mapping[str, Any] instead of dict in Entity #48421
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
TypedDict
for return values of these methods.Mapping
is covariant andTypedDict
is compatible withMapping[str, Any]
but not withdict[str, Any]
.There was discussion in mypy about if
TypedDict
should be compatible withdict[str, Any]
and the outcome of that discussion is thatMapping
should be used instead: python/mypy#4976The idea is to make it possible to annotate components with types without using
Any
. So, for example, overrideddevice_info
in component can returnTypedDict
instead 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: