From 7e972b8fa1d0002dd848363b7bf82ca28988c1f8 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 28 Feb 2024 23:24:31 -0500 Subject: [PATCH] Code improvements from new Ruff checks --- scripts/runtests.py | 2 +- scripts/stubsabot.py | 4 ++-- test_cases/stdlib/typing/check_all.py | 4 +--- tests/mypy_test.py | 2 +- tests/pytype_test.py | 2 +- tests/regr_test.py | 2 +- tests/stubtest_third_party.py | 6 +++--- tests/typecheck_typeshed.py | 2 +- tests/utils.py | 2 +- 9 files changed, 12 insertions(+), 14 deletions(-) diff --git a/scripts/runtests.py b/scripts/runtests.py index bec0e47ccaa0..c373ba903ada 100755 --- a/scripts/runtests.py +++ b/scripts/runtests.py @@ -101,7 +101,7 @@ def main() -> None: strict_params = _get_strict_params(path) print(f"\nRunning Pyright ({'stricter' if strict_params else 'base' } configs) for Python {python_version}...") pyright_result = subprocess.run( - [sys.executable, "tests/pyright_test.py", path, "--pythonversion", python_version] + strict_params, + [sys.executable, "tests/pyright_test.py", path, "--pythonversion", python_version, *strict_params], stderr=subprocess.PIPE, text=True, ) diff --git a/scripts/stubsabot.py b/scripts/stubsabot.py index b2a86e31d0f6..64c6c475c38c 100644 --- a/scripts/stubsabot.py +++ b/scripts/stubsabot.py @@ -48,7 +48,7 @@ def from_cmd_arg(cls, cmd_arg: str) -> ActionLevel: try: return cls[cmd_arg] except KeyError: - raise argparse.ArgumentTypeError(f'Argument must be one of "{list(cls.__members__)}"') + raise argparse.ArgumentTypeError(f'Argument must be one of "{list(cls.__members__)}"') from None nothing = 0, "make no changes" local = 1, "make changes that affect local repo" @@ -516,7 +516,7 @@ async def determine_action(stub_path: Path, session: aiohttp.ClientSession) -> U ) -@functools.lru_cache() +@functools.lru_cache def get_origin_owner() -> str: output = subprocess.check_output(["git", "remote", "get-url", "origin"], text=True).strip() match = re.match(r"(git@github.com:|https://github.com/)(?P[^/]+)/(?P[^/\s]+)", output) diff --git a/test_cases/stdlib/typing/check_all.py b/test_cases/stdlib/typing/check_all.py index b75e8a8d02a6..3c3f56813c69 100644 --- a/test_cases/stdlib/typing/check_all.py +++ b/test_cases/stdlib/typing/check_all.py @@ -1,10 +1,8 @@ -from __future__ import annotations - # pyright: reportWildcardImportFromLibrary=false - """ This tests that star imports work when using "all += " syntax. """ +from __future__ import annotations import sys from typing import * # noqa: F403 diff --git a/tests/mypy_test.py b/tests/mypy_test.py index 552dbd15cc77..f9d0ea3cc839 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -279,7 +279,7 @@ def run_mypy( flags.append("--no-site-packages") mypy_args = [*flags, *map(str, files)] - mypy_command = [venv_info.python_exe, "-m", "mypy"] + mypy_args + mypy_command = [venv_info.python_exe, "-m", "mypy", *mypy_args] if args.verbose: print(colored(f"running {' '.join(mypy_command)}", "blue")) result = subprocess.run(mypy_command, capture_output=True, text=True, env=env_vars) diff --git a/tests/pytype_test.py b/tests/pytype_test.py index 43c8fca03d82..ee314f3f0b28 100755 --- a/tests/pytype_test.py +++ b/tests/pytype_test.py @@ -210,7 +210,7 @@ def run_all_tests(*, files_to_test: Sequence[str], print_stderr: bool, dry_run: missing_modules = get_missing_modules(files_to_test) print("Testing files with pytype...") for i, f in enumerate(files_to_test): - python_version = "{0.major}.{0.minor}".format(sys.version_info) + python_version = f"{sys.version_info.major}.{sys.version_info.minor}" if dry_run: stderr = None else: diff --git a/tests/regr_test.py b/tests/regr_test.py index efa46e2b8a93..b0f689d95b95 100755 --- a/tests/regr_test.py +++ b/tests/regr_test.py @@ -211,7 +211,7 @@ def run_testcases( continue flags.append(str(path)) - mypy_command = [python_exe, "-m", "mypy"] + flags + mypy_command = [python_exe, "-m", "mypy", *flags] if verbosity is Verbosity.VERBOSE: description = f"{package.name}/{version}/{platform}" msg = f"{description}: {mypy_command=}\n" diff --git a/tests/stubtest_third_party.py b/tests/stubtest_third_party.py index 9b4e301d049e..be17ec616364 100755 --- a/tests/stubtest_third_party.py +++ b/tests/stubtest_third_party.py @@ -53,7 +53,7 @@ def run_stubtest( # If tool.stubtest.stubtest_requirements exists, run "pip install" on it. if stubtest_settings.stubtest_requirements: - pip_cmd = [pip_exe, "install"] + stubtest_settings.stubtest_requirements + pip_cmd = [pip_exe, "install", *stubtest_settings.stubtest_requirements] try: subprocess.run(pip_cmd, check=True, capture_output=True) except subprocess.CalledProcessError as e: @@ -67,7 +67,7 @@ def run_stubtest( # TODO: Maybe find a way to cache these in CI dists_to_install = [dist_req, get_mypy_req()] dists_to_install.extend(requirements.external_pkgs) # Internal requirements are added to MYPYPATH - pip_cmd = [pip_exe, "install"] + dists_to_install + pip_cmd = [pip_exe, "install", *dists_to_install] try: subprocess.run(pip_cmd, check=True, capture_output=True) except subprocess.CalledProcessError as e: @@ -134,7 +134,7 @@ def run_stubtest( print(file=sys.stderr) else: print(f"Re-running stubtest with --generate-allowlist.\nAdd the following to {allowlist_path}:", file=sys.stderr) - ret = subprocess.run(stubtest_cmd + ["--generate-allowlist"], env=stubtest_env, capture_output=True) + ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True) print_command_output(ret) return False diff --git a/tests/typecheck_typeshed.py b/tests/typecheck_typeshed.py index 4094399f06df..32f30fd73d89 100755 --- a/tests/typecheck_typeshed.py +++ b/tests/typecheck_typeshed.py @@ -21,7 +21,7 @@ parser = argparse.ArgumentParser(description="Run mypy on typeshed's own code in the `scripts` and `tests` directories.") parser.add_argument( "dir", - choices=DIRECTORIES_TO_TEST + (EMPTY,), + choices=(*DIRECTORIES_TO_TEST, EMPTY), nargs="*", action="extend", help=f"Test only these top-level typeshed directories (defaults to {DIRECTORIES_TO_TEST!r})", diff --git a/tests/utils.py b/tests/utils.py index 2fd376314057..c1b24256f753 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -119,7 +119,7 @@ def get_all_testcase_directories() -> list[PackageInfo]: potential_testcase_dir = testcase_dir_from_package_name(package_name) if potential_testcase_dir.is_dir(): testcase_directories.append(PackageInfo(package_name, potential_testcase_dir)) - return [PackageInfo("stdlib", Path("test_cases"))] + sorted(testcase_directories) + return [PackageInfo("stdlib", Path("test_cases")), *sorted(testcase_directories)] # ====================================================================