Skip to content

Commit

Permalink
test: add type hints to the test_infra tests (#1008)
Browse files Browse the repository at this point in the history
* Add type hints.
* Don't allow a space between the variable name and the colon introducing the type.
  • Loading branch information
tonyandrewmeyer authored Sep 22, 2023
1 parent ff6aeb9 commit 1e2c32c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
15 changes: 8 additions & 7 deletions test/test_infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1e2c32c

Please sign in to comment.