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

Port existing code to pathlib
This commit is contained in:
Avasam
2025-05-05 18:59:43 +02:00
committed by GitHub
parent 0eb44e574c
commit 4265ee7c72
11 changed files with 101 additions and 113 deletions
+7 -8
View File
@@ -150,6 +150,8 @@ def match(path: Path, args: TestConfig) -> bool:
def add_files(files: list[Path], module: Path, args: TestConfig) -> None:
"""Add all files in package or module represented by 'name' located in 'root'."""
if module.name.startswith("."):
return
if module.is_file() and module.suffix == ".pyi":
if match(module, args):
files.append(module)
@@ -244,10 +246,8 @@ def add_third_party_files(distribution: str, files: list[Path], args: TestConfig
seen_dists.add(distribution)
seen_dists.update(r.name for r in typeshed_reqs)
root = distribution_path(distribution)
for name in os.listdir(root):
if name.startswith("."):
continue
add_files(files, (root / name), args)
for path in root.iterdir():
add_files(files, path, args)
class TestResult(NamedTuple):
@@ -295,7 +295,7 @@ def test_third_party_distribution(
def test_stdlib(args: TestConfig) -> TestResult:
files: list[Path] = []
for file in STDLIB_PATH.iterdir():
if file.name in ("VERSIONS", TESTS_DIR) or file.name.startswith("."):
if file.name in ("VERSIONS", TESTS_DIR):
continue
add_files(files, file, args)
@@ -525,15 +525,14 @@ def test_third_party_stubs(args: TestConfig, tempdir: Path) -> TestSummary:
def test_typeshed(args: TestConfig, tempdir: Path) -> TestSummary:
print(f"*** Testing Python {args.version} on {args.platform}")
stdlib_dir, stubs_dir = Path("stdlib"), Path("stubs")
summary = TestSummary()
if stdlib_dir in args.filter or any(stdlib_dir in path.parents for path in args.filter):
if STDLIB_PATH in args.filter or any(STDLIB_PATH in path.parents for path in args.filter):
mypy_result, files_checked = test_stdlib(args)
summary.register_result(mypy_result, files_checked)
print()
if stubs_dir in args.filter or any(stubs_dir in path.parents for path in args.filter):
if STUBS_PATH in args.filter or any(STUBS_PATH in path.parents for path in args.filter):
tp_results = test_third_party_stubs(args, tempdir)
summary.merge(tp_results)
print()