mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Avoid constant alias for integration DOMAIN (#145788)
* Avoid constant alias for integration DOMAIN * Tweak * Improve * Three more --------- Co-authored-by: Shay Levy <levyshay1@gmail.com>
This commit is contained in:
parent
33fc700952
commit
5a727a4fa3
@ -233,6 +233,11 @@ class HassImportsFormatChecker(BaseChecker):
|
|||||||
"hass-import-constant-alias",
|
"hass-import-constant-alias",
|
||||||
"Used when a constant should be imported as an alias",
|
"Used when a constant should be imported as an alias",
|
||||||
),
|
),
|
||||||
|
"W7427": (
|
||||||
|
"`%s` alias is unnecessary for `%s`",
|
||||||
|
"hass-import-constant-unnecessary-alias",
|
||||||
|
"Used when a constant alias is unnecessary",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
options = ()
|
options = ()
|
||||||
|
|
||||||
@ -274,16 +279,24 @@ class HassImportsFormatChecker(BaseChecker):
|
|||||||
self, current_package: str, node: nodes.ImportFrom
|
self, current_package: str, node: nodes.ImportFrom
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Check for improper 'from ._ import _' invocations."""
|
"""Check for improper 'from ._ import _' invocations."""
|
||||||
if node.level <= 1 or (
|
if not current_package.startswith(
|
||||||
not current_package.startswith("homeassistant.components.")
|
("homeassistant.components.", "tests.components.")
|
||||||
and not current_package.startswith("tests.components.")
|
|
||||||
):
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
split_package = current_package.split(".")
|
split_package = current_package.split(".")
|
||||||
|
current_component = split_package[2]
|
||||||
|
|
||||||
|
self._check_for_constant_alias(node, current_component, current_component)
|
||||||
|
|
||||||
|
if node.level <= 1:
|
||||||
|
# No need to check relative import
|
||||||
|
return
|
||||||
|
|
||||||
if not node.modname and len(split_package) == node.level + 1:
|
if not node.modname and len(split_package) == node.level + 1:
|
||||||
for name in node.names:
|
for name in node.names:
|
||||||
# Allow relative import to component root
|
# Allow relative import to component root
|
||||||
if name[0] != split_package[2]:
|
if name[0] != current_component:
|
||||||
self.add_message("hass-absolute-import", node=node)
|
self.add_message("hass-absolute-import", node=node)
|
||||||
return
|
return
|
||||||
return
|
return
|
||||||
@ -298,6 +311,15 @@ class HassImportsFormatChecker(BaseChecker):
|
|||||||
) -> bool:
|
) -> bool:
|
||||||
"""Check for hass-import-constant-alias."""
|
"""Check for hass-import-constant-alias."""
|
||||||
if current_component == imported_component:
|
if current_component == imported_component:
|
||||||
|
# Check for `from homeassistant.components.self import DOMAIN as XYZ`
|
||||||
|
for name, alias in node.names:
|
||||||
|
if name == "DOMAIN" and (alias is not None and alias != "DOMAIN"):
|
||||||
|
self.add_message(
|
||||||
|
"hass-import-constant-unnecessary-alias",
|
||||||
|
node=node,
|
||||||
|
args=(alias, "DOMAIN"),
|
||||||
|
)
|
||||||
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# Check for `from homeassistant.components.other import DOMAIN`
|
# Check for `from homeassistant.components.other import DOMAIN`
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
from homeassistant.components.decora import DOMAIN as DECORA_DOMAIN
|
from homeassistant.components.decora import DOMAIN
|
||||||
from homeassistant.components.light import DOMAIN as PLATFORM_DOMAIN
|
from homeassistant.components.light import DOMAIN as PLATFORM_DOMAIN
|
||||||
from homeassistant.const import CONF_PLATFORM
|
from homeassistant.const import CONF_PLATFORM
|
||||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||||
@ -22,7 +22,7 @@ async def test_repair_issue_is_created(
|
|||||||
{
|
{
|
||||||
PLATFORM_DOMAIN: [
|
PLATFORM_DOMAIN: [
|
||||||
{
|
{
|
||||||
CONF_PLATFORM: DECORA_DOMAIN,
|
CONF_PLATFORM: DOMAIN,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
@ -30,5 +30,5 @@ async def test_repair_issue_is_created(
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert (
|
assert (
|
||||||
HOMEASSISTANT_DOMAIN,
|
HOMEASSISTANT_DOMAIN,
|
||||||
f"deprecated_system_packages_yaml_integration_{DECORA_DOMAIN}",
|
f"deprecated_system_packages_yaml_integration_{DOMAIN}",
|
||||||
) in issue_registry.issues
|
) in issue_registry.issues
|
||||||
|
@ -24,7 +24,7 @@ async def test_repair_issue_is_created(
|
|||||||
"""Test repair issue is created."""
|
"""Test repair issue is created."""
|
||||||
from homeassistant.components.sms import ( # pylint: disable=import-outside-toplevel
|
from homeassistant.components.sms import ( # pylint: disable=import-outside-toplevel
|
||||||
DEPRECATED_ISSUE_ID,
|
DEPRECATED_ISSUE_ID,
|
||||||
DOMAIN as SMS_DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
|
|
||||||
with (
|
with (
|
||||||
@ -33,7 +33,7 @@ async def test_repair_issue_is_created(
|
|||||||
):
|
):
|
||||||
config_entry = MockConfigEntry(
|
config_entry = MockConfigEntry(
|
||||||
title="test",
|
title="test",
|
||||||
domain=SMS_DOMAIN,
|
domain=DOMAIN,
|
||||||
data={
|
data={
|
||||||
CONF_DEVICE: "/dev/ttyUSB0",
|
CONF_DEVICE: "/dev/ttyUSB0",
|
||||||
},
|
},
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
from unittest.mock import Mock, patch
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
from homeassistant.components.image_processing import DOMAIN as IMAGE_PROCESSING_DOMAINN
|
from homeassistant.components.image_processing import DOMAIN as IMAGE_PROCESSING_DOMAINN
|
||||||
from homeassistant.components.tensorflow import CONF_GRAPH, DOMAIN as TENSORFLOW_DOMAIN
|
from homeassistant.components.tensorflow import CONF_GRAPH, DOMAIN
|
||||||
from homeassistant.const import CONF_ENTITY_ID, CONF_MODEL, CONF_PLATFORM, CONF_SOURCE
|
from homeassistant.const import CONF_ENTITY_ID, CONF_MODEL, CONF_PLATFORM, CONF_SOURCE
|
||||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||||
from homeassistant.helpers import issue_registry as ir
|
from homeassistant.helpers import issue_registry as ir
|
||||||
@ -22,7 +22,7 @@ async def test_repair_issue_is_created(
|
|||||||
{
|
{
|
||||||
IMAGE_PROCESSING_DOMAINN: [
|
IMAGE_PROCESSING_DOMAINN: [
|
||||||
{
|
{
|
||||||
CONF_PLATFORM: TENSORFLOW_DOMAIN,
|
CONF_PLATFORM: DOMAIN,
|
||||||
CONF_SOURCE: [
|
CONF_SOURCE: [
|
||||||
{CONF_ENTITY_ID: "camera.test_camera"},
|
{CONF_ENTITY_ID: "camera.test_camera"},
|
||||||
],
|
],
|
||||||
@ -36,5 +36,5 @@ async def test_repair_issue_is_created(
|
|||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert (
|
assert (
|
||||||
HOMEASSISTANT_DOMAIN,
|
HOMEASSISTANT_DOMAIN,
|
||||||
f"deprecated_system_packages_yaml_integration_{TENSORFLOW_DOMAIN}",
|
f"deprecated_system_packages_yaml_integration_{DOMAIN}",
|
||||||
) in issue_registry.issues
|
) in issue_registry.issues
|
||||||
|
Loading…
x
Reference in New Issue
Block a user