Skip to content

Commit 7c10f27

Browse files
committed
gh-133403: Type Tools/build/update_file.py and check it with mypy
1 parent 881144f commit 7c10f27

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-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: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,26 @@
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
15+
import typing
16+
17+
if typing.TYPE_CHECKING:
18+
from collections.abc import Iterator
19+
from io import TextIOWrapper
20+
21+
_Outcome: typing.TypeAlias = typing.Literal['created', 'updated', 'same']
1322

1423

1524
@contextlib.contextmanager
16-
def updating_file_with_tmpfile(filename, tmpfile=None):
25+
def updating_file_with_tmpfile(
26+
filename: str,
27+
tmpfile: str | None = None,
28+
) -> Iterator[tuple[TextIOWrapper, TextIOWrapper]]:
1729
"""A context manager for updating a file via a temp file.
1830
1931
The context manager provides two open files: the source file open
@@ -46,13 +58,18 @@ def updating_file_with_tmpfile(filename, tmpfile=None):
4658
update_file_with_tmpfile(filename, tmpfile)
4759

4860

49-
def update_file_with_tmpfile(filename, tmpfile, *, create=False):
61+
def update_file_with_tmpfile(
62+
filename: str,
63+
tmpfile: str,
64+
*,
65+
create: bool = False,
66+
) -> _Outcome:
5067
try:
5168
targetfile = open(filename, 'rb')
5269
except FileNotFoundError:
5370
if not create:
5471
raise # re-raise
55-
outcome = 'created'
72+
outcome: _Outcome = 'created'
5673
os.replace(tmpfile, filename)
5774
else:
5875
with targetfile:

0 commit comments

Comments
 (0)