pytest-pep8
pytest-pep8 is a pytest plugin designed to check PEP8 compliance in Python code. It integrates directly with the pytest testing framework to efficiently discover and check `.py` files for style guide violations. The plugin allows for flexible configuration to ignore specific errors or warnings on a per-project or per-file basis. Its last release was in April 2014, and it is largely unmaintained now.
Common errors
-
PytestDeprecationWarning: Direct construction of Pep8Item has been deprecated, please use Pep8Item.from_parent.
cause This error occurs because `pytest-pep8` uses an older internal API for constructing test items that has been deprecated and removed in recent versions of `pytest` (>= 6.0.1).fixThe `pytest-pep8` plugin is abandoned. You should migrate to a modern, actively maintained linter plugin such as `pytest-flake8` or `pytest-pycodestyle`. -
Running `pytest --pep8` reports an overwhelming number of PEP8 errors on my existing Python project.
cause Most older or unlinted Python projects will have numerous violations of the PEP8 style guide, and `pytest-pep8` checks all discovered `.py` files by default.fixStart by adding `pep8ignore = * ALL` under the `[pytest]` section in your `pytest.ini` or `setup.cfg` file to ignore all errors. Then, gradually remove `ALL` or specify individual error codes to ignore (e.g., `pep8ignore = E201 E231`) as you refactor your code. -
PEP8 checks are not updating or re-running after I modify and save a Python file.
cause The `pytest-pep8` plugin implements caching to speed up repeated runs. This cache might prevent checks from re-running on files it believes haven't changed.fixTo explicitly clear the cache and force `pytest-pep8` to re-evaluate all files, run `pytest` with the `--pep8` and `--clearcache` command-line options: `pytest --pep8 --clearcache`.
Warnings
- breaking The `pytest-pep8` plugin is incompatible with `pytest` versions 6.0.1 and newer due to significant internal API changes within pytest, specifically regarding node construction.
- deprecated The `pytest-pep8` project is largely unmaintained since its last release in April 2014. The underlying 'pep8' library it relies on has also been renamed to `pycodestyle`.
- gotcha Running `pytest --pep8` on an existing codebase may initially report a large number of PEP8 violations, potentially overwhelming.
- gotcha The plugin uses caching to improve performance, which can sometimes lead to stale results where changes in files are not immediately reflected in subsequent PEP8 checks.
Install
-
pip install pytest-pep8
Quickstart
# content of myproject/myfile.py
def somefunc( arg1, arg2):
pass
# Terminal
# Install the plugin
# pip install pytest-pep8
# Run pytest with the --pep8 flag
# pytest --pep8