Add date, datetime, Decimal to StateType alias
Proposed change
Initial idea to add a new type alias for SensorValueType below.
This PR adds date, datetime and Decimal to the StateType type alias to support typing the Sensor.state return type. Without that the signature of the superclass method is incompatible
homeassistant/components/sensor/__init__.py:540: error: Signature of "state" incompatible with supertype "Entity" [override]
homeassistant/components/sensor/__init__.py:540: note: Superclass:
homeassistant/components/sensor/__init__.py:540: note: str | int | float | None
homeassistant/components/sensor/__init__.py:540: note: Subclass:
homeassistant/components/sensor/__init__.py:540: note: str | int | float | date | datetime | Decimal | None
That's because the method return type is covariant, i.e. the superclass method return type should encompass all return types of the subclass methods. Subclasses are free to limit the return type further though, e.g. only returning str | None.
--
Initial rejected idea - SensorValueType
Noticed that quite a few integration were using the default StateType | date | datetime | Decimal type for Sensor.native_value. This required importing date, datetime, and Decimal which often weren't used anywhere else in the integration code.
This PR adds a new type alias to helpers.typing to make it easier for integrations to use. Of course they are still able to use narrower types if they choose to do so.
Open to suggestions
- for a better name if there is some
- if the alias should be moved to
sensor/__init__.py.
Type of change
- [ ] Dependency upgrade
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New integration (thank you!)
- [ ] New feature (which adds functionality to an existing integration)
- [ ] Deprecation (breaking change to happen in the future)
- [ ] Breaking change (fix/feature causing existing functionality to break)
- [x] Code quality improvements to existing code or addition of tests
Additional information
- This PR fixes or closes issue: fixes #
- This PR is related to issue:
- Link to documentation pull request:
Checklist
- [ ] The code change is tested and works locally.
- [ ] Local tests pass. Your PR cannot be merged unless tests pass
- [ ] There is no commented out code in this PR.
- [ ] I have followed the development checklist
- [ ] I have followed the perfect PR recommendations
- [ ] The code has been formatted using Ruff (
ruff format homeassistant tests) - [ ] Tests have been added to verify that the new code works.
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.io
If the code communicates with devices, web services, or third-party tools:
- [ ] The manifest file has all fields filled out correctly.
Updated and included derived files by running:python3 -m script.hassfest. - [ ] New or updated dependencies have been added to
requirements_all.txt.
Updated by runningpython3 -m script.gen_requirements_all. - [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
To help with the load of incoming pull requests:
- [ ] I have reviewed two other open pull requests in this repository.
Hey there @starkillerog, mind taking a look at this pull request as it has been labeled with an integration (reolink) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of reolink can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign reolinkRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Hey there @mletenay, @starkillerog, mind taking a look at this pull request as it has been labeled with an integration (goodwe) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of goodwe can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign goodweRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Hey there @hunterjm, mind taking a look at this pull request as it has been labeled with an integration (onvif) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of onvif can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign onvifRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Hey there @kane610, mind taking a look at this pull request as it has been labeled with an integration (unifi) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of unifi can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign unifiRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (sensor) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of sensor can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign sensorRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Hey there @danielhiversen, @elupus, @robbie1221, mind taking a look at this pull request as it has been labeled with an integration (rfxtrx) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of rfxtrx can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign rfxtrxRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
Hey there @hacf-fr, @quentame, @starkillerog, mind taking a look at this pull request as it has been labeled with an integration (netgear) you are listed as a code owner for? Thanks!
Code owner commands
Code owners of netgear can trigger bot actions by commenting:
-
@home-assistant closeCloses the pull request. -
@home-assistant rename Awesome new titleRenames the pull request. -
@home-assistant reopenReopen the pull request. -
@home-assistant unassign netgearRemoves the current integration label and assignees on the pull request, add the integration domain after the command. -
@home-assistant add-label needs-more-informationAdd a label (needs-more-information, problem in dependency, problem in custom component) to the pull request. -
@home-assistant remove-label needs-more-informationRemove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.
I thought about it before but is there a particular reason why StateType isn't including date | datetime | Decimal as well?
I thought about it before but is there a particular reason why
StateTypeisn't includingdate | datetime | Decimalas well?
Seems we now know the reason: It caused quite a few followup issues. In the end however, I now think it would be the right call. This would allow us to type Sensor.state as well. Atm the return type is just Any.
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:
We want to get rid of this one; not extend it.
If the use of StateType should be discouraged, I'm closing this then.