Force root import of references from other components (#125816)

* Force root import of references from other components

* Improve

* Adjust

* Tweak exceptions

* Another

* Another

* Another

* Another

* Another

* Another

* Another

* Another

* Another

* Another

* Another

* Another

* Adjust

* More

* Ignore violations in test

* Improve
This commit is contained in:
epenet 2024-09-16 11:28:42 +02:00 committed by GitHub
parent 9f1cc638c9
commit 02cb6a6af7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 72 additions and 6 deletions

View File

@ -394,6 +394,31 @@ _OBSOLETE_IMPORT: dict[str, list[ObsoleteImportMatch]] = {
],
}
_IGNORE_ROOT_IMPORT = (
"assist_pipeline",
"automation",
"bluetooth",
"camera",
"cast",
"device_automation",
"device_tracker",
"ffmpeg",
"ffmpeg_motion",
"google_assistant",
"hardware",
"homeassistant",
"homeassistant_hardware",
"http",
"manual",
"plex",
"recorder",
"rest",
"script",
"sensor",
"stream",
"zha",
)
# Blacklist of imports that should be using the namespace
@dataclass
@ -489,8 +514,9 @@ class HassImportsFormatChecker(BaseChecker):
if module.startswith(f"{self.current_package}."):
self.add_message("hass-relative-import", node=node)
continue
if module.startswith("homeassistant.components.") and module.endswith(
"const"
if (
module.startswith("homeassistant.components.")
and len(module.split(".")) > 3
):
if (
self.current_package.startswith("tests.components.")
@ -546,11 +572,17 @@ class HassImportsFormatChecker(BaseChecker):
self.add_message("hass-relative-import", node=node)
return
if node.modname.startswith("homeassistant.components.") and not (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == node.modname.split(".")[2]
if (
node.modname.startswith("homeassistant.components.")
and (module_parts := node.modname.split("."))
and (module_integration := module_parts[2])
and module_integration not in _IGNORE_ROOT_IMPORT
and not (
self.current_package.startswith("tests.components.")
and self.current_package.split(".")[2] == module_integration
)
):
if node.modname.endswith(".const"):
if len(module_parts) > 3:
self.add_message("hass-component-root-import", node=node)
return
for name, alias in node.names:

View File

@ -14,6 +14,8 @@ from hass_nabucasa.voice import TTS_VOICES
import pytest
from homeassistant.components.alexa import errors as alexa_errors
# pylint: disable-next=hass-component-root-import
from homeassistant.components.alexa.entities import LightCapabilities
from homeassistant.components.assist_pipeline.pipeline import STORAGE_KEY
from homeassistant.components.cloud.const import DEFAULT_EXPOSED_DOMAINS, DOMAIN

View File

@ -7,6 +7,8 @@ from pytest_unordered import unordered
from homeassistant.components.automation import DOMAIN as AUTOMATION_DOMAIN
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
# pylint: disable-next=hass-component-root-import
from homeassistant.components.binary_sensor.device_trigger import (
CONF_BAT_LOW,
CONF_NOT_BAT_LOW,

View File

@ -30,6 +30,8 @@ from homeassistant.components.assist_satellite import (
AssistSatelliteEntity,
AssistSatelliteEntityFeature,
)
# pylint: disable-next=hass-component-root-import
from homeassistant.components.assist_satellite.entity import AssistSatelliteState
from homeassistant.components.esphome import DOMAIN
from homeassistant.components.esphome.assist_satellite import (

View File

@ -9,10 +9,20 @@ from pytest_unordered import unordered
from homeassistant.components.camera import CameraEntityFeature
from homeassistant.components.climate import ATTR_MAX_TEMP, ATTR_MIN_TEMP, HVACMode
# pylint: disable-next=hass-component-root-import
from homeassistant.components.demo.binary_sensor import DemoBinarySensor
# pylint: disable-next=hass-component-root-import
from homeassistant.components.demo.cover import DemoCover
# pylint: disable-next=hass-component-root-import
from homeassistant.components.demo.light import LIGHT_EFFECT_LIST, DemoLight
# pylint: disable-next=hass-component-root-import
from homeassistant.components.demo.media_player import AbstractDemoPlayer
# pylint: disable-next=hass-component-root-import
from homeassistant.components.demo.switch import DemoSwitch
from homeassistant.components.google_assistant import (
EVENT_COMMAND_RECEIVED,

View File

@ -11,6 +11,8 @@ import pytest
import voluptuous as vol
from homeassistant.components import logbook, recorder
# pylint: disable-next=hass-component-root-import
from homeassistant.components.alexa.smart_home import EVENT_ALEXA_SMART_HOME
from homeassistant.components.automation import EVENT_AUTOMATION_TRIGGERED
from homeassistant.components.logbook.models import EventAsRow, LazyEventPartialState

View File

@ -8,6 +8,8 @@ import pytest
from pytraccar import TraccarException
from homeassistant import config_entries
# pylint: disable-next=hass-component-root-import
from homeassistant.components.traccar.device_tracker import PLATFORM_SCHEMA
from homeassistant.components.traccar_server.const import (
CONF_CUSTOM_ATTRIBUTES,

View File

@ -13,6 +13,8 @@ from voip_utils import CallInfo
from homeassistant.components import assist_pipeline, assist_satellite, tts, voip
from homeassistant.components.assist_satellite import AssistSatelliteEntity
# pylint: disable-next=hass-component-root-import
from homeassistant.components.assist_satellite.entity import AssistSatelliteState
from homeassistant.components.voip import HassVoipDatagramProtocol
from homeassistant.components.voip.assist_satellite import Tones, VoipAssistSatellite

View File

@ -51,11 +51,15 @@ from homeassistant.auth.const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY
from homeassistant.auth.models import Credentials
from homeassistant.auth.providers import homeassistant
from homeassistant.components.device_tracker.legacy import Device
# pylint: disable-next=hass-component-root-import
from homeassistant.components.websocket_api.auth import (
TYPE_AUTH,
TYPE_AUTH_OK,
TYPE_AUTH_REQUIRED,
)
# pylint: disable-next=hass-component-root-import
from homeassistant.components.websocket_api.http import URL
from homeassistant.config import YAML_CONFIG_FILE
from homeassistant.config_entries import ConfigEntries, ConfigEntry, ConfigEntryState

View File

@ -208,6 +208,10 @@ def test_good_root_import(
"from homeassistant.components.climate.const import ClimateEntityFeature",
"homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.climate.entity import ClimateEntityFeature",
"homeassistant.components.pylint_test.climate",
),
(
"from homeassistant.components.climate import const",
"tests.components.pylint_test.climate",
@ -220,6 +224,10 @@ def test_good_root_import(
"import homeassistant.components.climate.const as climate",
"tests.components.pylint_test.climate",
),
(
"import homeassistant.components.climate.entity as climate",
"tests.components.pylint_test.climate",
),
],
)
def test_bad_root_import(