From c8d65766095138b1cb31b6edb370a05cad3e5186 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 28 Dec 2024 14:22:49 -0500 Subject: [PATCH 1/2] Enable Ruff flake8-return --- lib/ts_utils/paths.py | 6 ++---- lib/ts_utils/utils.py | 3 +-- pyproject.toml | 1 + scripts/stubsabot.py | 3 +-- scripts/sync_protobuf/google_protobuf.py | 3 +-- tests/mypy_test.py | 5 ++--- tests/runtests.py | 3 +-- 7 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/ts_utils/paths.py b/lib/ts_utils/paths.py index 63119231720d..adec14a57d53 100644 --- a/lib/ts_utils/paths.py +++ b/lib/ts_utils/paths.py @@ -24,8 +24,7 @@ def distribution_path(distribution_name: str) -> Path: def tests_path(distribution_name: str) -> Path: if distribution_name == "stdlib": return STDLIB_PATH / TESTS_DIR - else: - return STUBS_PATH / distribution_name / TESTS_DIR + return STUBS_PATH / distribution_name / TESTS_DIR def test_cases_path(distribution_name: str) -> Path: @@ -35,5 +34,4 @@ def test_cases_path(distribution_name: str) -> Path: def allowlists_path(distribution_name: str) -> Path: if distribution_name == "stdlib": return tests_path("stdlib") / "stubtest_allowlists" - else: - return tests_path(distribution_name) + return tests_path(distribution_name) diff --git a/lib/ts_utils/utils.py b/lib/ts_utils/utils.py index 4dfe05488275..7ac025be93bc 100644 --- a/lib/ts_utils/utils.py +++ b/lib/ts_utils/utils.py @@ -194,8 +194,7 @@ def allowlists(distribution_name: str) -> list[str]: if distribution_name == "stdlib": return ["common.txt", platform_allowlist, version_allowlist, combined_allowlist, local_version_allowlist] - else: - return ["stubtest_allowlist.txt", platform_allowlist] + return ["stubtest_allowlist.txt", platform_allowlist] # ==================================================================== diff --git a/pyproject.toml b/pyproject.toml index db4e5501b289..e78661ba435e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,7 @@ select = [ "FA", # flake8-future-annotations "I", # isort "PGH", # pygrep-hooks + "RET", # flake8-return "RUF", # Ruff-specific and unused-noqa "UP", # pyupgrade "YTT", # flake8-2020 diff --git a/scripts/stubsabot.py b/scripts/stubsabot.py index aee09eb3e8a2..b2fbe0491f82 100644 --- a/scripts/stubsabot.py +++ b/scripts/stubsabot.py @@ -134,8 +134,7 @@ def __str__(self) -> str: def new_version(self) -> str: if self.new_version_spec.operator == "==": return str(self.new_version_spec)[2:] - else: - return str(self.new_version_spec) + return str(self.new_version_spec) @dataclass diff --git a/scripts/sync_protobuf/google_protobuf.py b/scripts/sync_protobuf/google_protobuf.py index da2ced6f9fc7..2580807781b0 100644 --- a/scripts/sync_protobuf/google_protobuf.py +++ b/scripts/sync_protobuf/google_protobuf.py @@ -48,11 +48,10 @@ def extract_proto_file_paths(temp_dir: Path) -> list[str]: """ with open(temp_dir / EXTRACTED_PACKAGE_DIR / "python" / "dist" / "BUILD.bazel") as file: matched_lines = filter(None, (re.search(PROTO_FILE_PATTERN, line) for line in file)) - proto_files = [ + return [ EXTRACTED_PACKAGE_DIR + "/src/google/protobuf/" + match.group(1).replace("compiler_", "compiler/") + ".proto" for match in matched_lines ] - return proto_files def main() -> None: diff --git a/tests/mypy_test.py b/tests/mypy_test.py index b1ad54735be9..bfa656de0a8b 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -268,10 +268,9 @@ def run_mypy( print_success_msg() if result.returncode == 0: return MypyResult.SUCCESS - elif result.returncode == 1: + if result.returncode == 1: return MypyResult.FAILURE - else: - return MypyResult.CRASH + return MypyResult.CRASH def add_third_party_files( diff --git a/tests/runtests.py b/tests/runtests.py index 1c5ba0c18ab9..817778b23df9 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -29,8 +29,7 @@ def _parse_jsonc(json_text: str) -> str: # strip comments from the file lines = [line for line in json_text.split("\n") if not line.strip().startswith("//")] # strip trailing commas from the file - valid_json = re.sub(r",(\s*?[\}\]])", r"\1", "\n".join(lines)) - return valid_json + return re.sub(r",(\s*?[\}\]])", r"\1", "\n".join(lines)) def _get_strict_params(stub_path: str) -> list[str]: From e37aa4e59fc6f94321f6e1e8e6fd783c158015b9 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 30 Dec 2024 16:38:26 -0500 Subject: [PATCH 2/2] ignore RET505-RET508 --- lib/ts_utils/paths.py | 6 ++++-- lib/ts_utils/utils.py | 3 ++- pyproject.toml | 9 +++++++-- scripts/stubsabot.py | 3 ++- tests/mypy_test.py | 5 +++-- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/ts_utils/paths.py b/lib/ts_utils/paths.py index adec14a57d53..63119231720d 100644 --- a/lib/ts_utils/paths.py +++ b/lib/ts_utils/paths.py @@ -24,7 +24,8 @@ def distribution_path(distribution_name: str) -> Path: def tests_path(distribution_name: str) -> Path: if distribution_name == "stdlib": return STDLIB_PATH / TESTS_DIR - return STUBS_PATH / distribution_name / TESTS_DIR + else: + return STUBS_PATH / distribution_name / TESTS_DIR def test_cases_path(distribution_name: str) -> Path: @@ -34,4 +35,5 @@ def test_cases_path(distribution_name: str) -> Path: def allowlists_path(distribution_name: str) -> Path: if distribution_name == "stdlib": return tests_path("stdlib") / "stubtest_allowlists" - return tests_path(distribution_name) + else: + return tests_path(distribution_name) diff --git a/lib/ts_utils/utils.py b/lib/ts_utils/utils.py index 7ebdf8f3e755..ff18f2cec6a3 100644 --- a/lib/ts_utils/utils.py +++ b/lib/ts_utils/utils.py @@ -194,7 +194,8 @@ def allowlists(distribution_name: str) -> list[str]: if distribution_name == "stdlib": return ["common.txt", platform_allowlist, version_allowlist, combined_allowlist, local_version_allowlist] - return ["stubtest_allowlist.txt", platform_allowlist] + else: + return ["stubtest_allowlist.txt", platform_allowlist] # ==================================================================== diff --git a/pyproject.toml b/pyproject.toml index a4a0e2410a2b..e1a660ec9d5d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -90,11 +90,16 @@ ignore = [ ### # Rules we don't want or don't agree with ### - # Slower and more verbose https://github.com/astral-sh/ruff/issues/7871 - "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` # Used for direct, non-subclass type comparison, for example: `type(val) is str` # see https://github.com/astral-sh/ruff/issues/6465 "E721", # Do not compare types, use `isinstance()` + # `else` can be clearer to read, especially for short branches or elif chains + "RET505", # Unnecessary `{branch}` after `return` statement + "RET506", # Unnecessary `{branch}` after `raise` statement + "RET507", # Unnecessary `{branch}` after `continue` statement + "RET508", # Unnecessary `{branch}` after `break` statement + # Slower and more verbose https://github.com/astral-sh/ruff/issues/7871 + "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` ### # False-positives, but already checked by type-checkers ### diff --git a/scripts/stubsabot.py b/scripts/stubsabot.py index b2fbe0491f82..aee09eb3e8a2 100644 --- a/scripts/stubsabot.py +++ b/scripts/stubsabot.py @@ -134,7 +134,8 @@ def __str__(self) -> str: def new_version(self) -> str: if self.new_version_spec.operator == "==": return str(self.new_version_spec)[2:] - return str(self.new_version_spec) + else: + return str(self.new_version_spec) @dataclass diff --git a/tests/mypy_test.py b/tests/mypy_test.py index bfa656de0a8b..b1ad54735be9 100755 --- a/tests/mypy_test.py +++ b/tests/mypy_test.py @@ -268,9 +268,10 @@ def run_mypy( print_success_msg() if result.returncode == 0: return MypyResult.SUCCESS - if result.returncode == 1: + elif result.returncode == 1: return MypyResult.FAILURE - return MypyResult.CRASH + else: + return MypyResult.CRASH def add_third_party_files(