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

Refactor code to improve static type checking (DomainEntry dataclass) #248

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

pdcastro
Copy link
Contributor

This PR does not provide any user-visible features, but it provides two development benefits:

  1. It replaces the computation of the following dictionary keys (string concatenation), which was found in copy-and-paste form across 6 files:
we_connect = hass.data[DOMAIN][config_entry.entry_id]
coordinator = hass.data[DOMAIN][config_entry.entry_id + "_coordinator"]
vehicles = hass.data[DOMAIN][config_entry.entry_id + "_vehicles”]

... with a DomainEntry dataclass that allows the following usage:

domain_entry: DomainEntry = hass.data[DOMAIN][config_entry.entry_id]
we_connect = domain_entry.we_connect
coordinator = domain_entry.coordinator
vehicles = domain_entry.vehicles

In the original form, the IDE (VSCode + Pylance) and linters / type checkers like pylint and pyright could not tell the types of variables we_connect, coordinator and vehicles, which were deemed ’unknown’ and affected a chain of type checking wherever those variables were used. In the second form proposed in this PR, type checking is available.

  1. It adds more type hints in several selected functions and several selected files, which again allows the IDE / pylint / pyright to aid with type checking.

Tested with Home Assistant version 2024.1.6.

@mitch-dc mitch-dc merged commit f1edab3 into mitch-dc:main Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants