From 61fbfe234abef4089b6fe35edc0b936a8a55ab9c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 11 Jun 2024 06:39:05 +0000 Subject: [PATCH 1/2] Fix import pylint warning in core tests --- tests/common.py | 27 ++++++++++++++------------- tests/test_backports.py | 2 +- tests/test_block_async_io.py | 2 +- tests/test_config_entries.py | 3 +-- tests/test_const.py | 2 +- tests/test_loader.py | 5 ++++- 6 files changed, 22 insertions(+), 19 deletions(-) diff --git a/tests/common.py b/tests/common.py index 732970e108b3f..9faf7e1071223 100644 --- a/tests/common.py +++ b/tests/common.py @@ -71,7 +71,6 @@ issue_registry as ir, label_registry as lr, recorder as recorder_helper, - restore_state, restore_state as rs, storage, translation, @@ -100,7 +99,7 @@ from homeassistant.util.unit_system import METRIC_SYSTEM import homeassistant.util.yaml.loader as yaml_loader -from tests.testing_config.custom_components.test_constant_deprecation import ( +from .testing_config.custom_components.test_constant_deprecation import ( import_deprecated_constant, ) @@ -1133,6 +1132,7 @@ def init_recorder_component(hass, add_config=None, db_url="sqlite://"): """Initialize the recorder.""" # Local import to avoid processing recorder and SQLite modules when running a # testcase which does not use the recorder. + # pylint: disable-next=import-outside-toplevel from homeassistant.components import recorder config = dict(add_config) if add_config else {} @@ -1154,8 +1154,8 @@ def init_recorder_component(hass, add_config=None, db_url="sqlite://"): def mock_restore_cache(hass: HomeAssistant, states: Sequence[State]) -> None: """Mock the DATA_RESTORE_CACHE.""" - key = restore_state.DATA_RESTORE_STATE - data = restore_state.RestoreStateData(hass) + key = rs.DATA_RESTORE_STATE + data = rs.RestoreStateData(hass) now = dt_util.utcnow() last_states = {} @@ -1167,14 +1167,14 @@ def mock_restore_cache(hass: HomeAssistant, states: Sequence[State]) -> None: json.dumps(restored_state["attributes"], cls=JSONEncoder) ), } - last_states[state.entity_id] = restore_state.StoredState.from_dict( + last_states[state.entity_id] = rs.StoredState.from_dict( {"state": restored_state, "last_seen": now} ) data.last_states = last_states _LOGGER.debug("Restore cache: %s", data.last_states) assert len(data.last_states) == len(states), f"Duplicate entity_id? {states}" - restore_state.async_get.cache_clear() + rs.async_get.cache_clear() hass.data[key] = data @@ -1182,8 +1182,8 @@ def mock_restore_cache_with_extra_data( hass: HomeAssistant, states: Sequence[tuple[State, Mapping[str, Any]]] ) -> None: """Mock the DATA_RESTORE_CACHE.""" - key = restore_state.DATA_RESTORE_STATE - data = restore_state.RestoreStateData(hass) + key = rs.DATA_RESTORE_STATE + data = rs.RestoreStateData(hass) now = dt_util.utcnow() last_states = {} @@ -1195,22 +1195,22 @@ def mock_restore_cache_with_extra_data( json.dumps(restored_state["attributes"], cls=JSONEncoder) ), } - last_states[state.entity_id] = restore_state.StoredState.from_dict( + last_states[state.entity_id] = rs.StoredState.from_dict( {"state": restored_state, "extra_data": extra_data, "last_seen": now} ) data.last_states = last_states _LOGGER.debug("Restore cache: %s", data.last_states) assert len(data.last_states) == len(states), f"Duplicate entity_id? {states}" - restore_state.async_get.cache_clear() + rs.async_get.cache_clear() hass.data[key] = data async def async_mock_restore_state_shutdown_restart( hass: HomeAssistant, -) -> restore_state.RestoreStateData: +) -> rs.RestoreStateData: """Mock shutting down and saving restore state and restoring.""" - data = restore_state.async_get(hass) + data = rs.async_get(hass) await data.async_dump_states() await async_mock_load_restore_state_from_storage(hass) return data @@ -1223,7 +1223,7 @@ async def async_mock_load_restore_state_from_storage( hass_storage must already be mocked. """ - await restore_state.async_get(hass).async_load() + await rs.async_get(hass).async_load() class MockEntity(entity.Entity): @@ -1571,6 +1571,7 @@ def async_get_persistent_notifications( def async_mock_cloud_connection_status(hass: HomeAssistant, connected: bool) -> None: """Mock a signal the cloud disconnected.""" + # pylint: disable-next=import-outside-toplevel from homeassistant.components.cloud import ( SIGNAL_CLOUD_CONNECTION_STATE, CloudConnectionState, diff --git a/tests/test_backports.py b/tests/test_backports.py index 09c11da37cba4..4df0a9e3f573f 100644 --- a/tests/test_backports.py +++ b/tests/test_backports.py @@ -14,7 +14,7 @@ functools as backports_functools, ) -from tests.common import import_and_test_deprecated_alias +from .common import import_and_test_deprecated_alias @pytest.mark.parametrize( diff --git a/tests/test_block_async_io.py b/tests/test_block_async_io.py index 5a1e38d78cda8..eab8033e37daf 100644 --- a/tests/test_block_async_io.py +++ b/tests/test_block_async_io.py @@ -14,7 +14,7 @@ from homeassistant import block_async_io from homeassistant.core import HomeAssistant -from tests.common import extract_stack_to_frame +from .common import extract_stack_to_frame async def test_protect_loop_debugger_sleep(caplog: pytest.LogCaptureFixture) -> None: diff --git a/tests/test_config_entries.py b/tests/test_config_entries.py index 5c2bf8b205b24..0208b33169c83 100644 --- a/tests/test_config_entries.py +++ b/tests/test_config_entries.py @@ -44,13 +44,12 @@ MockPlatform, async_capture_events, async_fire_time_changed, + async_get_persistent_notifications, mock_config_flow, mock_integration, mock_platform, ) -from tests.common import async_get_persistent_notifications - @pytest.fixture(autouse=True) def mock_handlers() -> Generator[None]: diff --git a/tests/test_const.py b/tests/test_const.py index 63b01388dd7cf..a6a2387b091a3 100644 --- a/tests/test_const.py +++ b/tests/test_const.py @@ -7,7 +7,7 @@ from homeassistant import const from homeassistant.components import sensor -from tests.common import ( +from .common import ( help_test_all, import_and_test_deprecated_constant, import_and_test_deprecated_constant_enum, diff --git a/tests/test_loader.py b/tests/test_loader.py index 328b55ddf80b7..b195de6006b14 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -131,6 +131,7 @@ async def test_custom_component_name( assert platform.__package__ == "custom_components.test" # Test custom components is mounted + # pylint: disable-next=import-outside-toplevel from custom_components.test_package import TEST assert TEST == 5 @@ -1247,14 +1248,16 @@ def test_import_executor_default(hass: HomeAssistant) -> None: assert built_in_comp.import_executor is True -async def test_config_folder_not_in_path(hass): +async def test_config_folder_not_in_path() -> None: """Test that config folder is not in path.""" # Verify that we are unable to import this file from top level with pytest.raises(ImportError): + # pylint: disable-next=import-outside-toplevel import check_config_not_in_path # noqa: F401 # Verify that we are able to load the file with absolute path + # pylint: disable-next=import-outside-toplevel,hass-relative-import import tests.testing_config.check_config_not_in_path # noqa: F401 From f93d99be6a300c564bf1688a16494438b17871b8 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 11 Jun 2024 06:42:29 +0000 Subject: [PATCH 2/2] Also adjust conftest.py --- tests/components/conftest.py | 40 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/tests/components/conftest.py b/tests/components/conftest.py index e44479873d8ad..42746525a0da7 100644 --- a/tests/components/conftest.py +++ b/tests/components/conftest.py @@ -54,7 +54,8 @@ def entity_registry_enabled_by_default() -> Generator[None]: @pytest.fixture(name="stub_blueprint_populate") def stub_blueprint_populate_fixture() -> Generator[None]: """Stub copying the blueprints to the config folder.""" - from tests.components.blueprint.common import stub_blueprint_populate_fixture_helper + # pylint: disable-next=import-outside-toplevel + from .blueprint.common import stub_blueprint_populate_fixture_helper yield from stub_blueprint_populate_fixture_helper() @@ -63,7 +64,8 @@ def stub_blueprint_populate_fixture() -> Generator[None]: @pytest.fixture(name="mock_tts_get_cache_files") def mock_tts_get_cache_files_fixture() -> Generator[MagicMock]: """Mock the list TTS cache function.""" - from tests.components.tts.common import mock_tts_get_cache_files_fixture_helper + # pylint: disable-next=import-outside-toplevel + from .tts.common import mock_tts_get_cache_files_fixture_helper yield from mock_tts_get_cache_files_fixture_helper() @@ -73,7 +75,8 @@ def mock_tts_init_cache_dir_fixture( init_tts_cache_dir_side_effect: Any, ) -> Generator[MagicMock]: """Mock the TTS cache dir in memory.""" - from tests.components.tts.common import mock_tts_init_cache_dir_fixture_helper + # pylint: disable-next=import-outside-toplevel + from .tts.common import mock_tts_init_cache_dir_fixture_helper yield from mock_tts_init_cache_dir_fixture_helper(init_tts_cache_dir_side_effect) @@ -81,9 +84,8 @@ def mock_tts_init_cache_dir_fixture( @pytest.fixture(name="init_tts_cache_dir_side_effect") def init_tts_cache_dir_side_effect_fixture() -> Any: """Return the cache dir.""" - from tests.components.tts.common import ( - init_tts_cache_dir_side_effect_fixture_helper, - ) + # pylint: disable-next=import-outside-toplevel + from .tts.common import init_tts_cache_dir_side_effect_fixture_helper return init_tts_cache_dir_side_effect_fixture_helper() @@ -96,7 +98,8 @@ def mock_tts_cache_dir_fixture( request: pytest.FixtureRequest, ) -> Generator[Path]: """Mock the TTS cache dir with empty dir.""" - from tests.components.tts.common import mock_tts_cache_dir_fixture_helper + # pylint: disable-next=import-outside-toplevel + from .tts.common import mock_tts_cache_dir_fixture_helper yield from mock_tts_cache_dir_fixture_helper( tmp_path, mock_tts_init_cache_dir, mock_tts_get_cache_files, request @@ -106,7 +109,8 @@ def mock_tts_cache_dir_fixture( @pytest.fixture(name="tts_mutagen_mock") def tts_mutagen_mock_fixture() -> Generator[MagicMock]: """Mock writing tags.""" - from tests.components.tts.common import tts_mutagen_mock_fixture_helper + # pylint: disable-next=import-outside-toplevel + from .tts.common import tts_mutagen_mock_fixture_helper yield from tts_mutagen_mock_fixture_helper() @@ -114,9 +118,8 @@ def tts_mutagen_mock_fixture() -> Generator[MagicMock]: @pytest.fixture(name="mock_conversation_agent") def mock_conversation_agent_fixture(hass: HomeAssistant) -> MockAgent: """Mock a conversation agent.""" - from tests.components.conversation.common import ( - mock_conversation_agent_fixture_helper, - ) + # pylint: disable-next=import-outside-toplevel + from .conversation.common import mock_conversation_agent_fixture_helper return mock_conversation_agent_fixture_helper(hass) @@ -133,7 +136,8 @@ def prevent_ffmpeg_subprocess() -> Generator[None]: @pytest.fixture def mock_light_entities() -> list[MockLight]: """Return mocked light entities.""" - from tests.components.light.common import MockLight + # pylint: disable-next=import-outside-toplevel + from .light.common import MockLight return [ MockLight("Ceiling", STATE_ON), @@ -145,7 +149,8 @@ def mock_light_entities() -> list[MockLight]: @pytest.fixture def mock_sensor_entities() -> dict[str, MockSensor]: """Return mocked sensor entities.""" - from tests.components.sensor.common import get_mock_sensor_entities + # pylint: disable-next=import-outside-toplevel + from .sensor.common import get_mock_sensor_entities return get_mock_sensor_entities() @@ -153,7 +158,8 @@ def mock_sensor_entities() -> dict[str, MockSensor]: @pytest.fixture def mock_switch_entities() -> list[MockSwitch]: """Return mocked toggle entities.""" - from tests.components.switch.common import get_mock_switch_entities + # pylint: disable-next=import-outside-toplevel + from .switch.common import get_mock_switch_entities return get_mock_switch_entities() @@ -161,7 +167,8 @@ def mock_switch_entities() -> list[MockSwitch]: @pytest.fixture def mock_legacy_device_scanner() -> MockScanner: """Return mocked legacy device scanner entity.""" - from tests.components.device_tracker.common import MockScanner + # pylint: disable-next=import-outside-toplevel + from .device_tracker.common import MockScanner return MockScanner() @@ -169,6 +176,7 @@ def mock_legacy_device_scanner() -> MockScanner: @pytest.fixture def mock_legacy_device_tracker_setup() -> Callable[[HomeAssistant, MockScanner], None]: """Return setup callable for legacy device tracker setup.""" - from tests.components.device_tracker.common import mock_legacy_device_tracker_setup + # pylint: disable-next=import-outside-toplevel + from .device_tracker.common import mock_legacy_device_tracker_setup return mock_legacy_device_tracker_setup