Enable Ruff flake8-use-pathlib (PTH) (#13795)

Port existing code to pathlib
This commit is contained in:
Avasam
2025-05-05 12:59:43 -04:00
committed by GitHub
parent 0eb44e574c
commit 4265ee7c72
11 changed files with 101 additions and 113 deletions
+4 -3
View File
@@ -4,6 +4,7 @@ import subprocess
import sys
from collections.abc import Iterable
from http.client import HTTPResponse
from pathlib import Path
from typing import TYPE_CHECKING
from urllib.request import urlopen
from zipfile import ZipFile
@@ -18,11 +19,11 @@ if TYPE_CHECKING:
MYPY_PROTOBUF_VERSION = mypy_protobuf__version__
def download_file(url: str, destination: StrPath) -> None:
def download_file(url: str, destination: Path) -> None:
print(f"Downloading '{url}' to '{destination}'")
resp: HTTPResponse
with urlopen(url) as resp, open(destination, "wb") as file:
file.write(resp.read())
with urlopen(url) as resp:
destination.write_bytes(resp.read())
def extract_archive(archive_path: StrPath, destination: StrPath) -> None:
+2 -2
View File
@@ -33,7 +33,7 @@ PROTO_FILE_PATTERN = re.compile(r'"//:(.*)_proto"')
def extract_python_version(file_path: Path) -> str:
"""Extract the Python version from https://github.com/protocolbuffers/protobuf/blob/main/version.json ."""
with open(file_path) as file:
with file_path.open() as file:
data: dict[str, Any] = json.load(file)
# The root key will be the protobuf source code version
version = next(iter(data.values()))["languages"]["python"]
@@ -47,7 +47,7 @@ def extract_proto_file_paths(temp_dir: Path) -> list[str]:
as described in py_proto_library calls in
https://github.com/protocolbuffers/protobuf/blob/main/python/dist/BUILD.bazel .
"""
with open(temp_dir / EXTRACTED_PACKAGE_DIR / "python" / "dist" / "BUILD.bazel") as file:
with (temp_dir / EXTRACTED_PACKAGE_DIR / "python" / "dist" / "BUILD.bazel").open() as file:
matched_lines = filter(None, (re.search(PROTO_FILE_PATTERN, line) for line in file))
proto_files = [
EXTRACTED_PACKAGE_DIR + "/src/google/protobuf/" + match.group(1).replace("compiler_", "compiler/") + ".proto"
+3 -6
View File
@@ -6,7 +6,6 @@ Generally, new minor versions are a good time to update the stubs.
from __future__ import annotations
import os
import re
import shutil
import subprocess
@@ -72,21 +71,19 @@ def post_creation() -> None:
for path in STUBS_FOLDER.rglob("*_pb2.pyi"):
print(f"Fixing imports in '{path}'")
with open(path, encoding="utf-8") as file:
filedata = file.read()
filedata = path.read_text(encoding="utf-8")
# Replace the target string
filedata = re.sub(TSL_IMPORT_PATTERN, "\\1tensorflow.tsl.", filedata)
filedata = re.sub(XLA_IMPORT_PATTERN, "\\1tensorflow.compiler.xla.", filedata)
# Write the file out again
with open(path, "w", encoding="utf-8") as file:
file.write(filedata)
path.write_text(filedata, encoding="utf-8")
print()
for to_remove in PROTOS_TO_REMOVE:
file_path = STUBS_FOLDER / "tensorflow" / to_remove
os.remove(file_path)
file_path.unlink()
print(f"Removed '{file_path}'")