From ef3d7189b8bd52884b8452c23ecde7679a20c635 Mon Sep 17 00:00:00 2001 From: Sid Srinivasan Date: Mon, 16 Jun 2025 12:31:15 -0500 Subject: [PATCH 1/6] pre-commit reformatting --- .github/workflows/code_quality_checks.yml | 11 +++++++++-- .pre-commit-config.yaml | 6 ++++++ nodescraper/interfaces/dataanalyzertask.py | 4 ++-- nodescraper/interfaces/task.py | 2 +- nodescraper/models/event.py | 2 +- .../plugins/inband/cmdline/cmdline_analyzer.py | 10 ++++++---- nodescraper/resultcollators/tablesummary.py | 8 +++++--- pyproject.toml | 3 ++- 8 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.github/workflows/code_quality_checks.yml b/.github/workflows/code_quality_checks.yml index 48d2d82..9ca1775 100644 --- a/.github/workflows/code_quality_checks.yml +++ b/.github/workflows/code_quality_checks.yml @@ -6,6 +6,9 @@ permissions: on: [pull_request] +permissions: + contents: read + jobs: pre-commit: runs-on: [ self-hosted ] @@ -13,8 +16,12 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: pre-commit/action@v3.0.1 - + - name: setup environment + run: | + ./dev-setup.sh + - name: run pre-commit hooks + run: | + pre-commit run --all-files --show-diff-on-failure --color=always - name: Print message on failure if: failure() run: | diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 04e4a7b..03f9342 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,3 +15,9 @@ repos: rev: 25.1.0 hooks: - id: black + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.15.0 + hooks: + - id: mypy + args: [--install-types, --non-interactive, --explicit-package-bases, --allow-redefinition] + language: system diff --git a/nodescraper/interfaces/dataanalyzertask.py b/nodescraper/interfaces/dataanalyzertask.py index 116ae02..23e419a 100644 --- a/nodescraper/interfaces/dataanalyzertask.py +++ b/nodescraper/interfaces/dataanalyzertask.py @@ -125,13 +125,13 @@ def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None: def analyze_data( self, data: TDataModel, - args: Optional[TAnalyzeArg | dict], + args: Optional[TAnalyzeArg], ) -> TaskResult: """Analyze the provided data and return a TaskResult Args: data (TDataModel): data to analyze - args (Optional[TAnalyzeArg | dict]): Optional arguments for analysis, can be a model or dict + args (Optional[TAnalyzeArg]): Optional arguments for analysis. Dicts will be handled in the decorator" Returns: TaskResult: Task result containing the analysis outcome diff --git a/nodescraper/interfaces/task.py b/nodescraper/interfaces/task.py index ce2ed9b..85d2cf2 100644 --- a/nodescraper/interfaces/task.py +++ b/nodescraper/interfaces/task.py @@ -81,7 +81,7 @@ def max_event_priority_level(self, input_value: str | EventPriority): if isinstance(input_value, str): value: EventPriority = getattr(EventPriority, input_value) elif isinstance(input_value, EventPriority): - value: EventPriority = input_value + value: EventPriority = input_value # type:ignore else: raise ValueError(f"Invalid type for max_event_priority_level: {type(input_value)}") diff --git a/nodescraper/models/event.py b/nodescraper/models/event.py index 0a759b0..3db02c3 100644 --- a/nodescraper/models/event.py +++ b/nodescraper/models/event.py @@ -51,7 +51,7 @@ class Event(BaseModel): default_factory=lambda: datetime.datetime.now(datetime.timezone.utc) ) reporter: str = "ERROR_SCRAPER" - category: str + category: str | Enum description: str data: dict = Field(default_factory=dict) priority: EventPriority diff --git a/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py b/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py index 69a1c6b..9902b18 100644 --- a/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py +++ b/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py @@ -23,7 +23,7 @@ # SOFTWARE. # ############################################################################### -from typing import Optional +from typing import Any, Optional from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus from nodescraper.interfaces import DataAnalyzer @@ -38,13 +38,15 @@ class CmdlineAnalyzer(DataAnalyzer[CmdlineDataModel, CmdlineAnalyzerArgs]): DATA_MODEL = CmdlineDataModel - def _compare_cmdline(self, cmdline: str, required_cmdline: list, banned_cmdline: list) -> bool: + def _compare_cmdline( + self, cmdline: str, required_cmdline: str | list[Any], banned_cmdline: str | list[Any] + ) -> bool: """Compare the kernel cmdline against required and banned cmdline arguments. Args: cmdline (str): Kernel command line arguments as a string. - required_cmdline (list): required kernel cmdline arguments that must be present. - banned_cmdline (list): banned kernel cmdline arguments that must not be present. + required_cmdline (str) | (list): required kernel cmdline arguments that must be present. + banned_cmdline (str) | (list): banned kernel cmdline arguments that must not be present. Returns: bool: True if the cmdline matches the required arguments and does not contain banned arguments, diff --git a/nodescraper/resultcollators/tablesummary.py b/nodescraper/resultcollators/tablesummary.py index 4d1fdbe..32584e8 100644 --- a/nodescraper/resultcollators/tablesummary.py +++ b/nodescraper/resultcollators/tablesummary.py @@ -40,7 +40,7 @@ def collate_results( connection_results (list[TaskResult]): list of connection results to collate """ - def gen_str_table(headers: list[str], rows: list[str]): + def gen_str_table(headers: list[str], rows: list[list[str | None]]): column_widths = [len(header) for header in headers] for row in rows: for i, cell in enumerate(row): @@ -67,8 +67,10 @@ def gen_row(row): if plugin_results: rows = [] - for result in plugin_results: - rows.append([result.source, result.status.name, result.message]) + for plugin_result in plugin_results: + rows.append( + [plugin_result.source, plugin_result.status.name, plugin_result.message] + ) table = gen_str_table(["Plugin", "Status", "Message"], rows) tables += f"\n\n{table}" diff --git a/pyproject.toml b/pyproject.toml index 7211335..92ef936 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,8 @@ dev = [ "ruff", "pre-commit", "pytest", - "pytest-cov" + "pytest-cov", + "mypy" ] [project.urls] From e4854a009b4cbaded9b2de66a7a0db7af27db6d0 Mon Sep 17 00:00:00 2001 From: Sid Srinivasan Date: Mon, 16 Jun 2025 12:35:34 -0500 Subject: [PATCH 2/6] fixed mypy method assign error in dataanalyzer --- nodescraper/interfaces/dataanalyzertask.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nodescraper/interfaces/dataanalyzertask.py b/nodescraper/interfaces/dataanalyzertask.py index 23e419a..223f156 100644 --- a/nodescraper/interfaces/dataanalyzertask.py +++ b/nodescraper/interfaces/dataanalyzertask.py @@ -119,7 +119,7 @@ def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None: raise TypeError(f"No data model set for {cls.__name__}") if hasattr(cls, "analyze_data"): - cls.analyze_data = analyze_decorator(cls.analyze_data) + setattr(cls, "analyze_data", analyze_decorator(cls.analyze_data)) @abc.abstractmethod def analyze_data( From 28837ee13afc024c99cc5ab9c4521bee9366880e Mon Sep 17 00:00:00 2001 From: Sid Srinivasan Date: Thu, 19 Jun 2025 15:46:59 -0500 Subject: [PATCH 3/6] pre-commit black formatting --- nodescraper/models/event.py | 7 ++++--- nodescraper/resultcollators/tablesummary.py | 10 ++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/nodescraper/models/event.py b/nodescraper/models/event.py index 3db02c3..3e0da16 100644 --- a/nodescraper/models/event.py +++ b/nodescraper/models/event.py @@ -51,7 +51,7 @@ class Event(BaseModel): default_factory=lambda: datetime.datetime.now(datetime.timezone.utc) ) reporter: str = "ERROR_SCRAPER" - category: str | Enum + category: str description: str data: dict = Field(default_factory=dict) priority: EventPriority @@ -73,7 +73,8 @@ def validate_timestamp(cls, timestamp: datetime.datetime) -> datetime.datetime: if timestamp.tzinfo is None or timestamp.tzinfo.utcoffset(timestamp) is None: raise ValueError("datetime must be timezone aware") - if timestamp.utcoffset() is not None and timestamp.utcoffset().total_seconds() != 0: + utc_offset = timestamp.utcoffset() + if utc_offset is not None and utc_offset.total_seconds() != 0: timestamp = timestamp.astimezone(datetime.timezone.utc) return timestamp @@ -90,7 +91,7 @@ def validate_category(cls, category: str | Enum) -> str: if isinstance(category, Enum): category = category.value - category = category.strip().upper() + category = str(category).strip().upper() category = re.sub(r"[\s-]", "_", category) return category diff --git a/nodescraper/resultcollators/tablesummary.py b/nodescraper/resultcollators/tablesummary.py index 32584e8..04708d3 100644 --- a/nodescraper/resultcollators/tablesummary.py +++ b/nodescraper/resultcollators/tablesummary.py @@ -59,8 +59,14 @@ def gen_row(row): tables = "" if connection_results: rows = [] - for result in connection_results: - rows.append([result.task, result.status.name, result.message]) + for connection_result in connection_results: + rows.append( + [ + connection_result.task, + connection_result.status.name, + connection_result.message, + ] + ) table = gen_str_table(["Connection", "Status", "Message"], rows) tables += f"\n\n{table}" From 4f9350011a3fdebbb595ca5cfdbc3286051fdbe7 Mon Sep 17 00:00:00 2001 From: Sid Srinivasan Date: Fri, 20 Jun 2025 12:51:28 -0500 Subject: [PATCH 4/6] pre-commit black formatting --- nodescraper/plugins/inband/cmdline/analyzer_args.py | 4 ++-- nodescraper/plugins/inband/cmdline/cmdline_analyzer.py | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/nodescraper/plugins/inband/cmdline/analyzer_args.py b/nodescraper/plugins/inband/cmdline/analyzer_args.py index be47f4f..fc2b9ab 100644 --- a/nodescraper/plugins/inband/cmdline/analyzer_args.py +++ b/nodescraper/plugins/inband/cmdline/analyzer_args.py @@ -27,8 +27,8 @@ class CmdlineAnalyzerArgs(BaseModel): - required_cmdline: str | list = Field(default_factory=list) - banned_cmdline: str | list = Field(default_factory=list) + required_cmdline: list = Field(default_factory=list) + banned_cmdline: list = Field(default_factory=list) model_config = {"extra": "forbid"} diff --git a/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py b/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py index 9902b18..69a1c6b 100644 --- a/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py +++ b/nodescraper/plugins/inband/cmdline/cmdline_analyzer.py @@ -23,7 +23,7 @@ # SOFTWARE. # ############################################################################### -from typing import Any, Optional +from typing import Optional from nodescraper.enums import EventCategory, EventPriority, ExecutionStatus from nodescraper.interfaces import DataAnalyzer @@ -38,15 +38,13 @@ class CmdlineAnalyzer(DataAnalyzer[CmdlineDataModel, CmdlineAnalyzerArgs]): DATA_MODEL = CmdlineDataModel - def _compare_cmdline( - self, cmdline: str, required_cmdline: str | list[Any], banned_cmdline: str | list[Any] - ) -> bool: + def _compare_cmdline(self, cmdline: str, required_cmdline: list, banned_cmdline: list) -> bool: """Compare the kernel cmdline against required and banned cmdline arguments. Args: cmdline (str): Kernel command line arguments as a string. - required_cmdline (str) | (list): required kernel cmdline arguments that must be present. - banned_cmdline (str) | (list): banned kernel cmdline arguments that must not be present. + required_cmdline (list): required kernel cmdline arguments that must be present. + banned_cmdline (list): banned kernel cmdline arguments that must not be present. Returns: bool: True if the cmdline matches the required arguments and does not contain banned arguments, From 50255ed18879911a95b3eaa3bd349458ede8c1be Mon Sep 17 00:00:00 2001 From: Sid Srinivasan Date: Fri, 20 Jun 2025 12:56:04 -0500 Subject: [PATCH 5/6] rebase --- .github/workflows/code_quality_checks.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/code_quality_checks.yml b/.github/workflows/code_quality_checks.yml index 9ca1775..2721143 100644 --- a/.github/workflows/code_quality_checks.yml +++ b/.github/workflows/code_quality_checks.yml @@ -6,9 +6,6 @@ permissions: on: [pull_request] -permissions: - contents: read - jobs: pre-commit: runs-on: [ self-hosted ] From 7d1850d66721b323f2f630a26be33e74f8476aa2 Mon Sep 17 00:00:00 2001 From: Sid Srinivasan Date: Fri, 20 Jun 2025 15:12:31 -0500 Subject: [PATCH 6/6] added ruff ignore --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 92ef936..752b144 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,4 +59,4 @@ profile = "black" [tool.ruff.lint] select = ["F", "B", "T20", "N", "W", "I", "E"] -ignore = ["E501", "N806"] +ignore = ["E501", "N806", "B010"]