[Git][ghc/ghc][wip/bchinn-argparse-filetype] Replace deprecated argparse.FileType
Brandon Chinn pushed to branch wip/bchinn-argparse-filetype at Glasgow Haskell Compiler / GHC Commits: cc7d226d by Brandon Chinn at 2025-10-21T16:36:26-07:00 Replace deprecated argparse.FileType - - - - - 3 changed files: - docs/users_guide/compare-flags.py - rts/gen_event_types.py - testsuite/driver/runtests.py Changes: ===================================== docs/users_guide/compare-flags.py ===================================== @@ -35,7 +35,7 @@ def expected_undocumented(flag: str) -> bool: return False -def read_documented_flags(doc_flags) -> Set[str]: +def read_documented_flags(doc_flags: Path) -> Set[str]: # Map characters that mark the end of a flag # to whitespace. trans = str.maketrans({ @@ -44,10 +44,10 @@ def read_documented_flags(doc_flags) -> Set[str]: '⟨': ' ', }) return {line.translate(trans).split()[0] - for line in doc_flags.read().split('\n') + for line in doc_flags.read_text().split('\n') if line != ''} -def read_ghc_flags(ghc_path: str) -> Set[str]: +def read_ghc_flags(ghc_path: Path) -> Set[str]: ghc_output = subprocess.check_output([ghc_path, '--show-options']) ghci_output = subprocess.check_output([ghc_path, '--interactive', '--show-options']) @@ -63,16 +63,16 @@ def error(s: str): def main() -> None: import argparse parser = argparse.ArgumentParser() - parser.add_argument('--ghc', type=argparse.FileType('r'), + parser.add_argument('--ghc', type=Path, help='path of GHC executable', required=True) - parser.add_argument('--doc-flags', type=argparse.FileType(mode='r', encoding='UTF-8'), + parser.add_argument('--doc-flags', type=Path, help='path of ghc-flags.txt output from Sphinx', required=True) args = parser.parse_args() doc_flags = read_documented_flags(args.doc_flags) - ghc_flags = read_ghc_flags(args.ghc.name) + ghc_flags = read_ghc_flags(args.ghc) failed = False ===================================== rts/gen_event_types.py ===================================== @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from pathlib import Path from typing import List, Union, Dict from collections import namedtuple @@ -198,17 +199,17 @@ def generate_event_types_defines() -> str: def main() -> None: import argparse parser = argparse.ArgumentParser() - parser.add_argument('--event-types-array', type=argparse.FileType('w'), metavar='FILE') - parser.add_argument('--event-types-defines', type=argparse.FileType('w'), metavar='FILE') + parser.add_argument('--event-types-array', type=Path, metavar='FILE') + parser.add_argument('--event-types-defines', type=Path, metavar='FILE') args = parser.parse_args() check_events() if args.event_types_array: - args.event_types_array.write(generate_event_types_array()) + args.event_types_array.write_text(generate_event_types_array()) if args.event_types_defines: - args.event_types_defines.write(generate_event_types_defines()) + args.event_types_defines.write_text(generate_event_types_defines()) if __name__ == '__main__': main() ===================================== testsuite/driver/runtests.py ===================================== @@ -83,7 +83,7 @@ parser.add_argument("--way", action="append", help="just this way") parser.add_argument("--skipway", action="append", help="skip this way") parser.add_argument("--threads", type=int, help="threads to run simultaneously") parser.add_argument("--verbose", type=int, choices=[0,1,2,3,4,5], help="verbose (Values 0 through 5 accepted)") -parser.add_argument("--junit", type=argparse.FileType('wb'), help="output testsuite summary in JUnit format") +parser.add_argument("--junit", type=Path, help="output testsuite summary in JUnit format") parser.add_argument("--broken-test", action="append", default=[], help="a test name to mark as broken for this run") parser.add_argument("--test-env", default='local', help="Override default chosen test-env.") parser.add_argument("--perf-baseline", type=GitRef, metavar='COMMIT', help="Baseline commit for performance comparsons.") @@ -91,7 +91,7 @@ perf_group.add_argument("--skip-perf-tests", action="store_true", help="skip per perf_group.add_argument("--only-perf-tests", action="store_true", help="Only do performance tests") parser.add_argument("--ignore-perf-failures", choices=['increases','decreases','all'], help="Do not fail due to out-of-tolerance perf tests") -parser.add_argument("--only-report-hadrian-deps", type=argparse.FileType('w'), +parser.add_argument("--only-report-hadrian-deps", type=Path, help="Dry run the testsuite and report all extra hadrian dependencies needed on the given file") args = parser.parse_args() @@ -615,14 +615,14 @@ else: summary(t, f) if args.junit: - junit(t).write(args.junit) - args.junit.close() + with args.junit.open("wb") as f: + junit(t).write(f) if config.only_report_hadrian_deps: print("WARNING - skipping all tests and only reporting required hadrian dependencies:", config.hadrian_deps) - for d in config.hadrian_deps: - print(d,file=config.only_report_hadrian_deps) - config.only_report_hadrian_deps.close() + with config.only_report_hadrian_deps.open("w") as f: + for d in config.hadrian_deps: + print(d, file=f) if len(t.unexpected_failures) > 0 or \ len(t.unexpected_stat_failures) > 0 or \ View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cc7d226d6ed46c62cdc9daae4182c8ad... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/cc7d226d6ed46c62cdc9daae4182c8ad... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Brandon Chinn (@brandonchinn178)