Repository navigation
style(repo): replace yapf with ruff and adopt the Google Python Style Guide's 4-space indent - #302
Conversation
643ec31 to
b8e62a8
Compare
aghassemlouei
left a comment
There was a problem hiding this comment.
Replacing yapf and pyflakes with ruff under a single ruff.toml configuration and enforcing ruff check . plus ruff format --check . in CI is a clean improvement across tools/, tests/, and gem4gov-cli/.
- Now that
#310is merged intomain,.github/workflows/ci.ymlonmainincludes thetflintjob, so this branch will need a rebase ontomainto resolve theci.ymlconflict. #299flattenstests/gem4gov_cli/*andtests/tools/*into top-leveltests/test_*.pyfiles and expands the unit test suite. Sequencing#299first (or rebasing#302after#299lands and re-runningruff format .andruff check --fix .) will avoid a multi-file rename and reformat conflict acrosstests/.
aghassemlouei
left a comment
There was a problem hiding this comment.
Approving the ruff and 4-space Google Python Style Guide adoption. Now that #305 has merged into main (0fb5fcb3), tools/check_boilerplate.py and tools/check_links.py have a small conflict with the .venv ignore additions from #305 (and .git-blame-ignore-revs will need the rebased Commit 1 SHA). Once you rebase this branch onto main, I will re-approve and merge it with rebase so .git-blame-ignore-revs stays valid.
Add a root ruff.toml that follows the Google Python Style Guide with 4-space indentation, an 80-character line limit, a Python 3.11 target, and the E, W, F, and I rule sets, then run ruff format over every tracked Python file and let ruff check sort imports one per line. Fix the remaining ruff check findings by hand. Wrap long string literals with implicit concatenation, rewrap long docstrings and comments, compare against None and False with is, rename ambiguous loop variables named l, and strip trailing whitespace from the analytics SQL queries. Update the source-matching assertions in tests/tools/test_gemini_enterprise_fixes.py to the reformatted gem4gov.py, and check the create_engine signature and its default values through the AST.
Run ruff format and ruff check from tools/lint.sh and the CI python job in place of yapf and pyflakes, and pin ruff in tools/requirements.txt to the version used for the reformat. Name the CI lint step after the wider ruff rule selection, and point the Gemini style guide at ruff format, ruff check, and the root ruff.toml.
Add .git-blame-ignore-revs with the ruff reformat commit so git blame and the GitHub blame view skip the formatting-only changes.
b8e62a8 to
7112ab0
Compare
aghassemlouei
left a comment
There was a problem hiding this comment.
Rebased onto main (21f8755b), resolved the .venv ignore conflict in tools/check_boilerplate.py and tools/check_links.py, and updated .git-blame-ignore-revs with the rebased Commit 1 SHA (95907a12).
Description
This pull request proposes a repository Python style for discussion. The goal is to match the Google Python Style Guide, which uses 4-space indentation and 80-character lines, and to replace
yapfwithruffbecause of its speed and broad community adoption for Python formatting. Switching back to 2-space indentation later takes a one-lineindent-widthchange in the rootruff.tomlfile and a rerun ofruff format.Following the published Google Python Style Guide keeps every Python file in this Google project aligned with Google's corporate coding standards, and gives new projects such as the planned
cli/tool one clear standard from their first commit.Fixes #304
The branch splits the work into three commits so you can review the tooling apart from the mechanical reformat, and every commit passes the CI checks that apply to it.
ruff.tomlfile, reformats the 28 tracked Python files thatruff formatchanges, and fixes everyruff checkfinding.__init__.pyfiles untouched because they are already formatted.tools/lint.shscript, thetools/requirements.txtfile, the CIpython-testsjob, and the.gemini/styleguide.mdPython rules fromyapfandpyflakestoruff..git-blame-ignore-revsfile sogit blameand the GitHub blame view skip commit 1.Configuration
ruff.tomlfile setsline-length = 80,indent-width = 4, andtarget-version = "py311"to match the CIpython-versionsetting.E,W,F, andIrule sets.Frule set carries the pyflakes checks, so the renamedPython Lint and CodeQL py/unused-local-variable Check (ruff)CI step keeps the expectations that the oldpyflakesstep enforced.typingandcollections.abcexempt, to follow the Google import style..terraform,.venv,venv,node_modules, and Jupyter notebooks, which keeps thevertex-mlopsnotebook out of this reformat.tools/requirements.txtfile pinsruff==0.17.1so a new ruff release cannot change formatter output and fail CI without a code change.Dwith the Google docstring convention,N,UP,B,SIM,PL, andRUFstay disabled repository-wide because enabling them requires non-mechanical rewrites of existing code.cli/project, can extend the rootruff.tomlfile and enable those stricter rule sets.Lint fixes in commit 1
NoneandFalsein thegem4gov.pyCLI useisandis not.lin thetools/changelog.py,tools/check_links.py, andtools/tfdoc.pyscripts and in one test use descriptive names.app.pySQL queries drop the trailing whitespace afterSELECT *.tests/tools/test_gemini_enterprise_fixes.pytest matched single-quotedgem4gov.pysource, so they now match the reformatted source.create_enginesignature assertion reads the argument names and theFalseandNonedefault values from the AST, which keeps it stable across future formatting.noqacomments.Notes for reviewers
pyflakesCI step, and theruffCI steps arrive in commit 2 after the code already conforms..git-blame-ignore-revsfile whenever a rebase or squash changes that SHA.git config blame.ignoreRevsFile .git-blame-ignore-revs, and GitHub reads the file automatically.tools/lint.shscript, so whichever lands second needs a small rebase in that file.tests/scripts/*andtests/tools/*files to a flattests/directory, so the reformat of the moved test files produces rename conflicts.ruff formatandruff check --fixafter the rebase, followed by a refreshed SHA in the.git-blame-ignore-revsfile.Type of Change
This style and tooling change fits none of the listed types and changes no runtime behavior.
Deployment & Compliance Impact
This change touches only Python tooling, tests, and CLI source formatting, so it affects no deployment or control implementation.
Checklist
Code Quality & Reusability
modules/orfast/stages-aw/can be leveraged for this change.docs/naming-convention.md.Documentation
README.mdof the modified module or blueprint.Security
Testing
The unchecked items do not apply because the change adds no module, Terraform resource, variable, output, or regime-specific configuration.
Testing Performed
The checks below ran locally against Python 3.11 to match CI, using the pinned
ruff==0.17.1from thetools/requirements.txtfile.ruff format --check .reports 223 files already formatted, compared with 28 files to reformat onmainunder the sameruff.tomlfile.ruff check .passes, compared with 665 findings onmainunder the sameruff.tomlfile.pytestpasses all 66 tests, the same count as onmain.python3 -m pyflakes tools tests blueprints/gemini-enterprise/gem4gov-cliexits cleanly, which shows parity with theFrule set and confirms that commit 1 passes the existingpyflakesCI step.bash -n tools/lint.shexits cleanly.mainshows only import order, docstring whitespace, and the lint fixes listed above.gem4gov.py --help,gem4gov.py license --help, andgem4gov.py license retract --helpcommands run cleanly and render the same help text, because Click reflows the rewrapped docstrings and help strings.python3 tools/check_boilerplate.py tools tests blueprintsreports the same 10blueprints/gemini-enterprisefiles on this branch and onmain, so that failure predates this change.tools/lint.shrun andshellcheckdid not run because Terraform and ShellCheck are not installed locally.