Skip to content

chore(bigtable): use ruff for generated sync files#16713

Open
daniel-sanche wants to merge 5 commits intomainfrom
bigtable_sync_format_3
Open

chore(bigtable): use ruff for generated sync files#16713
daniel-sanche wants to merge 5 commits intomainfrom
bigtable_sync_format_3

Conversation

@daniel-sanche
Copy link
Copy Markdown
Contributor

Bigtable uses CrossSync to auto-generate sync classes from async ones. Previously, bigtable used black for formatting, so generated files were formatted in the same way. This PR changes to ruff, so generated files match the rest of the repo

@daniel-sanche daniel-sanche requested a review from a team as a code owner April 17, 2026 22:37
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request transitions the project's code generation and formatting from black and autoflake to ruff. Key changes include updating the generate.py script to invoke ruff via subprocess, modifying noxfile.py dependencies, and re-formatting various auto-generated source and test files. Review feedback suggests defining RUFF_VERSION explicitly in the noxfile.py, providing a filename via --stdin-filename when piping code to ruff to ensure correct configuration application, and raising a RuntimeError with detailed output if the formatting process fails.

"pytest-asyncio",
BLACK_VERSION,
"autoflake",
RUFF_VERSION,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The variable RUFF_VERSION is used here, but its definition is not visible in the provided diff. If it was intended to replace BLACK_VERSION, please ensure that RUFF_VERSION is defined as a constant (e.g., RUFF_VERSION = "ruff==0.4.4") earlier in the file and that the now-unused BLACK_VERSION definition is removed.

Comment on lines +64 to +80
result = subprocess.run(
[
sys.executable,
"-m",
"ruff",
"check",
"--select",
"I,F401",
"--fix",
"--line-length=88",
"-",
],
input=full_str,
text=True,
capture_output=True,
check=True,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When running ruff check on code provided via stdin (-), it is highly recommended to provide a filename using --stdin-filename. This allows ruff to correctly identify the file's location, which is essential for determining if imports are first-party or third-party (the I rule) and for applying any path-specific configuration rules from pyproject.toml.

Suggested change
result = subprocess.run(
[
sys.executable,
"-m",
"ruff",
"check",
"--select",
"I,F401",
"--fix",
"--line-length=88",
"-",
],
input=full_str,
text=True,
capture_output=True,
check=True,
)
result = subprocess.run(
[
sys.executable,
"-m",
"ruff",
"check",
"--select",
"I,F401",
"--fix",
"--line-length=88",
"--stdin-filename",
self.output_path,
"-",
],
input=full_str,
text=True,
capture_output=True,
check=True,
)

Comment on lines +84 to +99
result = subprocess.run(
[
sys.executable,
"-m",
"ruff",
"format",
"--line-length=88",
"--config",
"format.skip-magic-trailing-comma=true",
"-",
],
input=full_str,
text=True,
capture_output=True,
check=True,
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the ruff check call, ruff format should also be provided with --stdin-filename to ensure it respects any file-specific formatting configurations defined in the repository's configuration files.

Suggested change
result = subprocess.run(
[
sys.executable,
"-m",
"ruff",
"format",
"--line-length=88",
"--config",
"format.skip-magic-trailing-comma=true",
"-",
],
input=full_str,
text=True,
capture_output=True,
check=True,
)
result = subprocess.run(
[
sys.executable,
"-m",
"ruff",
"format",
"--line-length=88",
"--config",
"format.skip-magic-trailing-comma=true",
"--stdin-filename",
self.output_path,
"-",
],
input=full_str,
text=True,
capture_output=True,
check=True,
)

Comment on lines +101 to +102
except subprocess.CalledProcessError as e:
print(f"ruff formatting failed: {e.stderr}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

In a code generation script, it is generally better to raise an exception when formatting fails rather than just printing to stdout. This ensures that the failure is caught by CI/CD pipelines (e.g., via nox) and prevents the script from silently returning unformatted or partially formatted code. Additionally, including e.stdout in the error message can be helpful as ruff sometimes outputs error details there.

Suggested change
except subprocess.CalledProcessError as e:
print(f"ruff formatting failed: {e.stderr}")
except subprocess.CalledProcessError as e:
raise RuntimeError(
f"ruff formatting failed for {self.output_path}: {e.stderr or e.stdout}"
) from e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant