chore(bigtable): use ruff for generated sync files#16713
chore(bigtable): use ruff for generated sync files#16713daniel-sanche wants to merge 5 commits intomainfrom
Conversation
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
| 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, | ||
| ) |
There was a problem hiding this comment.
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.
| 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, | |
| ) |
| 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, | ||
| ) |
There was a problem hiding this comment.
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.
| 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, | |
| ) |
| except subprocess.CalledProcessError as e: | ||
| print(f"ruff formatting failed: {e.stderr}") |
There was a problem hiding this comment.
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.
| 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 |
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