diff --git a/troubadix/argparser.py b/troubadix/argparser.py index d4d08e6c..1bbd292d 100644 --- a/troubadix/argparser.py +++ b/troubadix/argparser.py @@ -19,9 +19,9 @@ import sys from argparse import ArgumentParser, Namespace +from collections.abc import Sequence from multiprocessing import cpu_count from pathlib import Path -from typing import Iterable from pontos.terminal import Terminal @@ -56,7 +56,7 @@ def check_cpu_count(number: str) -> int: def parse_args( terminal: Terminal, - args: Iterable[str] = None, + args: Sequence[str], ) -> Namespace: """Parsing args for troubadix diff --git a/troubadix/plugin.py b/troubadix/plugin.py index f04d7350..ed624a57 100644 --- a/troubadix/plugin.py +++ b/troubadix/plugin.py @@ -16,9 +16,10 @@ # along with this program. If not, see . from abc import ABC, abstractmethod +from collections.abc import Iterable, Iterator from dataclasses import dataclass from pathlib import Path -from typing import Iterable, Iterator, Optional +from typing import Optional from troubadix.helper import CURRENT_ENCODING @@ -50,13 +51,13 @@ def __init__( self, *, root: Path, - nasl_file: Path = None, + nasl_file: Path, ) -> None: self.root = root self.nasl_file = nasl_file - self._file_content = None - self._lines = None + self._file_content: str | None = None + self._lines: list[str] | None = None @property def file_content(self) -> str: @@ -67,7 +68,7 @@ def file_content(self) -> str: return self._file_content @property - def lines(self) -> Iterable[str]: + def lines(self) -> list[str]: if not self._lines: self._lines = self.file_content.splitlines() return self._lines @@ -82,15 +83,14 @@ def __init__(self, *, root: Path, nasl_files: Iterable[Path]) -> None: class Plugin(ABC): """A linter plugin""" - name: str = None - description: str = None + name: str @abstractmethod def run(self) -> Iterator[LinterResult]: pass def fix(self) -> Iterator[LinterResult]: - return [] + return iter([]) class FilesPlugin(Plugin): diff --git a/troubadix/reporter.py b/troubadix/reporter.py index 2c991d4b..ed749d08 100644 --- a/troubadix/reporter.py +++ b/troubadix/reporter.py @@ -15,8 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from collections.abc import Iterable from pathlib import Path -from typing import Iterable, List from pontos.terminal import Terminal @@ -33,8 +33,8 @@ def __init__( root: Path, *, fix: bool = False, - log_file: Path = None, - log_file_statistic: Path = None, + log_file: Path | None = None, + log_file_statistic: Path | None = None, statistic: bool = True, verbose: int = 0, ignore_warnings: bool = False, @@ -78,7 +78,7 @@ def _report_ok(self, message: str) -> None: self._log_append(f"\t\t{message}".replace("\n", "\n\t\t")) def _process_plugin_results( - self, plugin_name: str, plugin_results: List[LinterResult] + self, plugin_name: str, plugin_results: Iterable[LinterResult] ): """Process the results of a plugin: Print/Log results if verbosity/logging fits and count the results""" diff --git a/troubadix/results.py b/troubadix/results.py index f23eb581..359a59e9 100644 --- a/troubadix/results.py +++ b/troubadix/results.py @@ -16,17 +16,15 @@ # along with this program. If not, see . from collections import defaultdict +from collections.abc import Iterator from pathlib import Path -from typing import Dict, Iterable, Iterator from troubadix.plugin import LinterResult, LinterWarning class Results: def __init__(self, ignore_warnings: bool = False) -> None: - self.plugin_results: Dict[str, Iterable[LinterResult]] = defaultdict( - list - ) + self.plugin_results: dict[str, list[LinterResult]] = defaultdict(list) self.has_plugin_results = False self._ignore_warnings = ignore_warnings @@ -34,16 +32,16 @@ def add_plugin_results( self, plugin_name: str, results: Iterator[LinterResult] ) -> "Results": if self._ignore_warnings: - results = [ + results_list = [ result for result in results if not isinstance(result, LinterWarning) ] else: - results = list(results) + results_list = list(results) self.has_plugin_results = self.has_plugin_results or bool(results) - self.plugin_results[plugin_name] += results + self.plugin_results[plugin_name] += results_list return self def __bool__(self): diff --git a/troubadix/runner.py b/troubadix/runner.py index e95ada1c..ea8b9bda 100644 --- a/troubadix/runner.py +++ b/troubadix/runner.py @@ -17,9 +17,9 @@ import datetime import signal +from collections.abc import Iterable from multiprocessing import Pool from pathlib import Path -from typing import Iterable from troubadix.helper.patterns import ( init_script_tag_patterns, @@ -49,11 +49,11 @@ def __init__( reporter: Reporter, *, root: Path, - excluded_plugins: Iterable[str] = None, - included_plugins: Iterable[str] = None, + excluded_plugins: Iterable[str] | None = None, + included_plugins: Iterable[str] | None = None, fix: bool = False, ignore_warnings: bool = False, - ) -> bool: + ) -> None: # plugins initialization self.plugins = StandardPlugins(excluded_plugins, included_plugins) diff --git a/troubadix/troubadix.py b/troubadix/troubadix.py index 1c683601..7348ef24 100644 --- a/troubadix/troubadix.py +++ b/troubadix/troubadix.py @@ -18,8 +18,8 @@ """ Main module for troubadix """ import sys +from collections.abc import Iterable from pathlib import Path -from typing import Iterable, List, Tuple from pontos.terminal import Terminal from pontos.terminal.terminal import ConsoleTerminal @@ -35,7 +35,7 @@ def generate_file_list( dirs: Iterable[Path], exclude_patterns: Iterable[str], include_patterns: Iterable[str], -) -> List[Path]: +) -> list[Path]: """Generates a files list under respect of several given arguments Arguments: @@ -48,7 +48,7 @@ def generate_file_list( Returns List of Path objects""" - files: List[Path] = [] + files: list[Path] = [] for directory in dirs: for pattern in include_patterns: files.extend(directory.glob(pattern)) @@ -64,10 +64,10 @@ def generate_file_list( def generate_patterns( terminal: ConsoleTerminal, - include_patterns: List[str], - exclude_patterns: List[str], + include_patterns: list[str], + exclude_patterns: list[str], non_recursive: bool, -) -> Tuple[List[str], List[str]]: +) -> tuple[list[str], list[str]]: """Generates the include and exclude patterns Arguments: