Skip to content

Commit e8c279f

Browse files
miss-islingtonsobolevnzware
authored
[3.14] gh-133403: Type Tools/build/update_file.py and check it with mypy (GH-133404) (#133603)
gh-133403: Type `Tools/build/update_file.py` and check it with `mypy` (GH-133404) (cherry picked from commit 50b52cb) Co-authored-by: sobolevn <mail@sobolevn.me> Co-authored-by: Zachary Ware <zach@python.org>
1 parent f12e576 commit e8c279f

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

Tools/build/mypy.ini

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[mypy]
22
files =
33
Tools/build/compute-changes.py,
4-
Tools/build/generate_sbom.py
4+
Tools/build/generate_sbom.py,
5+
Tools/build/update_file.py
6+
57
pretty = True
68

79
# Make sure Python can still be built
@@ -10,6 +12,8 @@ python_version = 3.10
1012

1113
# ...And be strict:
1214
strict = True
15+
strict_bytes = True
16+
local_partial_types = True
1317
extra_checks = True
1418
enable_error_code = ignore-without-code,redundant-expr,truthy-bool,possibly-undefined
1519
warn_unreachable = True

Tools/build/update_file.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,27 @@
66
actually change the in-tree generated code.
77
"""
88

9+
from __future__ import annotations
10+
911
import contextlib
1012
import os
1113
import os.path
1214
import sys
1315

16+
TYPE_CHECKING = False
17+
if TYPE_CHECKING:
18+
import typing
19+
from collections.abc import Iterator
20+
from io import TextIOWrapper
21+
22+
_Outcome: typing.TypeAlias = typing.Literal['created', 'updated', 'same']
23+
1424

1525
@contextlib.contextmanager
16-
def updating_file_with_tmpfile(filename, tmpfile=None):
26+
def updating_file_with_tmpfile(
27+
filename: str,
28+
tmpfile: str | None = None,
29+
) -> Iterator[tuple[TextIOWrapper, TextIOWrapper]]:
1730
"""A context manager for updating a file via a temp file.
1831
1932
The context manager provides two open files: the source file open
@@ -46,13 +59,18 @@ def updating_file_with_tmpfile(filename, tmpfile=None):
4659
update_file_with_tmpfile(filename, tmpfile)
4760

4861

49-
def update_file_with_tmpfile(filename, tmpfile, *, create=False):
62+
def update_file_with_tmpfile(
63+
filename: str,
64+
tmpfile: str,
65+
*,
66+
create: bool = False,
67+
) -> _Outcome:
5068
try:
5169
targetfile = open(filename, 'rb')
5270
except FileNotFoundError:
5371
if not create:
5472
raise # re-raise
55-
outcome = 'created'
73+
outcome: _Outcome = 'created'
5674
os.replace(tmpfile, filename)
5775
else:
5876
with targetfile:

0 commit comments

Comments
 (0)