Skip to content

Commit aed613f

Browse files
authored
[3.13] gh-133403: Type Tools/build/update_file.py and check it with mypy (GH-133404) (#133637)
(cherry picked from commit 50b52cb)
1 parent a063d6c commit aed613f

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

Tools/build/mypy.ini

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

59
# Make sure Python can still be built
@@ -8,6 +12,8 @@ python_version = 3.10
812

913
# ...And be strict:
1014
strict = True
15+
strict_bytes = True
16+
local_partial_types = True
1117
extra_checks = True
1218
enable_error_code = ignore-without-code,redundant-expr,truthy-bool,possibly-undefined
1319
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)