-
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feat: adds stub of the new violation TooManyRaisesViolation Test: Adds tests for TooManyRaisesViolation which for now are disabled Test: * Adds false positivity test with 3 raises in method * Adds false positivity test with 4 raises in module * Fixes wrong number of raises in tests * Fixes string literals in tests to be more pythonic Fix: fixes test for python 3.8 Docs: Document TooManyRaisesViolation rule. Issue #16 (#20) Merge branches Issue #14 (#21) * Feat: Implement TooManyRaisesViolation rule and config. Issue #14 * Fix: A priori fix for I001 and WPS230 lint failures. Issue #14 * Fix: A priori fix for AssertionError related to tests. Issue #14 * Fix: Modify test_public_attrs_count tests to ignore class _ComplexityCounter. Issue #14 * Fix: Sort imports in test_public_attrs_count. Issue #14 * Fix: Try to avoid lint failures in test_public_attrs_count. Issue #14 * Fix: Undo a few commits and restore defaults. Issue #14 * Refactor: Create class _ComplexityExitMetrics(object). Issue #14 * Fix: (Temporarily) remove decorator @DataClass from class _ComplexityExitMetrics(object). Issue #14 * Fix: Added _ at the beginning of a public attribute in class _ComplexityCounter(object). Issue #14 * Fix: Added = to define a public attribute in class _ComplexityCounter(object). Issue #14 * Fix: Fix various lint errors. Issue #14 * Docs: Changed docs for class _ComplexityExitMetrics(object). Issue #14 * Test: Added Gwin73's fixes for his tests. Issue #14 * Fix: fixes incorrect annotation for test * Fix for python 3.8 Co-authored-by: Adrian Westerberg <adrianwesterberg@hotmail.com> Test: Removes integration tests and add unit tests which right now fail Fix: Fixed a few lint errrors in test_raises_count.py. Issue #15 Issue #15 (#23) * Test: Created new tests for issue 3. Issue #15 * Test: Modified WPS235 in test_noqa.py. Issue #15 Co-authored-by: iZafiro <60047972+iZafiro@users.noreply.github.com> Docs: Added WPS235 to CHANGELOG.md. Issue #1160 Docs: Changed weird comment in test_raises.py. Issue #1160 Doc: Fixes doc Test: Simplifies integration test Test: Adds more tests Fix: Changes code of violation to make merge easier Refac: changes imports and formatting after rebasing. Refact issue #1160 (#26) * Refactor issue #1160 (#25) * Refactor: Refactored function.py to include a @DataClass. * Fix: Added missing comma to function.py. * Fix: Tried to fix syntax errors. * Fix: Tried to fix syntax errors. * Fix: Tried to fix syntax errors. * Fix: Tried to fix syntax errors. * Fix: Tried to fix syntax errors. * Fix: Tried to fix syntax errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix lint errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Tried to fix test errors. * Fix: Changed wrong frozen=False tag. * Fix: Changed default to factory. * Fix: Fixed lint errors. * Fix: Added defaultDict variable to dataclass. * Fix: Lint fix. * Refactor: Finish refactoring dataclass. * Fix: Fixed lint errors. * Fix: Fixed lint errors. * Fix: Fixed test errors. * Fixes merge * Fixes merge * Fixes merge Co-authored-by: Gwin73 <adrianwesterberg@hotmail.com> Co-authored-by: sobolevn <mail@sobolevn.me>
- Loading branch information
1 parent
2afc9a7
commit aedf714
Showing
15 changed files
with
295 additions
and
50 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
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 |
---|---|---|
|
@@ -108,6 +108,7 @@ | |
'WPS235': 1, | ||
'WPS236': 1, | ||
'WPS237': 1, | ||
'WPS238': 1, | ||
|
||
'WPS300': 1, | ||
'WPS301': 1, | ||
|
4 changes: 3 additions & 1 deletion
4
tests/test_visitors/test_ast/test_complexity/test_function/test_arguments_lambda.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
4 changes: 3 additions & 1 deletion
4
tests/test_visitors/test_ast/test_complexity/test_function/test_awaits_count.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
4 changes: 3 additions & 1 deletion
4
tests/test_visitors/test_ast/test_complexity/test_function/test_expressions.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
137 changes: 137 additions & 0 deletions
137
tests/test_visitors/test_ast/test_complexity/test_function/test_raises.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,137 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import pytest | ||
|
||
from wemake_python_styleguide.violations.complexity import ( | ||
TooManyRaisesViolation, | ||
) | ||
from wemake_python_styleguide.visitors.ast.complexity.function import ( | ||
FunctionComplexityVisitor, | ||
) | ||
|
||
module_many_raises = """ | ||
if some: | ||
raise SomeException | ||
raise SomeOtherException | ||
""" | ||
|
||
lambda_many_raises = """ | ||
lambda: SomeException if some else SomeOtherException | ||
""" | ||
|
||
function_template = """ | ||
def function(parameter): | ||
{0} | ||
{1} | ||
{2} | ||
""" | ||
|
||
instance_method_template = """ | ||
class Test(object): | ||
def method(self, parameter): | ||
{0} | ||
{1} | ||
{2} | ||
""" | ||
|
||
class_method_template = """ | ||
class Test(object): | ||
@classmethod | ||
def method(cls, parameter): | ||
{0} | ||
{1} | ||
{2} | ||
""" | ||
|
||
static_method_template = """ | ||
class Test(object): | ||
@staticmethod | ||
def method(parameter): | ||
{0} | ||
{1} | ||
{2} | ||
""" | ||
|
||
|
||
@pytest.mark.parametrize('code', [ | ||
module_many_raises, | ||
lambda_many_raises, | ||
]) | ||
def test_asserts_correct_count1( | ||
assert_errors, | ||
parse_ast_tree, | ||
options, | ||
code, | ||
): | ||
"""Testing that raises counted correctly.""" | ||
tree = parse_ast_tree(code) | ||
|
||
option_values = options(max_raises=1) | ||
visitor = FunctionComplexityVisitor(option_values, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) | ||
|
||
|
||
@pytest.mark.parametrize('context', [ | ||
function_template, | ||
instance_method_template, | ||
class_method_template, | ||
static_method_template, | ||
]) | ||
@pytest.mark.parametrize(('first', 'second', 'third'), [ | ||
('...', '', ''), | ||
('if some:', ' raise SomeException', 'raise SomeOtherException'), | ||
('def helper():', ' raise SomeException', 'raise SomeOtherException'), | ||
]) | ||
def test_raises_correct_count2( | ||
assert_errors, | ||
parse_ast_tree, | ||
context, | ||
first, | ||
second, | ||
third, | ||
default_options, | ||
mode, | ||
): | ||
"""Testing that raises are counted correctly.""" | ||
test_instance = context.format(first, second, third) | ||
tree = parse_ast_tree(mode(test_instance)) | ||
|
||
visitor = FunctionComplexityVisitor(default_options, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, []) | ||
|
||
|
||
@pytest.mark.parametrize('context', [ | ||
function_template, | ||
instance_method_template, | ||
class_method_template, | ||
static_method_template, | ||
]) | ||
@pytest.mark.parametrize(('first', 'second', 'third'), [ | ||
('if some:', ' raise SomeException', 'raise SomeOtherException'), | ||
('def helper():', ' raise SomeException', 'raise SomeOtherException'), | ||
]) | ||
def test_raises_wrong_count( | ||
assert_errors, | ||
assert_error_text, | ||
parse_ast_tree, | ||
options, | ||
context, | ||
first, | ||
second, | ||
third, | ||
mode, | ||
): | ||
"""Testing that many raises raises a warning.""" | ||
test_instance = function_template.format(first, second, third) | ||
tree = parse_ast_tree(mode(test_instance)) | ||
|
||
option_values = options(max_raises=1) | ||
visitor = FunctionComplexityVisitor(option_values, tree=tree) | ||
visitor.run() | ||
|
||
assert_errors(visitor, [TooManyRaisesViolation]) | ||
assert_error_text(visitor, '2', option_values.max_raises) |
4 changes: 3 additions & 1 deletion
4
tests/test_visitors/test_ast/test_complexity/test_function/test_returns.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
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,43 @@ | ||
from collections import defaultdict | ||
from typing import DefaultDict, List | ||
|
||
import attr | ||
from typing_extensions import final | ||
|
||
from wemake_python_styleguide.types import ( | ||
AnyFunctionDef, | ||
AnyFunctionDefAndLambda, | ||
) | ||
|
||
#: Function complexity counter. | ||
FunctionCounter = DefaultDict[AnyFunctionDef, int] | ||
|
||
#: Function and lambda complexity counter. | ||
FunctionCounterWithLambda = DefaultDict[AnyFunctionDefAndLambda, int] | ||
|
||
#: Function and their variables. | ||
FunctionNames = DefaultDict[AnyFunctionDef, List[str]] | ||
|
||
|
||
def _default_factory() -> FunctionCounter: | ||
"""We use a lot of defaultdic magic in these metrics.""" | ||
return defaultdict(int) | ||
|
||
|
||
@final | ||
@attr.dataclass(frozen=False) | ||
class ComplexityMetrics(object): | ||
""" | ||
Complexity metrics for functions. | ||
We use it as a store of all metrics we count in a function's body. | ||
There are quite a lot of them! | ||
""" | ||
|
||
returns: FunctionCounter = attr.ib(factory=_default_factory) | ||
raises: FunctionCounter = attr.ib(factory=_default_factory) | ||
awaits: FunctionCounter = attr.ib(factory=_default_factory) | ||
asserts: FunctionCounter = attr.ib(factory=_default_factory) | ||
expressions: FunctionCounter = attr.ib(factory=_default_factory) | ||
arguments: FunctionCounterWithLambda = attr.ib(factory=_default_factory) | ||
variables: FunctionNames = attr.ib(factory=lambda: defaultdict(list)) |
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
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.