The pre-commit hook fails silently when a pyproject.toml file exists and toml isn't installed
#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:
- Use the
additional_dependenciesfeature ofpre-committo always requiretomlwhen installing thepydocstylepre-commithook. I see the argument for not wanting to error withouttomlin a general setting, but in the context ofpre-commitwhere the dependencies can be tightly managed I think it's OK to have this dependency encoded. - Change the warning to an error, but also add a grep (or equivalent) of a
pyproject.tomlfile (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 requiringtomlis important enough even in apre-commithook then this would be a way to do a more cursory check of the file without parsing it. - Set
verbose: Truein thepre-commithook definition. This choice has the downside of always makingpydocstylenoisier, but it at least ensures that the warning will be visible. - The status quo. This solution puts the onus on the user to realize that
pydocstylefails 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.
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.
:+1: Had the same issue, thanks for opening this issue because it helped!
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.