Django’s coding style requirements have two different maximum line lengths for .py files, one for code lines and another for """docstrings"""
and # comment lines
:
We allow up to 88 characters as this is the line length used by
black
. This check is included when you runflake8
. Documentation, comments, and docstrings should be wrapped at 79 characters, even though PEP 8 suggests 72.
Only the 88 char limit is enforced by Django’s flake8 config—including pre-commit hooks and the CI linters job. Currently, the smaller 79 char limit is (sometimes) manually enforced in the PR review process. This seems to me a poor use of both reviewers’ and contributors’ time and efforts.
I created ticket-36500 and PR #19627, thinking this could be quickly solved by adding max-doc-length = 79
to the .flake8 config file. Sadly, it’s not so simple. There are well over 1300 cases of existing code that exceeds that limit, across 380 files (roughly 14% of the .py files in the codebase). Thus, this post.
Some options:
- Use black’s 88-char limit for all code. Remove the separate 79-char limit on docstrings/comments from the coding standards. (All existing code is then already compliant.)
- The docs say “should” not “must.” Don’t treat 79 chars as a hard limit, and ignore it in PR reviews.
- Add
max-doc-length = 79
to the flake8 config, and grandfather in the existing violations usingper-file-ignores
(like this). Clean up existing violations as the code is later edited for other reasons. - Clean up the existing violations first, then change the flake8 config. (I’m not volunteering.)
- Add
args: ["--max-docs-length=79"]
to the pre-commit flake8 config—but not to the project-wide .flake8 config file. This would catch new edits that exceed the limit, in pre-commit only (for contributors who have pre-commit set up). But it wouldn’t check the limit in the CI linters job, and enforcement in PRs would still be manual. [Edit: this is probably not workable. See Jacob Walls’ comment in the ticket.]
My preference would be either option 1 or 3. (Or option 4 if someone else is up for the work.) My feeling is that purely mechanical style requirements should either be enforced automatically, or should be eliminated if they can’t. (After all, that’s why we use tools like flake8 and black.)