From 0cfdcb2145c2c7094bb608d683c24f38271b8d54 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 28 Feb 2024 23:50:19 -0500 Subject: [PATCH 1/8] Less selective about pyupgrades rules --- pyproject.toml | 19 +++++++++++-------- scripts/stubsabot.py | 2 +- stubs/six/six/moves/builtins.pyi | 5 ++++- tests/pytype_test.py | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c3e1cdd1c112..361ebeec1656 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,6 +27,7 @@ exclude = [ select = [ "FA", # flake8-future-annotations "I", # isort + "UP", # pyupgrade # Only enable rules that have safe autofixes: "F401", # Remove unused imports "PYI009", # use `...`, not `pass`, in empty class bodies @@ -39,14 +40,16 @@ select = [ "PYI032", # use `object`, not `Any`, as the second parameter to `__eq__` "PYI055", # multiple `type[T]` usages in a union "PYI058", # use `Iterator` as the return type for `__iter__` methods - "UP004", # Remove explicit `object` inheritance - "UP006", # PEP-585 autofixes - "UP007", # PEP-604 autofixes - "UP013", # Class-based syntax for TypedDicts - "UP014", # Class-based syntax for NamedTuples - "UP019", # Use str over typing.Text - "UP035", # import from typing, not typing_extensions, wherever possible - "UP039", # don't use parens after a class definition with no bases +] +ignore = [ + # We like explicit open modes + "UP015", # redundant-open-modes +] + +[tool.ruff.lint.extend-per-file-ignores] +# Generated protobuf files: +"*_pb2.pyi" = [ + "UP036", # outdated-version-block ] [tool.ruff.lint.isort] diff --git a/scripts/stubsabot.py b/scripts/stubsabot.py index b2a86e31d0f6..635117c4a9bc 100644 --- a/scripts/stubsabot.py +++ b/scripts/stubsabot.py @@ -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/stubs/six/six/moves/builtins.pyi b/stubs/six/six/moves/builtins.pyi index 9596ba032aa0..b497e50424ad 100644 --- a/stubs/six/six/moves/builtins.pyi +++ b/stubs/six/six/moves/builtins.pyi @@ -1 +1,4 @@ -from builtins import * +# flake8: noqa: NQA102 # https://github.com/plinss/flake8-noqa/issues/22 +# six explicitely re-exports builtins. Normally this is something we'd want to avoid. +# But this is specifically a compatibility package. +from builtins import * # noqa: UP029 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: From 8391afc658a539269d5c74d6e06808d466a54e38 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 28 Feb 2024 23:58:24 -0500 Subject: [PATCH 2/8] Update stubs/six/six/moves/builtins.pyi --- stubs/six/six/moves/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/six/six/moves/builtins.pyi b/stubs/six/six/moves/builtins.pyi index b497e50424ad..1f3812602c5e 100644 --- a/stubs/six/six/moves/builtins.pyi +++ b/stubs/six/six/moves/builtins.pyi @@ -1,4 +1,4 @@ # flake8: noqa: NQA102 # https://github.com/plinss/flake8-noqa/issues/22 -# six explicitely re-exports builtins. Normally this is something we'd want to avoid. +# six explicitly re-exports builtins. Normally this is something we'd want to avoid. # But this is specifically a compatibility package. from builtins import * # noqa: UP029 From 0d47df9fb15321edab8d542578dbf3a4d3dbf038 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 00:10:51 -0500 Subject: [PATCH 3/8] Use message for comments rather than rule name --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 361ebeec1656..aa8a1ce7ecdc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,13 +43,13 @@ select = [ ] ignore = [ # We like explicit open modes - "UP015", # redundant-open-modes + "UP015", # Unnecessary open mode parameters ] [tool.ruff.lint.extend-per-file-ignores] # Generated protobuf files: "*_pb2.pyi" = [ - "UP036", # outdated-version-block + "UP036", # Version block is outdated for minimum Python version ] [tool.ruff.lint.isort] From bf358ed9d629ba7cf644c987d5212a85c6beb0bd Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 12:04:27 -0500 Subject: [PATCH 4/8] Ignore "modernization" autofix rules from test_cases --- pyproject.toml | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index aa8a1ce7ecdc..278135f82fb0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,13 +41,14 @@ select = [ "PYI055", # multiple `type[T]` usages in a union "PYI058", # use `Iterator` as the return type for `__iter__` methods ] -ignore = [ - # We like explicit open modes - "UP015", # Unnecessary open mode parameters -] -[tool.ruff.lint.extend-per-file-ignores] +[tool.ruff.lint.per-file-ignores] +# Disable "mordernization" rules with autofixes from test cases as they often +# deliberately contain code that might not be considered idiomatic or modern +# These can be run manually once in a while +"**/test_cases/**/*.py" = ["UP"] # Generated protobuf files: +# TODO: Re-run sync_tensorflow_protobuf_stubs.sh with this rule enabled to remove this entry "*_pb2.pyi" = [ "UP036", # Version block is outdated for minimum Python version ] From 170653f661f2a496812b4f4347d341a9800382f1 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 12:44:37 -0500 Subject: [PATCH 5/8] s2clientprotocol --- stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi b/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi index e954e46118e1..53553c043bbc 100644 --- a/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi +++ b/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi @@ -4,7 +4,7 @@ isort:skip_file """ import builtins import collections.abc -import sys +import typing as typing_extensions import google.protobuf.descriptor import google.protobuf.internal.containers @@ -12,11 +12,6 @@ import google.protobuf.message import s2clientprotocol.common_pb2 import s2clientprotocol.error_pb2 -if sys.version_info >= (3, 8): - import typing as typing_extensions -else: - import typing_extensions - DESCRIPTOR: google.protobuf.descriptor.FileDescriptor @typing_extensions.final From 9c597167fc791cbc833def10343b346ae2aed834 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 12:46:04 -0500 Subject: [PATCH 6/8] Revert "s2clientprotocol" This reverts commit 170653f661f2a496812b4f4347d341a9800382f1. --- stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi b/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi index 53553c043bbc..e954e46118e1 100644 --- a/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi +++ b/stubs/s2clientprotocol/s2clientprotocol/query_pb2.pyi @@ -4,7 +4,7 @@ isort:skip_file """ import builtins import collections.abc -import typing as typing_extensions +import sys import google.protobuf.descriptor import google.protobuf.internal.containers @@ -12,6 +12,11 @@ import google.protobuf.message import s2clientprotocol.common_pb2 import s2clientprotocol.error_pb2 +if sys.version_info >= (3, 8): + import typing as typing_extensions +else: + import typing_extensions + DESCRIPTOR: google.protobuf.descriptor.FileDescriptor @typing_extensions.final From 1ffbf847fc6bc0210b756bc8c5ce0da9fc08f390 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 13:12:00 -0500 Subject: [PATCH 7/8] Ignore UP038 everywhere --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 278135f82fb0..e965e9425526 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -41,6 +41,10 @@ select = [ "PYI055", # multiple `type[T]` usages in a union "PYI058", # use `Iterator` as the return type for `__iter__` methods ] +ignore = [ + # Slower and more verbose https://github.com/astral-sh/ruff/issues/7871 + "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` +] [tool.ruff.lint.per-file-ignores] # Disable "mordernization" rules with autofixes from test cases as they often From 8835b8c42bbe38425c64b0c44a720845e71db0ca Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 29 Feb 2024 13:14:31 -0500 Subject: [PATCH 8/8] Update pyproject.toml Co-authored-by: Alex Waygood --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index e965e9425526..30812adf1bb2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -47,7 +47,7 @@ ignore = [ ] [tool.ruff.lint.per-file-ignores] -# Disable "mordernization" rules with autofixes from test cases as they often +# Disable "modernization" rules with autofixes from test cases as they often # deliberately contain code that might not be considered idiomatic or modern # These can be run manually once in a while "**/test_cases/**/*.py" = ["UP"]