Skip to content

Commit b986027

Browse files
authored
chore: stricter mypy (#2053)
* chore: improve mypy Signed-off-by: Henry Schreiner <[email protected]> * chore(types): type functions in tests Signed-off-by: Henry Schreiner <[email protected]> * chore(types): No partial types in tests Signed-off-by: Henry Schreiner <[email protected]> --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent b4d1e17 commit b986027

27 files changed

+131
-111
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ repos:
3232
- nox
3333
- packaging
3434
- pygithub
35+
- pytest
3536
- rich
3637
- tomli
3738
- tomli_w
@@ -40,6 +41,7 @@ repos:
4041
- types-jinja2
4142
- types-pyyaml
4243
- types-requests
44+
- types-setuptools
4345
- uv
4446
- validate-pyproject
4547
- id: mypy

bin/make_dependency_update_pr.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33
from __future__ import annotations
44

55
import os
6+
import subprocess
67
import sys
78
import textwrap
89
import time
910
from pathlib import Path
10-
from subprocess import run
1111

1212
import click
1313

1414

15-
def shell(cmd, *, check: bool, **kwargs):
16-
return run([cmd], shell=True, check=check, **kwargs)
15+
def shell(cmd: str, *, check: bool, **kwargs: object) -> subprocess.CompletedProcess[str]:
16+
return subprocess.run([cmd], shell=True, check=check, **kwargs) # type: ignore[call-overload, no-any-return]
1717

1818

19-
def git_repo_has_changes():
20-
unstaged_changes = shell("git diff-index --quiet HEAD --", check=False).returncode != 0
21-
staged_changes = shell("git diff-index --quiet --cached HEAD --", check=False).returncode != 0
19+
def git_repo_has_changes() -> bool:
20+
unstaged_changes: bool = shell("git diff-index --quiet HEAD --", check=False).returncode != 0
21+
staged_changes: bool = (
22+
shell("git diff-index --quiet --cached HEAD --", check=False).returncode != 0
23+
)
2224
return unstaged_changes or staged_changes
2325

2426

2527
@click.command()
26-
def main():
28+
def main() -> None:
2729
project_root = Path(__file__).parent / ".."
2830
os.chdir(project_root)
2931

@@ -57,7 +59,7 @@ def main():
5759
PR generated by `{os.path.basename(__file__)}`.
5860
"""
5961
)
60-
run(
62+
subprocess.run(
6163
[
6264
"gh",
6365
"pr",

bin/projects.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ def get_projects(
172172
return sorted((Project(item, github) for item in config), reverse=online)
173173

174174

175-
def render_projects(projects: Sequence[Project], *, dest_path: Path, include_info: bool = True):
175+
def render_projects(
176+
projects: Sequence[Project], *, dest_path: Path, include_info: bool = True
177+
) -> str:
176178
io = StringIO()
177179
print = functools.partial(builtins.print, file=io)
178180

@@ -203,7 +205,7 @@ def insert_projects_table(
203205
projects: Sequence[Project],
204206
input_filename: str,
205207
include_info: bool = True,
206-
):
208+
) -> None:
207209
text = file.read_text()
208210
projects_table = render_projects(projects, include_info=include_info, dest_path=file)
209211

bin/run_example_ci_configs.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44

55
import os
66
import shutil
7+
import subprocess
78
import sys
89
import textwrap
910
import time
1011
import typing
1112
from glob import glob
1213
from pathlib import Path
13-
from subprocess import run
1414
from urllib.parse import quote
1515

1616
import click
1717

1818

19-
def shell(cmd, *, check: bool, **kwargs):
20-
return run([cmd], shell=True, check=check, **kwargs)
19+
def shell(cmd: str, *, check: bool, **kwargs: object) -> subprocess.CompletedProcess[str]:
20+
return subprocess.run([cmd], shell=True, check=check, **kwargs) # type: ignore[call-overload, no-any-return]
2121

2222

23-
def git_repo_has_changes():
23+
def git_repo_has_changes() -> bool:
2424
unstaged_changes = shell("git diff-index --quiet HEAD --", check=False).returncode != 0
2525
staged_changes = shell("git diff-index --quiet --cached HEAD --", check=False).returncode != 0
2626
return unstaged_changes or staged_changes
2727

2828

29-
def generate_basic_project(path):
29+
def generate_basic_project(path: Path) -> None:
3030
sys.path.insert(0, "")
3131
from test.test_projects.c import new_c_project
3232

@@ -79,7 +79,7 @@ class CIService(typing.NamedTuple):
7979
]
8080

8181

82-
def ci_service_for_config_file(config_file):
82+
def ci_service_for_config_file(config_file: str) -> CIService:
8383
filename = Path(config_file).name
8484

8585
try:
@@ -134,7 +134,7 @@ def run_example_ci_configs(config_files=None):
134134
dst_config_file.parent.mkdir(parents=True, exist_ok=True)
135135
shutil.copyfile(src_config_file, dst_config_file)
136136

137-
run(["git", "add", example_project], check=True)
137+
subprocess.run(["git", "add", example_project], check=True)
138138
message = textwrap.dedent(
139139
f"""\
140140
Test example minimal configs
@@ -144,7 +144,7 @@ def run_example_ci_configs(config_files=None):
144144
Time: {timestamp}
145145
"""
146146
)
147-
run(["git", "commit", "--no-verify", "--message", message], check=True)
147+
subprocess.run(["git", "commit", "--no-verify", "--message", message], check=True)
148148
shell(f"git subtree --prefix={example_project} push origin {branch_name}", check=True)
149149

150150
print("---")

bin/update_how_it_works_image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919

2020

21-
def main():
21+
def main() -> None:
2222
subprocess.run(["mkdocs", "build"], check=True)
2323

2424
hti = Html2Image(custom_flags=["--force-device-scale-factor=2"])

bin/update_readme_changelog.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
)
2121

2222

23-
def main():
23+
def main() -> None:
2424
changelog_text = CHANGELOG_FILE.read_text()
2525
readme_text = README_FILE.read_text()
2626

bin/update_virtualenv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class VersionTuple:
3636
version_string: str
3737

3838

39-
def git_ls_remote_versions(url) -> list[VersionTuple]:
39+
def git_ls_remote_versions(url: str) -> list[VersionTuple]:
4040
versions: list[VersionTuple] = []
4141
tags = subprocess.run(
4242
["git", "ls-remote", "--tags", url], check=True, text=True, capture_output=True

docs/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
def define_env(env: Any) -> None:
1010
"Hook function for mkdocs-macros"
1111

12-
@env.macro
12+
@env.macro # type: ignore[misc]
1313
def subprocess_run(*args: str) -> str:
1414
"Run a subprocess and return the stdout"
1515
env = os.environ.copy()

pyproject.toml

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,20 @@ files = [
113113
]
114114
warn_unused_configs = true
115115

116-
warn_redundant_casts = true
117-
no_implicit_reexport = true
118-
strict_equality = true
119-
warn_unused_ignores = true
120-
check_untyped_defs = true
121-
122-
disallow_subclassing_any = true
123-
disallow_any_generics = true
124-
warn_return_any = true
125-
no_implicit_optional = true
116+
strict = true
117+
disallow_untyped_defs = false
126118

127119
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
128-
warn_unreachable = true
120+
warn_unreachable = false
129121

130122
[[tool.mypy.overrides]]
131123
module = "cibuildwheel.*"
132124
disallow_untyped_defs = true
133-
disallow_untyped_calls = true
134-
disallow_incomplete_defs = true
135-
disallow_untyped_decorators = true
136125

137126
[[tool.mypy.overrides]]
138127
module = [
139-
"setuptools",
140128
"setuptools._distutils", # needed even if only directly import setuptools._distutils.util
141129
"setuptools._distutils.util",
142-
"pytest", # ignored in pre-commit to speed up check
143130
"bashlex",
144131
"bashlex.*",
145132
"importlib_resources",

test/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from .utils import EMULATED_ARCHS, platform
1212

1313

14-
def pytest_addoption(parser) -> None:
14+
def pytest_addoption(parser: pytest.Parser) -> None:
1515
parser.addoption(
1616
"--run-emulation",
1717
action="store",

0 commit comments

Comments
 (0)