Add WPS484: forbid strings that document nothing - #3810
Merged
Conversation
A string statement only documents something when it opens a module, class or function body, follows a module or class attribute, follows `self.some = ...` in a method, or follows a `type` alias. Anywhere else it does nothing at runtime while still reading like documentation. The check reuses `is_doc_string_value()`, the predicate WPS226 already uses to skip docstrings, so both rules share one definition of a documenting position instead of keeping two that can drift apart. Closes wemake-services#3808
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3810 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 374 375 +1
Lines 12708 12763 +55
Branches 879 880 +1
=========================================
+ Hits 12708 12763 +55 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
sobolevn
reviewed
Sep 12, 2026
sobolevn
left a comment
Member
There was a problem hiding this comment.
The first correction:
after
self.some = ...inside a constructor
Not in every other method
:)
`self.some = 1` in a regular method assigns to an instance that already exists, so a string after it documents nothing. Only `__init__` defines the attributes, which is what PEP 258 and Sphinx read. This narrows the shared predicate, so it moves `WPS226` as well: the same string after `self.some = 1` outside a constructor is counted again. That is the point of the two rules sharing one definition. Refs wemake-services#3808
sobolevn
reviewed
Sep 12, 2026
Co-authored-by: sobolevn <mail@sobolevn.me>
Four review points plus the build: - The applied suggestion dropped the escaping on one example but kept `"""`, which closed the class docstring early and broke the parse. Both examples now use `'''`, which is what the suggestion asked for. - `__new__` defines attributes too, so both constructors are accepted. - The placement tests now run over `"Doc"`, `'Doc'`, `"""Doc"""` and `'''Doc'''`, and cover a `__new__` constructor. - Master released 1.8.1 while this branch was open, so the merge swept the entry into a released patch section. A new violation cannot ship in a patch anyway, so it starts a fresh WIP section. Refs wemake-services#3808
sobolevn
reviewed
Sep 12, 2026
sobolevn
left a comment
Member
There was a problem hiding this comment.
Thanks!
Last fixes:
- please test dataclass-style attrs explicitly, with and without default values
- there's a common error that people use
"""Docstring"""before the attribute, not after. we should also explicitly test and report this case
Both already behaved correctly, so this is coverage plus docs, no change to the check itself. Dataclass fields are plain annotated class attributes, with or without a default, so they were already allowed. Now they say so explicitly. A string placed before an attribute is already reported, since the statement above it is not an assignment. Three shapes are covered: at module level, in a class body, and between two attributes. The example in the violation now shows this, as it is the common way to get it wrong. Refs wemake-services#3808
Contributor
Author
|
Added tests for dataclass-style attrs and for attribute docstring placement. Both cases already behaved correctly, so no code change was needed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I have made things!
Adds
WPS484, the rule from #3808. It flags a string statement placed where it cannot document anything:A string documents something in exactly four places:
self.some = ...inside a methodtype Some = ...alias onpython3.12+The check itself is a single condition, because
WPS226already had to answer this same question in order to skip docstrings:That reuse is the point of the rule, not a shortcut. Both rules now read one definition of a documenting position, so refining it later moves them together instead of letting two copies drift.
The violation sits in
best_practices.pynext toWPS428, the old "statement has no effect" rule that was retired to ruff'sB015/B018in 1.0.0. Neither of those flags a string, which is how these slip through today.wemake-python-styleguideitself is clean under the new rule.Open to review
I picked these three without asking, so say if any is wrong:
WrongDocStringPlacementViolationWPS484type Some = intdocstrings as a valid position, given they're still only a proposal upstreamAI Policy
Checklist
CHANGELOG.mdRelated issues
Closes #3808
🙏 Please, if you or your company is finding wemake-python-styleguide valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/wemake-python-styleguide. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.