Skip to content

Conversation

dorde-antic
Copy link
Contributor

@dorde-antic dorde-antic commented Sep 8, 2025

Motivation

This PR introduces tooling to enforce consistent Python formatting and linting within the rocMLIR repository. Resolves https://github.com/ROCm/rocMLIR-internal/issues/1872/

Technical Details

Added .github/workflows/ci.yml to implement a GitHub Actions workflow that runs on develop for both pushes and pull requests. The workflow installs flake8, yapf, and pep8-naming, then runs:

  • flake8, excluding the external/ directory
  • yapf --dry-run, excluding external/**/*.py, and reporting format issues

Added .pre-commit-config.yaml to configure local pre-commit hooks with:

  • YAPF (auto-formatting) using -i
  • Flake8 (with pep8-naming plugin)
  • An exclude rule to ignore files under external/

Added .style.yapf to define YAPF formatting preferences.

Test Plan

1. Github Actions CI Testing

Temporarily adding different types python files to test if they trigger Github Actions and if the CI works properly.
I'll try more cases with different py files (some that should pass checks and some that should fail)

Warning

Do not merge with python files from this PR since they are used for testing

2. Local Testing

  • Run pre-commit install, followed by pre-commit run --all-files to confirm YAPF and Flake8 are working correctly.
  • Attempt to commit changes (with or without dummy files), and verify that the pre-commit hooks execute upon commit.

3. Github Actions CI + pre-commit

Confirm that if pre-commit made changes, it made them in a way that they will pass the Github Actions check.

Test Result

In progress ⏳

Submission Checklist

Copy link

codecov bot commented Sep 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1978      +/-   ##
===========================================
+ Coverage    79.50%   79.62%   +0.11%     
===========================================
  Files          100      102       +2     
  Lines        31016    31910     +894     
  Branches      4819     5047     +228     
===========================================
+ Hits         24659    25406     +747     
- Misses        4245     4312      +67     
- Partials      2112     2192      +80     
Flag Coverage Δ
mfma ?
navi4x 79.62% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.
see 50 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dorde-antic dorde-antic marked this pull request as ready for review September 9, 2025 19:18
@dorde-antic dorde-antic requested a review from causten as a code owner September 9, 2025 19:18
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces Python code formatting and linting infrastructure for the rocMLIR repository, implementing both CI automation and local development tooling to enforce consistent Python code style.

  • Adds GitHub Actions CI workflow for automated Python code quality checks on pull requests and pushes
  • Configures pre-commit hooks with YAPF formatter and Flake8 linter for local development
  • Sets up YAPF style configuration based on PEP8 with project-specific customizations

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
pip_requirements.txt Adds Python formatting and linting dependencies
.github/workflows/ci.yml Implements CI workflow for Python code quality checks
.pre-commit-config.yaml Configures local pre-commit hooks for YAPF and Flake8
.style.yapf Defines YAPF formatting style configuration
mlir/utils/performance/dummyfile.py Test file with intentional formatting issues for CI validation

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@dorde-antic
Copy link
Contributor Author

dorde-antic commented Sep 9, 2025

  1. We might want to consider making Python Lint and Format Check a required check for PRs (once it's tested on different cases so that it won't block PRs from merging for wrong reason)
  2. We should thing about defining a naming convention for Python (and other related stuff) across the project, so that we adjust settings for flake and yapf before merging

Atm this is how CI (Github Action) look like in case of current py file:
image

and this is example how it looks for other files we already have inside mlir dir (atm with current settings):
image

@dhernandez0
Copy link
Contributor

please, can you reformat all existing python files in this PR?

@dorde-antic
Copy link
Contributor Author

please, can you reformat all existing python files in this PR?

i can, btw, changes introduced in this PR would make Github action fail only if files that are changed in future PRs are python and inside mlir dir. So it wouldn't fail because of all other existing files are unformatted. The screenshot above that shows all python files in mlir/ ... was testing to see how it would look (when tried on all py files),

but for example for this PR Github Actions CI would only show this:
image
and not all of the py files in mlir/


on:
pull_request:
branches: [develop]
Copy link
Member

Choose a reason for hiding this comment

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

Add release branches too ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@umangyadav one check, it would be all from release/* ?

exclude: '^external/.*'

repos:
- repo: https://github.com/google/yapf
Copy link
Member

Choose a reason for hiding this comment

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

Because it is pre-commit hook, does it mean we need to update our docker locally for this to work ? You may want to notify team about this once this is merged.

@dorde-antic
Copy link
Contributor Author

And this is example when YAPF check fails (happened after switching to google style):

image

@@ -0,0 +1,4 @@
[style]
based_on_style = google
column_limit = 79
Copy link
Member

Choose a reason for hiding this comment

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

why 79? . it's an odd number.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's flake8's default, but we can change it here and on flake's as well if you want

@umangyadav umangyadav requested a review from Copilot September 19, 2025 12:46
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

id: changes
shell: bash
run: |
BASE_SHA="${{ github.event.pull_request.base.sha }}"
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

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

The BASE_SHA will be empty for push events since github.event.pull_request doesn't exist. This will cause the git diff command to fail. Use a conditional to handle both pull request and push events differently, or use github.event.before for push events.

Suggested change
BASE_SHA="${{ github.event.pull_request.base.sha }}"
if [ "${{ github.event_name }}" = "pull_request" ]; then
BASE_SHA="${{ github.event.pull_request.base.sha }}"
else
BASE_SHA="${{ github.event.before }}"
fi

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

@dorde-antic this comment looks like a valid issue. can you check ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, btw one thing about push events is that we didn't test them (since it would mean pushing to develop to try it) do you want that we keep push events as well? I think it make sense to have them since code can be changed that way too, the only thing is I'm not sure how to test it without spamming the commit history on develop

Copy link
Member

Choose a reason for hiding this comment

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

We can skip testing on push for now (you can monitor for a few pushes later) but my concern is that we should include path to get SHA for the push events as the copilot suggested.

set -eou pipefail

NEED_POP=0
if ! git diff --quiet || ! git diff --quiet -- .; then
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

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

The condition ! git diff --quiet -- . is redundant since ! git diff --quiet already checks the entire working directory. The second condition can be removed.

Suggested change
if ! git diff --quiet || ! git diff --quiet -- .; then
if ! git diff --quiet; then

Copilot uses AI. Check for mistakes.

@umangyadav umangyadav merged commit 2363cbb into develop Sep 22, 2025
8 of 22 checks passed
@umangyadav umangyadav deleted the py-tidyandformatter branch September 22, 2025 12:12
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.

3 participants