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

Improve dispatcher typing #106872

Merged
merged 7 commits into from
Jan 8, 2024
Merged

Improve dispatcher typing #106872

merged 7 commits into from
Jan 8, 2024

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Jan 2, 2024

Proposed change

An idea to improve the dispatcher typing, in particular async_dispatcher_connect and async_dispatcher_send.

def funct(data1: str, data2: int) -> None:
    ...

def other_funct(data1: str, data2: int, x: int) -> None:
    ...

async_dispatcher_connect(hass, "signal", funct)
async_dispatcher_connect(hass, "signal", other_funct)  # one more required argument
async_dispatcher_send(hass, "signal", "Hello")  # missing 'data2' arg

At the moment there is no automated check to see if either a compatible callable is passed to async_dispatcher_connect or async_dispatcher_send is called with the right arguments.

This PR adds a new generic type SignalType to enable mypy to find these issues and Pylance to provide better intellisense support.

SIGNAL: SignalType[str, int] = SignalType("signal")

async_dispatcher_connect(hass, SIGNAL, funct)
async_dispatcher_send(hass, SIGNAL, "Hello", 2)

async_dispatcher_connect(hass, SIGNAL, other_funct)  # mypy error
async_dispatcher_send(hass, SIGNAL, "Hello", 2, 2)  # type error; mypy can't detect that yet

With the updated typing the last two calls will result in type errors.
Pylance can already detect both whereas mypy seems have a bug with the second one, unfortunately.

--
My first idea was to make SignalType inherit from str to be able to use them interchangeably. Unfortunately, that meant the type checkers would revert back to the str overload if the arguments didn't match defeating the whole purpose of this change. I've added custom __hash__ and __eq__ methods for SignalType to be compatible with string dict keys.

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 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.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

home-assistant bot commented Jan 2, 2024

Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration (cast) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of cast 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 cast 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
Copy link

home-assistant bot commented Jan 2, 2024

Hey there @home-assistant/cloud, mind taking a look at this pull request as it has been labeled with an integration (cloud) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of cloud 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 cloud 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.

@MartinHjelmare
Copy link
Member

The idea sounds ok, but we can't use it if mypy gives an error.

@cdce8p
Copy link
Member Author

cdce8p commented Jan 5, 2024

The idea sounds ok, but we can't use it if mypy gives an error.

The issue with mypy is that it can't detect error due to incorrect typing yet, i.e. a false-positive false-negative. We'll still get the intellisense support and type checking from Pylance / pyright.

Opened an issue for mypy: python/mypy#16739

--
The two type ignores are just to get the overload typing working.

@MartinHjelmare
Copy link
Member

The issue with mypy is that it can't detect error due to incorrect typing yet, i.e. a false-positive.

Isn't that a false negative?

@cdce8p
Copy link
Member Author

cdce8p commented Jan 5, 2024

The issue with mypy is that it can't detect error due to incorrect typing yet, i.e. a false-positive.

Isn't that a false negative?

Yeah, I thought that I might have mixed them up 😅

@MartinHjelmare
Copy link
Member

It's good that the mypy error isn't in the way although it'll give a somewhat false sense of comfort. I'm positive to the change. Let's hear what others think.

@cdce8p
Copy link
Member Author

cdce8p commented Jan 7, 2024

It's good that the mypy error isn't in the way although it'll give a somewhat false sense of comfort.

Opened a PR for Mypy. Hope it makes it in time for the next release. python/mypy#16759
With that, Mypy will output the same errors as pyright for an incorrect usage.

I'm positive to the change. Let's hear what others think.

👍🏻

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.

With the issue/PR upstream in progress, I think it is fine for now.

Thanks, @cdce8p 👍

../Frenck

@frenck frenck merged commit fde7a6e into home-assistant:dev Jan 8, 2024
52 of 53 checks passed
@cdce8p cdce8p deleted the type-dispatcher branch January 8, 2024 08:49
@github-actions github-actions bot locked and limited conversation to collaborators Jan 9, 2024
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.

4 participants