From 1e2c32cc36a9f979e839fc41a0c2b06cb1dd04ec Mon Sep 17 00:00:00 2001 From: Tony Meyer Date: Fri, 22 Sep 2023 16:37:40 +1200 Subject: [PATCH] test: add type hints to the test_infra tests (#1008) * Add type hints. * Don't allow a space between the variable name and the colon introducing the type. --- pyproject.toml | 8 +++++--- test/test_infra.py | 15 ++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 00d8c9efd..f523c1ec0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,7 +8,7 @@ show_missing = true # Formatting tools configuration [tool.autopep8] max-line-length = 99 -ignore = ["E203", "W503"] +ignore = ["W503"] recursive = true jobs = -1 aggressive = 3 @@ -22,13 +22,15 @@ max-line-length = 99 max-doc-length = 99 exclude = [".git", "__pycache__", ".tox", "build", "dist", "*.egg_info", "venv"] select = ["E", "W", "F", "C", "N", "R", "D", "H"] -ignore = ["D105", "D107", "E203", "W503"] +ignore = ["D105", "D107", "W503"] # D100, D101, D102, D103, D104: Ignore missing docstrings in tests per-file-ignores = ["test/*:D100,D101,D102,D103,D104"] docstring-convention = "google" [tool.pyright] -include = ["ops/*.py", "ops/_private/*.py"] +include = ["ops/*.py", "ops/_private/*.py", + "test/test_infra.py", +] pythonVersion = "3.8" # check no python > 3.8 features are used pythonPlatform = "All" typeCheckingMode = "strict" diff --git a/test/test_infra.py b/test/test_infra.py index be9e0da6e..47183f45d 100644 --- a/test/test_infra.py +++ b/test/test_infra.py @@ -19,19 +19,20 @@ import subprocess import sys import tempfile +import typing import unittest import ops -def get_python_filepaths(include_tests=True): +def get_python_filepaths(include_tests: bool = True): """Helper to retrieve paths of Python files.""" python_paths = ['setup.py'] roots = ['ops'] if include_tests: roots.append('test') for root in roots: - for dirpath, dirnames, filenames in os.walk(root): + for dirpath, _, filenames in os.walk(root): for filename in filenames: if filename.endswith(".py"): python_paths.append(os.path.join(dirpath, filename)) @@ -42,7 +43,7 @@ class InfrastructureTests(unittest.TestCase): def test_quote_backslashes(self): # ensure we're not using unneeded backslash to escape strings - issues = [] + issues: typing.List[typing.Tuple[str, int, str]] = [] for filepath in get_python_filepaths(): with open(filepath, "rt", encoding="utf8") as fh: for idx, line in enumerate(fh, 1): @@ -54,7 +55,7 @@ def test_quote_backslashes(self): def test_ensure_copyright(self): # all non-empty Python files must have a proper copyright somewhere in the first 5 lines - issues = [] + issues: typing.List[str] = [] regex = re.compile(r"# Copyright \d\d\d\d(-\d\d\d\d)? Canonical Ltd.\n") for filepath in get_python_filepaths(): if os.stat(filepath).st_size == 0: @@ -69,7 +70,7 @@ def test_ensure_copyright(self): if issues: self.fail("Please add copyright headers to the following files:\n" + "\n".join(issues)) - def _run_setup(self, *args): + def _run_setup(self, *args: str) -> str: proc = subprocess.run( (sys.executable, 'setup.py') + args, stdout=subprocess.PIPE, @@ -101,7 +102,7 @@ def test_install_requires(self): # For some reason "setup.py --requires" doesn't work, so do this the hard way with open('setup.py', encoding='utf-8') as f: - lines = [] + lines: typing.List[str] = [] for line in f: if 'install_requires=[' in line: break @@ -131,7 +132,7 @@ def test_imports(self): with self.subTest(name=name): self.check(name) - def check(self, name): + def check(self, name: str): """Helper function to run the test.""" fd, testfile = tempfile.mkstemp() self.addCleanup(os.unlink, testfile)