Avoid creating a coordinator in tests#169859
Draft
joostlek wants to merge 3 commits intohome-assistant:devfrom
Draft
Avoid creating a coordinator in tests#169859joostlek wants to merge 3 commits intohome-assistant:devfrom
joostlek wants to merge 3 commits intohome-assistant:devfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a new custom pylint checker to discourage direct DataUpdateCoordinator (and subclasses) instantiation inside integration test modules, pushing tests toward exercising behavior via entities/state/registries instead of integration internals.
Changes:
- Add
hass_enforce_no_coordinator_instantiation_in_testspylint plugin and enable it viapyproject.toml. - Add dedicated unit tests for the new pylint plugin and wire up fixtures in
tests/pylint/conftest.py. - Suppress the new warning in a set of existing test modules that currently instantiate coordinators directly.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
pylint/plugins/hass_enforce_no_coordinator_instantiation_in_tests.py |
New pylint checker that flags coordinator subclass instantiation in tests.components.<domain>.test_* modules. |
pyproject.toml |
Enables the new pylint plugin by adding it to load-plugins. |
tests/pylint/conftest.py |
Adds fixtures to load and instantiate the new checker for unit tests. |
tests/pylint/test_enforce_no_coordinator_instantiation_in_tests.py |
Adds unit tests validating when the new checker should/should not emit messages. |
tests/components/weatherflow_cloud/test_coordinators.py |
Disables the new warning for this module. |
tests/components/tibber/test_statistics.py |
Disables the new warning for this module. |
tests/components/template/test_trigger_entity.py |
Disables the new warning for this module. |
tests/components/tado/test_helper.py |
Disables the new warning for this module. |
tests/components/opower/test_coordinator.py |
Disables the new warning for this module. |
tests/components/mill/test_coordinator.py |
Disables the new warning for this module. |
tests/components/homeassistant_hardware/test_update.py |
Disables the new warning for this module. |
tests/components/homeassistant_hardware/test_switch.py |
Disables the new warning for this module. |
tests/components/homeassistant_hardware/test_coordinator.py |
Disables the new warning for this module. |
tests/components/fritz/test_coordinator.py |
Disables the new warning for this module. |
tests/components/anglian_water/test_coordinator.py |
Disables the new warning for this module. |
tests/components/amberelectric/test_coordinator.py |
Disables the new warning for this module. |
tests/components/airq/test_coordinator.py |
Disables the new warning for this module. |
Comment on lines
+14
to
+47
| # Stub of the real DataUpdateCoordinator class hierarchy so that astroid can | ||
| # resolve the ``inherits from coordinator base`` check. The ``qname()`` of | ||
| # these classes will be evaluated against the configured base list. | ||
| COORDINATOR_STUB = """ | ||
| import sys | ||
| import types | ||
|
|
||
| _pkg = types.ModuleType("homeassistant") | ||
| _helpers = types.ModuleType("homeassistant.helpers") | ||
| _uc = types.ModuleType("homeassistant.helpers.update_coordinator") | ||
| sys.modules["homeassistant"] = _pkg | ||
| sys.modules["homeassistant.helpers"] = _helpers | ||
| sys.modules["homeassistant.helpers.update_coordinator"] = _uc | ||
|
|
||
|
|
||
| class DataUpdateCoordinator: | ||
| pass | ||
|
|
||
|
|
||
| class TimestampDataUpdateCoordinator(DataUpdateCoordinator): | ||
| pass | ||
|
|
||
|
|
||
| _uc.DataUpdateCoordinator = DataUpdateCoordinator | ||
| _uc.TimestampDataUpdateCoordinator = TimestampDataUpdateCoordinator | ||
| DataUpdateCoordinator.__module__ = "homeassistant.helpers.update_coordinator" | ||
| TimestampDataUpdateCoordinator.__module__ = ( | ||
| "homeassistant.helpers.update_coordinator" | ||
| ) | ||
| """ | ||
|
|
||
|
|
||
| def _parse(code: str, module_name: str) -> astroid.Module: | ||
| """Parse code, ensuring the coordinator stub module is registered.""" |
Comment on lines
+64
to
+81
| @pytest.mark.parametrize( | ||
| ("code", "module_name"), | ||
| [ | ||
| pytest.param( | ||
| """ | ||
| from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
|
|
||
|
|
||
| class FooCoordinator(DataUpdateCoordinator): | ||
| pass | ||
|
|
||
|
|
||
| def test_thing(hass, entry): | ||
| coordinator = FooCoordinator(hass, entry) | ||
| """, | ||
| "tests.components.foo.test_coordinator", | ||
| id="direct_data_update_coordinator_subclass", | ||
| ), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
Avoid creating a coordinator in tests as we want to test our integrations without touching internals. By introducing this pylint check we can discourage new integrations from doing this.
Type of change
Additional information
Checklist
ruff format homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all.To help with the load of incoming pull requests: