Repository: pandas-dev/pandas
Pull request: STYLE: moving spacing conventions in cython casting to pre-commit
- [X] closes #42121 
- [ ] 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


Problem statement:
STYLE move spacing conventions in cython casting to pre-commit
here's the check which needs moving, it's in `ci/code_checks.sh`
```
### LINTING ###
if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then

    # Check that cython casting is of the form `<type>obj` as opposed to `<type> obj`;
    # it doesn't make a difference, but we want to be internally consistent.
    # Note: this grep pattern is (intended to be) equivalent to the python
    # regex r'(?<![ ->])> '
    MSG='Linting .pyx code for spacing conventions in casting' ; echo $MSG
    invgrep -r -E --include '*.pyx' --include '*.pxi.in' '[a-zA-Z0-9*]> ' pandas/_libs
    RET=$(($RET + $?)) ; echo $MSG "DONE"

    # readability/casting: Warnings about C casting instead of C++ casting
    # runtime/int: Warnings about using C number types instead of C++ ones
    # build/include_subdir: Warnings about prefacing included header files with directory

fi
```

It could be a `pygrep` check

Hints from issue discussion:
This could be added to the `unwanted-patterns` hook
take

Commit message: ENH: Add new hook for cython casting

Patch to review:
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index d580fcf4fc545..ffbe5154d84ed 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -103,6 +103,13 @@ repos:
             |\.\.\ code-block\ ::
             |\.\.\ ipython\ ::
         types_or: [python, cython, rst]
+    -   id: cython-casting
+        name: Cython Casting
+        language: pygrep
+        entry: |
+            # Cython casting is of the form `<type>obj` as opposed to `<type> obj`
+            |[a-zA-Z0-9*]>\s
+        types: [cython]
     -   id: pip-to-conda
         name: Generate pip dependency from conda
         description: This hook checks if the conda environment.yml and requirements-dev.txt are equal
diff --git a/ci/code_checks.sh b/ci/code_checks.sh
index 59548ecd3c710..e502d50040237 100755
--- a/ci/code_checks.sh
+++ b/ci/code_checks.sh
@@ -41,23 +41,6 @@ if [[ "$GITHUB_ACTIONS" == "true" ]]; then
     INVGREP_PREPEND="##[error]"
 fi
 
-### LINTING ###
-if [[ -z "$CHECK" || "$CHECK" == "lint" ]]; then
-
-    # Check that cython casting is of the form `<type>obj` as opposed to `<type> obj`;
-    # it doesn't make a difference, but we want to be internally consistent.
-    # Note: this grep pattern is (intended to be) equivalent to the python
-    # regex r'(?<![ ->])> '
-    MSG='Linting .pyx code for spacing conventions in casting' ; echo $MSG
-    invgrep -r -E --include '*.pyx' --include '*.pxi.in' '[a-zA-Z0-9*]> ' pandas/_libs
-    RET=$(($RET + $?)) ; echo $MSG "DONE"
-
-    # readability/casting: Warnings about C casting instead of C++ casting
-    # runtime/int: Warnings about using C number types instead of C++ ones
-    # build/include_subdir: Warnings about prefacing included header files with directory
-
-fi
-
 ### PATTERNS ###
 if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
 


Code review comments:
diff_hunk: @@ -103,6 +103,13 @@ repos:
             |\.\.\ code-block\ ::
             |\.\.\ ipython\ ::
         types_or: [python, cython, rst]
+    -   id: cython-casting
+        name: Cython Casting
+        language: pygrep
+        entry: |
+            # Cython casting is of the form `<type>obj` as opposed to `<type> obj`
+            |[a-zA-Z0-9*]>\s
+        types: [cython]
path: .pre-commit-config.yaml
text: @user1:
`pxi.in` isn't recognised as `cython` by [identify](https://github.com/pre-commit/identify), does the following work?

```suggestion
        files: (\.pyx|\.pxi.in)$
```

@author:
I tried that, but it shows the following message, don't know if it worked or not as it showed the same message when I ran unwanted patterns with the cython check their. While, it gave error for .rst files when ran with ci-check on PR.

<img width="1030" alt="Screenshot 2021-08-02 at 8 51 19 PM" src="https://user-images.githubusercontent.com/46242526/127885070-14358a8b-6d6f-452b-99a6-3bbac71bdd41.png">



@author:
Should I make a commit anyway?

@user1:
ah, you should run `pre-commit run cython-casting --all-files`, else it'll only run against files you've staged

@author:
Thank you!! I ran that and found that my hook is not working for the previous test.pyx or test.pix.in example, it works when I move the logic to unwanted hooks but it doesn't work with the new hook

@author:
Ok with the quotes it works now

diff_hunk: @@ -103,6 +103,13 @@ repos:
             |\.\.\ code-block\ ::
             |\.\.\ ipython\ ::
         types_or: [python, cython, rst]
+    -   id: cython-casting
+        name: Cython Casting
+        language: pygrep
+        entry: |
+            # Cython casting is of the form `<type>obj` as opposed to `<type> obj`
+            |[a-zA-Z0-9*]>\s
path: .pre-commit-config.yaml
text: @user1:
this can probably just fit on one line: `entry: [a-zA-Z0-9*]>\s`

@author:
I tried that, but I got the following error.

<img width="632" alt="Screenshot 2021-08-02 at 10 00 55 PM" src="https://user-images.githubusercontent.com/46242526/127894291-c9f19d1f-6a3d-43c9-8c69-e6e43ebf6afe.png">


@user1:
ah you probably just need quotes around the string

@author:
Thank you it worked, will update the PR. Also, now I can just use the space at the end as before.