Run flake8 on more files (#85333)

Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: Dave T <17680170+davet2001@users.noreply.github.com>
This commit is contained in:
Max R 2023-01-16 14:53:14 -05:00 committed by GitHub
parent 3aad153913
commit 156c815499
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 19 additions and 15 deletions

View file

@ -40,7 +40,7 @@ repos:
- flake8-comprehensions==3.10.1
- flake8-noqa==1.3.0
- mccabe==0.7.0
files: ^(homeassistant|script|tests)/.+\.py$
exclude: docs/source/conf.py
- repo: https://github.com/PyCQA/bandit
rev: 1.7.4
hooks:

View file

@ -1,6 +1,5 @@
"""
Sphinx extension to add ReadTheDocs-style "Edit on GitHub" links to the
sidebar.
Sphinx extension for ReadTheDocs-style "Edit on GitHub" links on the sidebar.
Loosely based on https://github.com/astropy/astropy/pull/347
"""
@ -12,6 +11,7 @@ __licence__ = "BSD (3 clause)"
def get_github_url(app, view, path):
"""Build the GitHub URL."""
return (
f"https://github.com/{app.config.edit_on_github_project}/"
f"{view}/{app.config.edit_on_github_branch}/"
@ -20,6 +20,7 @@ def get_github_url(app, view, path):
def html_page_context(app, pagename, templatename, context, doctree):
"""Build the HTML page."""
if templatename != "page.html":
return
@ -38,6 +39,7 @@ def html_page_context(app, pagename, templatename, context, doctree):
def setup(app):
"""Set up the app."""
app.add_config_value("edit_on_github_project", "", True)
app.add_config_value("edit_on_github_branch", "master", True)
app.add_config_value("edit_on_github_src_path", "", True) # 'eg' "docs/"

View file

@ -22,7 +22,7 @@ class HassConstructorFormatChecker(BaseChecker): # type: ignore[misc]
options = ()
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
"""Called when a FunctionDef node is visited."""
"""Check for improperly typed `__init__` definitions."""
if not node.is_method() or node.name != "__init__":
return

View file

@ -20,7 +20,7 @@ if TYPE_CHECKING:
class _Special(Enum):
"""Sentinel values"""
"""Sentinel values."""
UNDEFINED = 1
@ -2837,7 +2837,7 @@ def _has_valid_annotations(
def _get_module_platform(module_name: str) -> str | None:
"""Called when a Module node is visited."""
"""Return the platform for the module name."""
if not (module_match := _MODULE_REGEX.match(module_name)):
# Ensure `homeassistant.components.<component>`
# Or `homeassistant.components.<component>.<platform>`
@ -2878,12 +2878,13 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
)
def __init__(self, linter: PyLinter | None = None) -> None:
"""Initialize the HassTypeHintChecker."""
super().__init__(linter)
self._function_matchers: list[TypeHintMatch] = []
self._class_matchers: list[ClassTypeHintMatch] = []
def visit_module(self, node: nodes.Module) -> None:
"""Called when a Module node is visited."""
"""Populate matchers for a Module node."""
self._function_matchers = []
self._class_matchers = []
@ -2907,7 +2908,7 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
self._class_matchers.reverse()
def visit_classdef(self, node: nodes.ClassDef) -> None:
"""Called when a ClassDef node is visited."""
"""Apply relevant type hint checks on a ClassDef node."""
ancestor: nodes.ClassDef
checked_class_methods: set[str] = set()
ancestors = list(node.ancestors()) # cache result for inside loop
@ -2934,7 +2935,7 @@ class HassTypeHintChecker(BaseChecker): # type: ignore[misc]
checked_class_methods.add(function_node.name)
def visit_functiondef(self, node: nodes.FunctionDef) -> None:
"""Called when a FunctionDef node is visited."""
"""Apply relevant type hint checks on a FunctionDef node."""
for match in self._function_matchers:
if not match.need_to_check_function(node) or node.is_method():
continue

View file

@ -396,11 +396,12 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
options = ()
def __init__(self, linter: PyLinter | None = None) -> None:
"""Initialize the HassImportsFormatChecker."""
super().__init__(linter)
self.current_package: str | None = None
def visit_module(self, node: nodes.Module) -> None:
"""Called when a Module node is visited."""
"""Determine current package."""
if node.package:
self.current_package = node.name
else:
@ -408,7 +409,7 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
self.current_package = node.name[: node.name.rfind(".")]
def visit_import(self, node: nodes.Import) -> None:
"""Called when a Import node is visited."""
"""Check for improper `import _` invocations."""
if self.current_package is None:
return
for module, _alias in node.names:
@ -430,7 +431,7 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
def _visit_importfrom_relative(
self, current_package: str, node: nodes.ImportFrom
) -> None:
"""Called when a ImportFrom node is visited."""
"""Check for improper 'from ._ import _' invocations."""
if (
node.level <= 1
or not current_package.startswith("homeassistant.components.")
@ -449,7 +450,7 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
self.add_message("hass-absolute-import", node=node)
def visit_importfrom(self, node: nodes.ImportFrom) -> None:
"""Called when a ImportFrom node is visited."""
"""Check for improper 'from _ import _' invocations."""
if not self.current_package:
return
if node.level is not None:

View file

@ -29,13 +29,13 @@ class HassLoggerFormatChecker(BaseChecker): # type: ignore[misc]
options = ()
def visit_call(self, node: nodes.Call) -> None:
"""Called when a Call node is visited."""
"""Check for improper log messages."""
if not isinstance(node.func, nodes.Attribute) or not isinstance(
node.func.expr, nodes.Name
):
return
if not node.func.expr.name in LOGGER_NAMES:
if node.func.expr.name not in LOGGER_NAMES:
return
if not node.args: