pydocstyle icon indicating copy to clipboard operation
pydocstyle copied to clipboard

The pre-commit hook fails silently when a pyproject.toml file exists and toml isn't installed

Open vyasr opened this issue 3 years ago • 3 comments

#534 enabled usage of pydocstyle with pyproject.toml for configuration. In that PR, it was discussed that toml shouldn't be a hard dependency for using pydocstyle (which I agree with), and that it should simply throw a warning if a pyproject.toml file is detected without the toml package present to read it. However, this is problematic when using pydocstyle as a pre-commit hook because pre-commit will silently consume stdout unless the hook returns a nonzero exit code.

I see a few potential solutions here, which I've placed in order of preference:

  1. Use the additional_dependencies feature of pre-commit to always require toml when installing the pydocstyle pre-commit hook. I see the argument for not wanting to error without toml in a general setting, but in the context of pre-commit where the dependencies can be tightly managed I think it's OK to have this dependency encoded.
  2. Change the warning to an error, but also add a grep (or equivalent) of a pyproject.toml file (if one exists) to see whether the string [tool.pydocstyle] is present in the file as a precondition of erroring. This is a bit more engineering effort, but if avoiding requiring toml is important enough even in a pre-commit hook then this would be a way to do a more cursory check of the file without parsing it.
  3. Set verbose: True in the pre-commit hook definition. This choice has the downside of always making pydocstyle noisier, but it at least ensures that the warning will be visible.
  4. The status quo. This solution puts the onus on the user to realize that pydocstyle fails silently in this case. I personally don't think this is a viable solution, but I listed it in case the project maintainers feel otherwise.

I am happy to make a PR if we could come to a consensus on the preferred solution.

vyasr avatar Aug 02 '22 22:08 vyasr

For completeness, here's a MWE:

# pyproject.toml
[tool.pydocstyle]
select = D30
# .pre-commit-config.yaml
repos:
  - repo: https://github.com/PyCQA/pydocstyle
    rev: 6.1.1
    hooks:
      - id: pydocstyle
# test.py
"""Docstring."""                                                                
                                                                                
                                                                                
def myfunc():                                                                   
    """docstring."""                                                            
    pass   

And here's the output:

(main) vyasr-dt% pre-commit run --all-files
pydocstyle...............................................................Passed
(main) vyasr-dt% pre-commit run --all-files -v
pydocstyle...............................................................Passed
- hook id: pydocstyle
- duration: 0.05s

WARNING: The /home/vyasr/local/testing/pydocstyle_hook/pyproject.toml configuration file was ignored, because the `toml` package is not installed.

vyasr avatar Aug 02 '22 22:08 vyasr

:+1: Had the same issue, thanks for opening this issue because it helped!

choucavalier avatar Sep 20 '22 09:09 choucavalier

With pydocstyle 6.3.0 package tomli also required (version 6.2.0 works without adding this package as a hook dependency), otherwise pyproject.toml configuration will be ignored.

ivysochyn avatar Aug 18 '23 12:08 ivysochyn