RobertCraigie / pyright-python

Python command line wrapper for pyright, a static type checker
https://pypi.org/project/pyright/
MIT License
164 stars 22 forks source link

Add a less verbose second hook #269

Open randolf-scholz opened 4 months ago

randolf-scholz commented 4 months ago

pyright's output is quite verbose for a pre-commit hook. We can combine it with grep to give much less verbose output:

hooks:
  - id: pyright
    entry: bash -c '! pyright "$@" | grep -Po "(?<=$PWD/)(.*:.*)"' --

Does it make sense to add this as a second hook, maybe "pyright-minimal"?

Of course, a caveat here is that it is less robust and could silently fail if $PWD contains funny special characters.

randolf-scholz commented 4 months ago

A version that preserves the coloring of pyright, based on https://stackoverflow.com/a/20401674:

entry: bash -c '! script -fqc "$(printf "%q " pyright "$@")" /dev/null | grep --color=never -Po "(?<=$PWD/)(.*:.*)"' --

EDIT: This should also work

entry: sh -c '! script -c "pyright $*" /dev/null | grep --color=never -Po "(?<=$PWD/)(.*:.*)"' --
randolf-scholz commented 1 month ago

There seems to be no interest in such a feature, therefore closing. People who want more concise output can use a config like:

  - repo: https://github.com/RobertCraigie/pyright-python
    rev: <version>
    hooks:
      - id: pyright
        entry: |
          sh -c '
              result=$(script -c "pyright $*" /dev/null | grep --color=never -Po "(?<=$PWD/)(.*:.*)");
              count=$(echo -n "$result" | grep -c "^");
              if [ "$count" -ne 0 ]; then
                  echo "$result";
                  echo "\033[1;31mFound ${count} errors\033[0m";
                  exit 1;
              fi
          ' --
RobertCraigie commented 1 month ago

Sorry for the lack of response @randolf-scholz, to be clear you'd want the ouptut to just include the number of errors / warnings, no additional information?

randolf-scholz commented 4 weeks ago

pyright often gives detailed information that can be really useful, but feels too verbose for a pre-commit hook for my taste. The script above limits the output to one line per error message, and includes the total count of violations.