Skip to content

Avoid creating a coordinator in tests#169859

Draft
joostlek wants to merge 3 commits intohome-assistant:devfrom
joostlek:coordinator-tests
Draft

Avoid creating a coordinator in tests#169859
joostlek wants to merge 3 commits intohome-assistant:devfrom
joostlek:coordinator-tests

Conversation

@joostlek
Copy link
Copy Markdown
Member

@joostlek joostlek commented May 5, 2026

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

  • 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:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_tests pylint plugin and enable it via pyproject.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",
),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants