-
-
Notifications
You must be signed in to change notification settings - Fork 436
Add WPS484: forbid strings that document nothing #3810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
203145f
Add WPS484: forbid strings that document nothing
vyhuholl 788009b
Merge branch 'master' into issue-3808
vyhuholl 4ea8b94
Only a constructor defines instance attributes to document
vyhuholl debf226
Update wemake_python_styleguide/violations/best_practices.py
vyhuholl b05da1f
Fix docstring syntax, support __new__, move the changelog entry
vyhuholl 663120a
Test dataclass attributes and docstrings placed before them
vyhuholl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
239 changes: 239 additions & 0 deletions
239
tests/test_visitors/test_ast/test_statements/test_doc_string_placement.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import pytest | ||
|
|
||
| from wemake_python_styleguide.compat.constants import PY312 | ||
| from wemake_python_styleguide.violations.best_practices import ( | ||
| WrongDocStringPlacementViolation, | ||
| ) | ||
| from wemake_python_styleguide.visitors.ast.statements import ( | ||
| DocStringPlacementVisitor, | ||
| ) | ||
|
|
||
| module_docstring = '{0}' | ||
|
|
||
| function_docstring = """ | ||
| def some(): | ||
| {0} | ||
| """ | ||
|
|
||
| class_docstring = """ | ||
| class Some: | ||
| {0} | ||
| """ | ||
|
|
||
| module_attribute = """ | ||
| first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| annotated_module_attribute = """ | ||
| first: int = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| class_attribute = """ | ||
| class Some: | ||
| 'Class docs.' | ||
|
|
||
| first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| dataclass_attribute = """ | ||
| @dataclass | ||
| class Some: | ||
| 'Class docs.' | ||
|
|
||
| first: int | ||
| {0} | ||
| """ | ||
|
|
||
| dataclass_attribute_with_default = """ | ||
| @dataclass | ||
| class Some: | ||
| 'Class docs.' | ||
|
|
||
| first: int = 0 | ||
| {0} | ||
| """ | ||
|
|
||
| instance_attribute = """ | ||
| class Some: | ||
| def __init__(self): | ||
| 'Method docs.' | ||
| self.first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| instance_attribute_in_new = """ | ||
| class Some: | ||
| def __new__(cls): | ||
| 'Method docs.' | ||
| cls.first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| type_alias = pytest.param( | ||
| """ | ||
| type Some = int | ||
| {0} | ||
| """, | ||
| marks=pytest.mark.skipif( | ||
| not PY312, | ||
| reason='`type` aliases are only in Python 3.12+', | ||
| ), | ||
| ) | ||
|
|
||
| foreign_attribute = """ | ||
| some.first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| # A docstring goes after the attribute it documents, never before it. | ||
| before_module_attribute = """ | ||
| 'Module docs.' | ||
|
|
||
| {0} | ||
| first = 1 | ||
| """ | ||
|
|
||
| before_class_attribute = """ | ||
| class Some: | ||
| 'Class docs.' | ||
|
|
||
| {0} | ||
| first: int | ||
| """ | ||
|
|
||
| between_attributes = """ | ||
| class Some: | ||
| 'Class docs.' | ||
|
|
||
| first = 1 | ||
| 'Documents first.' | ||
|
|
||
| {0} | ||
| second = 2 | ||
| """ | ||
|
|
||
| instance_attribute_outside_constructor = """ | ||
| class Some: | ||
| def method(self): | ||
| 'Method docs.' | ||
| self.first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| local_variable = """ | ||
| def some(): | ||
| 'Function docs.' | ||
| first = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| multiple_targets = """ | ||
| first = second = 1 | ||
| {0} | ||
| """ | ||
|
|
||
| inside_condition = """ | ||
| def some(arg): | ||
| 'Function docs.' | ||
| if arg: | ||
| {0} | ||
| return 1 | ||
| return 0 | ||
| """ | ||
|
|
||
| after_call = """ | ||
| def some(): | ||
| 'Function docs.' | ||
| print(1) | ||
| {0} | ||
| """ | ||
|
|
||
| after_doc_string = """ | ||
| def some(): | ||
| 'Function docs.' | ||
| {0} | ||
| """ | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'code', | ||
| [ | ||
| module_docstring, | ||
| function_docstring, | ||
| class_docstring, | ||
| module_attribute, | ||
| annotated_module_attribute, | ||
| class_attribute, | ||
| dataclass_attribute, | ||
| dataclass_attribute_with_default, | ||
| instance_attribute, | ||
| instance_attribute_in_new, | ||
| type_alias, | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| 'string_value', | ||
| [ | ||
| '"Doc"', | ||
| "'Doc'", | ||
| '"""Doc"""', | ||
| "'''Doc'''", | ||
| ], | ||
| ) | ||
| def test_documenting_string( | ||
| assert_errors, | ||
| parse_ast_tree, | ||
| default_options, | ||
| code, | ||
| string_value, | ||
| ): | ||
| """Testing that strings documenting something are allowed.""" | ||
| tree = parse_ast_tree(code.format(string_value)) | ||
|
|
||
| visitor = DocStringPlacementVisitor(default_options, tree=tree) | ||
| visitor.run() | ||
|
|
||
| assert_errors(visitor, []) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| 'code', | ||
| [ | ||
| foreign_attribute, | ||
| before_module_attribute, | ||
| before_class_attribute, | ||
| between_attributes, | ||
| instance_attribute_outside_constructor, | ||
| local_variable, | ||
| multiple_targets, | ||
| inside_condition, | ||
| after_call, | ||
| after_doc_string, | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| 'string_value', | ||
| [ | ||
| '"Doc"', | ||
| "'Doc'", | ||
| '"""Doc"""', | ||
| "'''Doc'''", | ||
| ], | ||
| ) | ||
| def test_string_documenting_nothing( | ||
| assert_errors, | ||
| parse_ast_tree, | ||
| default_options, | ||
| code, | ||
| string_value, | ||
| ): | ||
| """Testing that strings documenting nothing are forbidden.""" | ||
| tree = parse_ast_tree(code.format(string_value)) | ||
|
|
||
| visitor = DocStringPlacementVisitor(default_options, tree=tree) | ||
| visitor.run() | ||
|
|
||
| assert_errors(visitor, [WrongDocStringPlacementViolation]) |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.