Skip to content
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

Add override decorators to sensor #94998

Merged
merged 1 commit into from
Oct 6, 2023
Merged

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Jun 21, 2023

Proposed change

Followup to the recent mypy upgrade: #94987

This PR adds the first override decorators. Is that something we want to do? Although the performance impact should minimal, adding it to so many methods might still be noticeable - haven't checked that. Basically it could be added to each integration / platform multiple times.

Mypy doesn't yet implement the strict mode, which would require the decorator if a method overrides an implementation in a base class, suggested in the PEP. However, we could start adding it now. FYI: For the strict mode, we would roughly need to add it 4.800 times.

https://docs.python.org/3.12/library/typing.html#typing.override

/CC: @frenck

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)
  • 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 Black (black --fast 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

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.

@frenck frenck marked this pull request as draft June 21, 2023 16:50
@frenck
Copy link
Member

frenck commented Jun 21, 2023

Marked it as draft for a moment, will get back to this.

../Frenck

@frenck
Copy link
Member

frenck commented Jun 22, 2023

@cdce8p Discussed this a bit, and wondering if this can be enforced?

Like for example, if an integration has strict typing enabled within Home Assistant, can we error with MyPy an @override is missing?

@cdce8p
Copy link
Member Author

cdce8p commented Jun 22, 2023

@cdce8p Discussed this a bit, and wondering if this can be enforced?

Like for example, if an integration has strict typing enabled within Home Assistant, can we error with MyPy an @override is missing?

Not yet. That is the strict mode suggested in the PEP. Mypy doesn't implement it yet. It looks fairly easy to add which is why I already had the 4.800 number. A run with it enabled would look something like this: https://github.com/cdce8p/ha-core/actions/runs/5341252109/jobs/9681895425#step:15:4857

https://peps.python.org/pep-0698/#strict-enforcement-per-project

@frenck
Copy link
Member

frenck commented Jun 22, 2023

Right, so the reason for asking is that we think it might be confusion/adding a level of complexity for newer contributors, ending up slapping it onto everything or nothing at all.

Therefore, it would be nice if we could check and enforce it has been applied and correctly applied in case strict mode is enabled for the specific integration.

Only that way we can guarantee it is correctly done. In the far future we might change it when this becomes more common of course.

@cdce8p
Copy link
Member Author

cdce8p commented Jul 13, 2023

FYI The strict mode will be part of the next mypy release. My PR was just merged. python/mypy#15512

@cdce8p
Copy link
Member Author

cdce8p commented Aug 11, 2023

Therefore, it would be nice if we could check and enforce it has been applied and correctly applied in case strict mode is enabled for the specific integration.

Now possible with mypy 1.5.0 and enable-error-code="explicit-override".
https://mypy-lang.blogspot.com/2023/08/mypy-15-released.html

@cdce8p cdce8p marked this pull request as ready for review August 11, 2023 11:31
Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @cdce8p 👍

../Frenck

@frenck frenck merged commit d5f07ef into home-assistant:dev Oct 6, 2023
34 checks passed
@cdce8p cdce8p deleted the first-override branch October 6, 2023 11:46
@github-actions github-actions bot locked and limited conversation to collaborators Oct 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants