Add new rule to enforce relative imports in pylint (#77358)

* Add new rule to enforce relative imports in pylint

* Early return

* Adjust components
This commit is contained in:
epenet 2022-08-29 08:55:32 +02:00 committed by GitHub
parent 067d21a307
commit 8ed689fede
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 46 additions and 23 deletions

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import voluptuous as vol import voluptuous as vol
from homeassistant.components import ads
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -19,6 +18,7 @@ from . import (
STATE_KEY_STATE, STATE_KEY_STATE,
AdsEntity, AdsEntity,
) )
from .. import ads
DEFAULT_NAME = "ADS sensor" DEFAULT_NAME = "ADS sensor"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(

View File

@ -4,7 +4,6 @@ import logging
import requests import requests
from homeassistant.components import ios
from homeassistant.components.notify import ( from homeassistant.components.notify import (
ATTR_DATA, ATTR_DATA,
ATTR_MESSAGE, ATTR_MESSAGE,
@ -15,6 +14,8 @@ from homeassistant.components.notify import (
) )
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .. import ios
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
PUSH_URL = "https://ios-push.home-assistant.io/push" PUSH_URL = "https://ios-push.home-assistant.io/push"

View File

@ -1,7 +1,6 @@
"""Support for Home Assistant iOS app sensors.""" """Support for Home Assistant iOS app sensors."""
from __future__ import annotations from __future__ import annotations
from homeassistant.components import ios
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE from homeassistant.const import PERCENTAGE
@ -12,6 +11,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.icon import icon_for_battery_level from homeassistant.helpers.icon import icon_for_battery_level
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .. import ios
from .const import DOMAIN from .const import DOMAIN
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (

View File

@ -1,7 +1,6 @@
"""Support for MySensors binary sensors.""" """Support for MySensors binary sensors."""
from __future__ import annotations from __future__ import annotations
from homeassistant.components import mysensors
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASSES, DEVICE_CLASSES,
BinarySensorDeviceClass, BinarySensorDeviceClass,
@ -13,6 +12,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import mysensors
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
from .helpers import on_unload from .helpers import on_unload

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.components import mysensors
from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
ATTR_TARGET_TEMP_HIGH, ATTR_TARGET_TEMP_HIGH,
@ -22,6 +21,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import mysensors
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
from .helpers import on_unload from .helpers import on_unload

View File

@ -4,7 +4,6 @@ from __future__ import annotations
from enum import Enum, unique from enum import Enum, unique
from typing import Any from typing import Any
from homeassistant.components import mysensors
from homeassistant.components.cover import ATTR_POSITION, CoverEntity from homeassistant.components.cover import ATTR_POSITION, CoverEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import STATE_OFF, STATE_ON, Platform from homeassistant.const import STATE_OFF, STATE_ON, Platform
@ -12,6 +11,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import mysensors
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
from .helpers import on_unload from .helpers import on_unload

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from typing import Any, cast from typing import Any, cast
from homeassistant.components import mysensors
from homeassistant.components.device_tracker import AsyncSeeCallback from homeassistant.components.device_tracker import AsyncSeeCallback
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -11,6 +10,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify from homeassistant.util import slugify
from .. import mysensors
from .const import ATTR_GATEWAY_ID, DevId, DiscoveryInfo, GatewayId from .const import ATTR_GATEWAY_ID, DevId, DiscoveryInfo, GatewayId
from .helpers import on_unload from .helpers import on_unload

View File

@ -3,7 +3,6 @@ from __future__ import annotations
from typing import Any, cast from typing import Any, cast
from homeassistant.components import mysensors
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_RGB_COLOR, ATTR_RGB_COLOR,
@ -18,6 +17,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.color import rgb_hex_to_rgb_list from homeassistant.util.color import rgb_hex_to_rgb_list
from .. import mysensors
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo, SensorType from .const import MYSENSORS_DISCOVERY, DiscoveryInfo, SensorType
from .device import MySensorsDevice from .device import MySensorsDevice
from .helpers import on_unload from .helpers import on_unload

View File

@ -3,11 +3,11 @@ from __future__ import annotations
from typing import Any from typing import Any
from homeassistant.components import mysensors
from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .. import mysensors
from .const import DevId, DiscoveryInfo from .const import DevId, DiscoveryInfo

View File

@ -5,7 +5,6 @@ from typing import Any
from awesomeversion import AwesomeVersion from awesomeversion import AwesomeVersion
from homeassistant.components import mysensors
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
@ -37,6 +36,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import mysensors
from .const import MYSENSORS_DISCOVERY, DiscoveryInfo from .const import MYSENSORS_DISCOVERY, DiscoveryInfo
from .helpers import on_unload from .helpers import on_unload

View File

@ -5,7 +5,6 @@ from typing import Any
import voluptuous as vol import voluptuous as vol
from homeassistant.components import mysensors
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, Platform
@ -14,6 +13,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import mysensors
from .const import ( from .const import (
DOMAIN as MYSENSORS_DOMAIN, DOMAIN as MYSENSORS_DOMAIN,
MYSENSORS_DISCOVERY, MYSENSORS_DISCOVERY,

View File

@ -5,7 +5,6 @@ import datetime
import voluptuous as vol import voluptuous as vol
from homeassistant.components import pilight
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
from homeassistant.const import ( from homeassistant.const import (
CONF_DISARM_AFTER_TRIGGER, CONF_DISARM_AFTER_TRIGGER,
@ -21,6 +20,8 @@ from homeassistant.helpers.event import track_point_in_time
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from .. import pilight
CONF_VARIABLE = "variable" CONF_VARIABLE = "variable"
CONF_RESET_DELAY_SEC = "reset_delay_sec" CONF_RESET_DELAY_SEC = "reset_delay_sec"

View File

@ -5,7 +5,6 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components import pilight
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_NAME, CONF_PAYLOAD, CONF_UNIT_OF_MEASUREMENT from homeassistant.const import CONF_NAME, CONF_PAYLOAD, CONF_UNIT_OF_MEASUREMENT
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -13,6 +12,8 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .. import pilight
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_VARIABLE = "variable" CONF_VARIABLE = "variable"

View File

@ -17,7 +17,6 @@ from sqlalchemy.sql.expression import literal
from sqlalchemy.sql.lambdas import StatementLambdaElement from sqlalchemy.sql.lambdas import StatementLambdaElement
from sqlalchemy.sql.selectable import Subquery from sqlalchemy.sql.selectable import Subquery
from homeassistant.components import recorder
from homeassistant.components.websocket_api.const import ( from homeassistant.components.websocket_api.const import (
COMPRESSED_STATE_LAST_UPDATED, COMPRESSED_STATE_LAST_UPDATED,
COMPRESSED_STATE_STATE, COMPRESSED_STATE_STATE,
@ -25,6 +24,7 @@ from homeassistant.components.websocket_api.const import (
from homeassistant.core import HomeAssistant, State, split_entity_id from homeassistant.core import HomeAssistant, State, split_entity_id
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from .. import recorder
from .db_schema import RecorderRuns, StateAttributes, States from .db_schema import RecorderRuns, StateAttributes, States
from .filters import Filters from .filters import Filters
from .models import ( from .models import (

View File

@ -1,11 +1,12 @@
"""Support for binary sensors using Tellstick Net.""" """Support for binary sensors using Tellstick Net."""
from homeassistant.components import binary_sensor, tellduslive from homeassistant.components import binary_sensor
from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import tellduslive
from .entry import TelldusLiveEntity from .entry import TelldusLiveEntity

View File

@ -1,7 +1,7 @@
"""Support for Tellstick covers using Tellstick Net.""" """Support for Tellstick covers using Tellstick Net."""
from typing import Any from typing import Any
from homeassistant.components import cover, tellduslive from homeassistant.components import cover
from homeassistant.components.cover import CoverEntity from homeassistant.components.cover import CoverEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -9,6 +9,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import TelldusLiveClient from . import TelldusLiveClient
from .. import tellduslive
from .entry import TelldusLiveEntity from .entry import TelldusLiveEntity

View File

@ -2,13 +2,14 @@
import logging import logging
from typing import Any from typing import Any
from homeassistant.components import light, tellduslive from homeassistant.components import light
from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity from homeassistant.components.light import ATTR_BRIGHTNESS, ColorMode, LightEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import tellduslive
from .entry import TelldusLiveEntity from .entry import TelldusLiveEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -1,7 +1,7 @@
"""Support for Tellstick Net/Telstick Live sensors.""" """Support for Tellstick Net/Telstick Live sensors."""
from __future__ import annotations from __future__ import annotations
from homeassistant.components import sensor, tellduslive from homeassistant.components import sensor
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
@ -23,6 +23,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import tellduslive
from .entry import TelldusLiveEntity from .entry import TelldusLiveEntity
SENSOR_TYPE_TEMPERATURE = "temp" SENSOR_TYPE_TEMPERATURE = "temp"

View File

@ -1,11 +1,12 @@
"""Support for Tellstick switches using Tellstick Net.""" """Support for Tellstick switches using Tellstick Net."""
from homeassistant.components import switch, tellduslive from homeassistant.components import switch
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .. import tellduslive
from .entry import TelldusLiveEntity from .entry import TelldusLiveEntity

View File

@ -3,7 +3,7 @@ import json
import voluptuous as vol import voluptuous as vol
from homeassistant.components import trace, websocket_api from homeassistant.components import websocket_api
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import ( from homeassistant.helpers.dispatcher import (
@ -24,6 +24,8 @@ from homeassistant.helpers.script import (
debug_stop, debug_stop,
) )
from .. import trace
# mypy: allow-untyped-calls, allow-untyped-defs # mypy: allow-untyped-calls, allow-untyped-defs
TRACE_DOMAINS = ("automation", "script") TRACE_DOMAINS = ("automation", "script")

View File

@ -5,7 +5,6 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components import zabbix
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -13,6 +12,8 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .. import zabbix
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
_CONF_TRIGGERS = "triggers" _CONF_TRIGGERS = "triggers"

View File

@ -329,7 +329,13 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
f"{self.current_package}." f"{self.current_package}."
): ):
self.add_message("hass-relative-import", node=node) self.add_message("hass-relative-import", node=node)
elif obsolete_imports := _OBSOLETE_IMPORT.get(node.modname): return
if self.current_package.startswith("homeassistant.components") and node.modname == "homeassistant.components":
for name in node.names:
if name[0] == self.current_package.split(".")[2]:
self.add_message("hass-relative-import", node=node)
return
if obsolete_imports := _OBSOLETE_IMPORT.get(node.modname):
for name_tuple in node.names: for name_tuple in node.names:
for obsolete_import in obsolete_imports: for obsolete_import in obsolete_imports:
if import_match := obsolete_import.constant.match(name_tuple[0]): if import_match := obsolete_import.constant.match(name_tuple[0]):

View File

@ -84,6 +84,12 @@ def test_good_import(
"CONSTANT", "CONSTANT",
"hass-absolute-import", "hass-absolute-import",
), ),
(
"homeassistant.components.pylint_test.api.hub",
"homeassistant.components",
"pylint_test",
"hass-relative-import",
),
], ],
) )
def test_bad_import( def test_bad_import(
@ -111,7 +117,7 @@ def test_bad_import(
line=1, line=1,
col_offset=0, col_offset=0,
end_line=1, end_line=1,
end_col_offset=len(import_from) + 21, end_col_offset=len(import_from) + len(import_what) + 13,
), ),
): ):
imports_checker.visit_importfrom(import_node) imports_checker.visit_importfrom(import_node)