Skip to content

Commit

Permalink
Run flake8 on more files (home-assistant#85333)
Browse files Browse the repository at this point in the history
Co-authored-by: epenet <[email protected]>
Co-authored-by: Dave T <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2023
1 parent 3aad153 commit 156c815
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions docs/source/_ext/edit_on_github.py
Original file line number Diff line number Diff line change
@@ -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
"""
Expand All @@ -12,6 +11,7 @@


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}/"
Expand All @@ -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

Expand All @@ -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/"
Expand Down
2 changes: 1 addition & 1 deletion pylint/plugins/hass_constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions pylint/plugins/hass_enforce_type_hints.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


class _Special(Enum):
"""Sentinel values"""
"""Sentinel values."""

UNDEFINED = 1

Expand Down Expand Up @@ -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>`
Expand Down Expand Up @@ -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 = []

Expand All @@ -2907,7 +2908,7 @@ def visit_module(self, node: nodes.Module) -> None:
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
Expand All @@ -2934,7 +2935,7 @@ def _visit_class_functions(
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
Expand Down
9 changes: 5 additions & 4 deletions pylint/plugins/hass_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,20 @@ 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:
# Strip name of the current module
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:
Expand All @@ -430,7 +431,7 @@ def visit_import(self, node: nodes.Import) -> None:
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.")
Expand All @@ -449,7 +450,7 @@ def _visit_importfrom_relative(
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:
Expand Down
4 changes: 2 additions & 2 deletions pylint/plugins/hass_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 156c815

Please sign in to comment.