Skip to content

Commit

Permalink
Feat: Implement TooManyRaisesViolation rule and config. Issue #14
Browse files Browse the repository at this point in the history
  • Loading branch information
fils2 committed Feb 24, 2020
1 parent 2c634e1 commit 6524c7d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
9 changes: 9 additions & 0 deletions wemake_python_styleguide/options/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@
- ``max-attributes`` - maximum number of public instance attributes,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_ATTRIBUTES`
- ``max-raises`` - maximum number of raises in a function,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_RAISES`
- ``max-cognitive-score`` - maximum amount of cognitive complexity
per function, defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_COGNITIVE_SCORE`
Expand Down Expand Up @@ -360,6 +363,12 @@ class Configuration(object):
'Maximum number of public instance attributes.',
),

_Option(
'--max-raises',
defaults.MAX_RAISES,
'Maximum number of raises in a function.',
),

_Option(
'--max-cognitive-score',
defaults.MAX_COGNITIVE_SCORE,
Expand Down
3 changes: 3 additions & 0 deletions wemake_python_styleguide/options/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
#: Maximum number of public attributes in a single class.
MAX_ATTRIBUTES: Final = 6 # guessed

#: Maximum number of raises in a function.
MAX_RAISES: Final = 3 # guessed

#: Maximum amount of cognitive complexity per function.
MAX_COGNITIVE_SCORE: Final = 12 # based on this code statistics

Expand Down
1 change: 1 addition & 0 deletions wemake_python_styleguide/options/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class _ValidatedOptions(object):
max_asserts: int = attr.ib(validator=[_min_max(min=1)])
max_access_level: int = attr.ib(validator=[_min_max(min=1)])
max_attributes: int = attr.ib(validator=[_min_max(min=1)])
max_raises: int = attr.ib(validator=[_min_max(min=1)])
max_cognitive_score: int = attr.ib(validator=[_min_max(min=1)])
max_cognitive_average: int = attr.ib(validator=[_min_max(min=1)])
max_call_level: int = attr.ib(validator=[_min_max(min=1)])
Expand Down
4 changes: 4 additions & 0 deletions wemake_python_styleguide/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ def max_access_level(self) -> int:
def max_attributes(self) -> int:
...

@property
def max_raises(self) -> int:
...

@property
def max_cognitive_score(self) -> int:
...
Expand Down
8 changes: 5 additions & 3 deletions wemake_python_styleguide/violations/complexity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,14 +1110,16 @@ class TooManyRaisesViolation(ASTViolation):
Solution here.
Configuration:
Configuration here.
This rule is configurable with ``--max-raises``.
Default:
:str:`wemake_python_styleguide.options.defaults.MAX_RAISES`
See also:
Links here.
.. versionadded:: 0.14.0
.. versionadded:: 0.15.0
"""

error_template = 'Error template here'
error_template = 'Found too many raises in a function: {0}'
code = 235
9 changes: 9 additions & 0 deletions wemake_python_styleguide/visitors/ast/complexity/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
TooManyExpressionsViolation,
TooManyLocalsViolation,
TooManyReturnsViolation,
TooManyRaisesViolation,
)
from wemake_python_styleguide.visitors.base import BaseNodeVisitor
from wemake_python_styleguide.visitors.decorators import alias
Expand Down Expand Up @@ -53,6 +54,7 @@ def __init__(self) -> None:
self.asserts: _FunctionCounter = defaultdict(int)
self.returns: _FunctionCounter = defaultdict(int)
self.expressions: _FunctionCounter = defaultdict(int)
self.raises: _FunctionCounter = defaultdict(int)
self.variables: DefaultDict[AnyFunctionDef, List[str]] = defaultdict(
list,
)
Expand Down Expand Up @@ -106,6 +108,7 @@ def _check_sub_node(
ast.Expr: self.expressions,
ast.Await: self.awaits,
ast.Assert: self.asserts,
ast.Raise: self.raises,
}

for types, counter in error_counters.items():
Expand Down Expand Up @@ -146,6 +149,7 @@ def visit_any_function(self, node: AnyFunctionDef) -> None:
TooManyLocalsViolation
TooManyArgumentsViolation
TooManyAwaitsViolation
TooManyRaisesViolation
"""
self._counter.check_arguments_count(node)
Expand Down Expand Up @@ -214,6 +218,11 @@ def _function_checks(self) -> List[_CheckRule]:
self.options.max_asserts,
TooManyAssertsViolation,
),
(
self._counter.raises,
self.options.max_raises,
TooManyRaisesViolation,
),
]

def _post_visit(self) -> None:
Expand Down

0 comments on commit 6524c7d

Please sign in to comment.