mirror of
https://github.com/home-assistant/core.git
synced 2025-12-27 14:31:13 +00:00
Pylint plugin to check that relative imports are used (#50937)
* Pylint plugin to check that relative imports are used * Fix existing sites * Update description message * Fix typo
This commit is contained in:
committed by
GitHub
parent
b704f0e729
commit
016abda12e
@@ -1,5 +1,5 @@
|
||||
"""Plugin for constructor definitions."""
|
||||
from astroid import ClassDef, Const, FunctionDef
|
||||
from astroid import Const, FunctionDef
|
||||
from pylint.checkers import BaseChecker
|
||||
from pylint.interfaces import IAstroidChecker
|
||||
from pylint.lint import PyLinter
|
||||
@@ -43,7 +43,7 @@ class HassConstructorFormatChecker(BaseChecker): # type: ignore[misc]
|
||||
return
|
||||
|
||||
# Check that return type is specified and it is "None".
|
||||
if not isinstance(node.returns, Const) or node.returns.value != None:
|
||||
if not isinstance(node.returns, Const) or node.returns.value is not None:
|
||||
self.add_message("hass-constructor-return", node=node)
|
||||
|
||||
|
||||
|
||||
52
pylint/plugins/hass_imports.py
Normal file
52
pylint/plugins/hass_imports.py
Normal file
@@ -0,0 +1,52 @@
|
||||
"""Plugin for checking imports."""
|
||||
from __future__ import annotations
|
||||
|
||||
from astroid import Import, ImportFrom, Module
|
||||
from pylint.checkers import BaseChecker
|
||||
from pylint.interfaces import IAstroidChecker
|
||||
from pylint.lint import PyLinter
|
||||
|
||||
|
||||
class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
|
||||
"""Checker for imports."""
|
||||
|
||||
__implements__ = IAstroidChecker
|
||||
|
||||
name = "hass_imports"
|
||||
priority = -1
|
||||
msgs = {
|
||||
"W0011": (
|
||||
"Relative import should be used",
|
||||
"hass-relative-import",
|
||||
"Used when absolute import should be replaced with relative import",
|
||||
),
|
||||
}
|
||||
options = ()
|
||||
|
||||
def __init__(self, linter: PyLinter | None = None) -> None:
|
||||
super().__init__(linter)
|
||||
self.current_module: str | None = None
|
||||
|
||||
def visit_module(self, node: Module) -> None:
|
||||
"""Called when a Import node is visited."""
|
||||
self.current_module = node.name
|
||||
|
||||
def visit_import(self, node: Import) -> None:
|
||||
"""Called when a Import node is visited."""
|
||||
for module, _alias in node.names:
|
||||
if module.startswith(f"{self.current_module}."):
|
||||
self.add_message("hass-relative-import", node=node)
|
||||
|
||||
def visit_importfrom(self, node: ImportFrom) -> None:
|
||||
"""Called when a ImportFrom node is visited."""
|
||||
if node.level is not None:
|
||||
return
|
||||
if node.modname == self.current_module or node.modname.startswith(
|
||||
f"{self.current_module}."
|
||||
):
|
||||
self.add_message("hass-relative-import", node=node)
|
||||
|
||||
|
||||
def register(linter: PyLinter) -> None:
|
||||
"""Register the checker."""
|
||||
linter.register_checker(HassImportsFormatChecker(linter))
|
||||
Reference in New Issue
Block a user