-
Notifications
You must be signed in to change notification settings - Fork 1.7k
chore(bigtable): use ruff for generated sync files #16713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -51,18 +51,55 @@ def render(self, with_formatter=True, save_to_disk: bool = True) -> str: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Render the file to a string, and optionally save to disk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Args: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with_formatter: whether to run the output through black before returning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with_formatter: whether to run the output through ruff before returning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| save_to_disk: whether to write the output to the file path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| full_str = self.header + ast.unparse(self.tree) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if with_formatter: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import black # type: ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import autoflake # type: ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| full_str = black.format_str( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| autoflake.fix_code(full_str, remove_all_unused_imports=True), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mode=black.FileMode(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import subprocess | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import sys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Run ruff check | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| full_str = result.stdout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Run ruff format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+84
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| full_str = result.stdout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except subprocess.CalledProcessError as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| print(f"ruff formatting failed: {e.stderr}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+101
to
+102
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if save_to_disk: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import os | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.makedirs(os.path.dirname(self.output_path), exist_ok=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,8 +43,7 @@ | |
| "pytest", | ||
| "pytest-cov", | ||
| "pytest-asyncio", | ||
| BLACK_VERSION, | ||
| "autoflake", | ||
| RUFF_VERSION, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable |
||
| ] | ||
| UNIT_TEST_EXTERNAL_DEPENDENCIES: List[str] = [] | ||
| UNIT_TEST_LOCAL_DEPENDENCIES: List[str] = [] | ||
|
|
@@ -553,8 +552,7 @@ def generate_sync(session): | |
| """ | ||
| Re-generate sync files for the library from CrossSync-annotated async source | ||
| """ | ||
| session.install(BLACK_VERSION) | ||
| session.install("autoflake") | ||
| session.install(RUFF_VERSION) | ||
| session.run("python", ".cross_sync/generate.py", ".") | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When running
ruff checkon code provided via stdin (-), it is highly recommended to provide a filename using--stdin-filename. This allowsruffto correctly identify the file's location, which is essential for determining if imports are first-party or third-party (theIrule) and for applying any path-specific configuration rules frompyproject.toml.