From 7c10f2797edb03355d63f900c9dd4f04ef35aa5f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 5 May 2025 00:31:42 +0300 Subject: [PATCH 1/2] gh-133403: Type `Tools/build/update_file.py` and check it with `mypy` --- Tools/build/mypy.ini | 6 +++++- Tools/build/update_file.py | 23 ++++++++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Tools/build/mypy.ini b/Tools/build/mypy.ini index 06224163884a98..db546c6fb3481c 100644 --- a/Tools/build/mypy.ini +++ b/Tools/build/mypy.ini @@ -1,7 +1,9 @@ [mypy] files = Tools/build/compute-changes.py, - Tools/build/generate_sbom.py + Tools/build/generate_sbom.py, + Tools/build/update_file.py + pretty = True # Make sure Python can still be built @@ -10,6 +12,8 @@ python_version = 3.10 # ...And be strict: strict = True +strict_bytes = True +local_partial_types = True extra_checks = True enable_error_code = ignore-without-code,redundant-expr,truthy-bool,possibly-undefined warn_unreachable = True diff --git a/Tools/build/update_file.py b/Tools/build/update_file.py index b4182c1d0cb638..ce10a0678befec 100644 --- a/Tools/build/update_file.py +++ b/Tools/build/update_file.py @@ -6,14 +6,26 @@ actually change the in-tree generated code. """ +from __future__ import annotations + import contextlib import os import os.path import sys +import typing + +if typing.TYPE_CHECKING: + from collections.abc import Iterator + from io import TextIOWrapper + + _Outcome: typing.TypeAlias = typing.Literal['created', 'updated', 'same'] @contextlib.contextmanager -def updating_file_with_tmpfile(filename, tmpfile=None): +def updating_file_with_tmpfile( + filename: str, + tmpfile: str | None = None, +) -> Iterator[tuple[TextIOWrapper, TextIOWrapper]]: """A context manager for updating a file via a temp file. The context manager provides two open files: the source file open @@ -46,13 +58,18 @@ def updating_file_with_tmpfile(filename, tmpfile=None): update_file_with_tmpfile(filename, tmpfile) -def update_file_with_tmpfile(filename, tmpfile, *, create=False): +def update_file_with_tmpfile( + filename: str, + tmpfile: str, + *, + create: bool = False, +) -> _Outcome: try: targetfile = open(filename, 'rb') except FileNotFoundError: if not create: raise # re-raise - outcome = 'created' + outcome: _Outcome = 'created' os.replace(tmpfile, filename) else: with targetfile: From 170c1dde3fa349107ba180e91f44a12353c4fa06 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Mon, 5 May 2025 13:35:16 +0300 Subject: [PATCH 2/2] Address review --- Tools/build/update_file.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tools/build/update_file.py b/Tools/build/update_file.py index ce10a0678befec..b4a5fb6e778ae8 100644 --- a/Tools/build/update_file.py +++ b/Tools/build/update_file.py @@ -12,9 +12,10 @@ import os import os.path import sys -import typing -if typing.TYPE_CHECKING: +TYPE_CHECKING = False +if TYPE_CHECKING: + import typing from collections.abc import Iterator from io import TextIOWrapper pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy