-
-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes: 1. Now checking aliases' names inside imports, closes #65 2. Adds `generator_stop` to the whitelist of future imports, closes #64 3. Adds `CONTRIBUTING.md`, closes #62 4. Refactors how errors are yielded, closes #61 5. Fixes `wemake.services` badge, closes #59 6. Restricts dotted raw imports, closes #56
- Loading branch information
Showing
12 changed files
with
304 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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,69 @@ | ||
# How to contribute | ||
|
||
If you want to start working on this project, | ||
you will need to get familiar with these APIs: | ||
|
||
- [Writing a `flake8` plugin](http://flake8.pycqa.org/en/latest/plugin-development/) | ||
- [Using `ast` module](https://docs.python.org/3/library/ast.html) | ||
|
||
It is also recommended to take a look at these resources: | ||
|
||
- [Missing `ast` guide](https://greentreesnakes.readthedocs.io/en/latest/) | ||
|
||
|
||
## Dependencies | ||
|
||
We use [`poetry`](https://github.com/sdispater/poetry) to manage the dependencies. | ||
|
||
To install them you would need to run two commands: | ||
|
||
```bash | ||
poetry install | ||
poetry develop | ||
``` | ||
|
||
To activate your `virtualenv` run `poetry shell`. | ||
|
||
|
||
## Tests | ||
|
||
We use `pytest` and `flake8` for quality control. | ||
To run all tests: | ||
|
||
```bash | ||
pytest | ||
``` | ||
|
||
This step is mandatory during the CI. | ||
|
||
|
||
## Type checks | ||
|
||
We use `mypy` to run type checks on our code. | ||
To use it: | ||
|
||
```bash | ||
mypy wemake_python_styleguide | ||
``` | ||
|
||
This step is mandatory during the CI. | ||
|
||
|
||
## Before submitting | ||
|
||
Before submitting your code please do the following steps: | ||
|
||
1. Run `pytest` to make sure everything was working before | ||
2. Add any changes you want | ||
3. Adds tests for the new changes | ||
4. Edit documentation if you have changed something significant | ||
5. Run `pytest` again to make sure it is still working | ||
6. Run `mypy` to ensure that types are correct | ||
|
||
|
||
## Other help | ||
|
||
You can contribute by spreading a word about this library. | ||
It would also be a huge contribution to write | ||
a short article on how you are using this project. | ||
What are your best-practices? |
This file contains 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 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
79 changes: 79 additions & 0 deletions
79
tests/test_visitors/test_wrong_import/test_dotted_raw_import.py
This file contains 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,79 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.visitors.wrong_import import ( | ||
DottedRawImportViolation, | ||
WrongImportVisitor, | ||
) | ||
|
||
regular_import = """ | ||
import {0} | ||
""" | ||
|
||
regular_import_with_alias = """ | ||
import {0} as alias | ||
""" | ||
|
||
from_import = """ | ||
from {0} import some | ||
""" | ||
|
||
from_import_with_alias = """ | ||
from {0} import some as alias | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
regular_import, | ||
regular_import_with_alias, | ||
]) | ||
@pytest.mark.parametrize('to_import', [ | ||
'dotted.path' | ||
'nested.dotted.path', | ||
]) | ||
def test_wrong_dotted_import(assert_errors, parse_ast_tree, code, to_import): | ||
"""Testing that dotted raw imports are restricted.""" | ||
tree = parse_ast_tree(code.format(to_import)) | ||
|
||
visiter = WrongImportVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, [DottedRawImportViolation]) | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
regular_import, | ||
regular_import_with_alias, | ||
]) | ||
@pytest.mark.parametrize('to_import', [ | ||
'os', | ||
'sys', | ||
]) | ||
def test_correct_flat_import(assert_errors, parse_ast_tree, code, to_import): | ||
"""Testing that flat raw imports are allowed.""" | ||
tree = parse_ast_tree(code.format(to_import)) | ||
|
||
visiter = WrongImportVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, []) | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
from_import, | ||
from_import_with_alias, | ||
]) | ||
@pytest.mark.parametrize('to_import', [ | ||
'regular', | ||
'dotted.path' | ||
'nested.dotted.path', | ||
]) | ||
def test_regular_from_import(assert_errors, parse_ast_tree, code, to_import): | ||
"""Testing that dotted `from` imports are allowed.""" | ||
tree = parse_ast_tree(code.format(to_import)) | ||
|
||
visiter = WrongImportVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, []) |
This file contains 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 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,93 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import string | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.visitors.wrong_name import ( | ||
BAD_VARIABLE_NAMES, | ||
PrivateNameViolation, | ||
TooShortVariableNameViolation, | ||
WrongNameVisitor, | ||
WrongVariableNameViolation, | ||
) | ||
|
||
import_alias = """ | ||
import os as {0} | ||
""" | ||
|
||
from_import_alias = """ | ||
from os import path as {0} | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize('bad_name', BAD_VARIABLE_NAMES) | ||
@pytest.mark.parametrize('code', [ | ||
import_alias, | ||
from_import_alias, | ||
]) | ||
def test_wrong_import_alias_names( | ||
assert_errors, parse_ast_tree, bad_name, code, | ||
): | ||
"""Testing that import aliases can not have blacklisted names.""" | ||
tree = parse_ast_tree(code.format(bad_name)) | ||
|
||
visiter = WrongNameVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, [WrongVariableNameViolation]) | ||
|
||
|
||
@pytest.mark.parametrize('short_name', string.ascii_letters) | ||
@pytest.mark.parametrize('code', [ | ||
import_alias, | ||
from_import_alias, | ||
]) | ||
def test_too_short_import_alias_names( | ||
assert_errors, parse_ast_tree, short_name, code, | ||
): | ||
"""Testing that import aliases can not have too short names.""" | ||
tree = parse_ast_tree(code.format(short_name)) | ||
|
||
visiter = WrongNameVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, [TooShortVariableNameViolation]) | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
import_alias, | ||
from_import_alias, | ||
]) | ||
def test_private_import_alias_names( | ||
assert_errors, parse_ast_tree, code, | ||
): | ||
"""Testing that import aliases can not have too private names.""" | ||
tree = parse_ast_tree(code.format('__hidden')) | ||
|
||
visiter = WrongNameVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, [PrivateNameViolation]) | ||
|
||
|
||
@pytest.mark.parametrize('correct_name', [ | ||
'my_alias', | ||
'xy', | ||
'test', | ||
'_protected', | ||
]) | ||
@pytest.mark.parametrize('code', [ | ||
import_alias, | ||
from_import_alias, | ||
]) | ||
def test_correct_import_alias_names( | ||
assert_errors, parse_ast_tree, correct_name, code, | ||
): | ||
"""Testing that import aliases can have normal names.""" | ||
tree = parse_ast_tree(code.format(correct_name)) | ||
|
||
visiter = WrongNameVisitor() | ||
visiter.visit(tree) | ||
|
||
assert_errors(visiter, []) |
This file contains 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 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 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 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.