CI: Run pre-commit gh action on only changed files
State: closed


- [x] closes #38862
- [ ] tests added / passed
- [x] Ensure all linting tests pass, see [here](https://pandas.pydata.org/pandas-docs/dev/development/contributing.html#code-standards) for how to run them
- [ ] whatsnew entry
-----
Since pre-commit in comment-bot runs on all files, it's a bit slow. Running it only on diff should speed it up significantly.

Labels:
name: CI
description: Continuous Integration
name: Code Style
description: Code style, linting, code_checks

Comments:
Thanks @gunjan-solanki !
hmm would be ok with doing this locally

but is there a reason to do this in the CI itself?

iow if there is a failure in a prior commit that is not corrected this would ignore it right?
Just checking I've understood - you're asking what happens if someone makes a commit with some linting error, then makes another commit, then pushes, and then uses this comment bot?

If so, then it should be fine, e.g.:
```console
$ git reset --hard upstream/master
HEAD is now at 339b1a658a CI: Run pre-commit gh action on only changed files (#39505)
$ git checkout -b tmp
Switched to a new branch 'tmp'
$ # add trailing whitespace
$ git diff
diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py
index b1675fa5c5..cc8929be08 100644
--- a/pandas/io/formats/info.py
+++ b/pandas/io/formats/info.py
@@ -228,7 +228,7 @@ class DataFrameInfo(BaseInfo):
     """
 
     def __init__(
-        self,
+        self, 
         data: DataFrame,
         memory_usage: Optional[Union[bool, str]] = None,
     ):
$ git commit -a -m 'add trailing whitespace' --no-verify
[tmp 4c40756c9c] add trailing whitespace
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git commit -m 'noop' --allow-empty --no-verify
[tmp 4c9b6d1d4c] noop
$ pre-commit run --from-ref=master --to-ref=HEAD --all-files
black..................................................................................................Failed
- hook id: black
- files were modified by this hook

reformatted pandas/io/formats/info.py
All done! ✨ 🍰 ✨
1 file reformatted.

flake8.................................................................................................Passed
flake8 (cython)....................................................................(no files to check)Skipped
flake8 (cython template)...........................................................(no files to check)Skipped
isort..................................................................................................Passed
pyupgrade..............................................................................................Passed
rst ``code`` is two backticks......................................................(no files to check)Skipped
rst directives end with two colons.....................................................................Passed
rst ``inline code`` next to normal text................................................................Passed
Generate pip dependency from conda.................................................(no files to check)Skipped
flake8-rst.........................................................................(no files to check)Skipped
Check for non-standard imports.........................................................................Passed
Check for non-standard numpy.random-related imports excluding pandas/_testing.py.......................Passed
Check for non-standard imports in test suite.......................................(no files to check)Skipped
Check for incorrect code block or IPython directives...................................................Passed
Check for use of not concatenated strings..............................................................Passed
Check for strings with wrong placed spaces.............................................................Passed
Check for import of private attributes across modules..................................................Passed
Check for use of private functions across modules......................................................Passed
Check for use of bare pytest raises................................................(no files to check)Skipped
Check for inconsistent use of pandas namespace in tests............................(no files to check)Skipped
Check for use of Union[Series, DataFrame] instead of FrameOrSeriesUnion alias..........................Passed
Check for use of foo.__class__ instead of type(foo)....................................................Passed
Check for outdated annotation syntax and missing error codes...........................................Passed
Check for use of np.bool instead of np.bool_...........................................................Passed
Check code for instances of os.remove..............................................(no files to check)Skipped
Check code for instances of pd.api.types...........................................(no files to check)Skipped
Strip unnecessary `# noqa`s............................................................................Passed
Fix End of Files.......................................................................................Passed
Trim Trailing Whitespace...............................................................................Passed
codespell..............................................................................................Passed
no-string-hints........................................................................................Passed
```

---

Just to clarify, the change in this PR only affects the comment bot.

The job which runs the CI checks, which is run on every pull requests, still runs the checks on _all_ files
ok as long as we run in all files in CI this is fine