core icon indicating copy to clipboard operation
core copied to clipboard

Add date, datetime, Decimal to StateType alias

Open cdce8p opened this issue 1 year ago • 9 comments

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:

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 running python3 -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:

cdce8p avatar Oct 09 '24 11:10 cdce8p

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign reolink Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign goodwe Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign onvif Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign unifi Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign sensor Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign rfxtrx Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

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 close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign netgear Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

home-assistant[bot] avatar Oct 09 '24 11:10 home-assistant[bot]

I thought about it before but is there a particular reason why StateType isn't including date | datetime | Decimal as well?

gjohansson-ST avatar Oct 09 '24 11:10 gjohansson-ST

I thought about it before but is there a particular reason why StateType isn't including date | datetime | Decimal as 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.

cdce8p avatar Oct 09 '24 12:10 cdce8p

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks :+1:

Learn more about our pull request process.

home-assistant[bot] avatar Oct 09 '24 14:10 home-assistant[bot]

We want to get rid of this one; not extend it.

If the use of StateType should be discouraged, I'm closing this then.

cdce8p avatar Oct 09 '24 14:10 cdce8p