Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

wemake-python-styleguide

Package Overview
Dependencies
Maintainers
1
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wemake-python-styleguide - npm Package Compare versions

Comparing version
1.2.0
to
1.3.0
+1
-1
PKG-INFO
Metadata-Version: 2.3
Name: wemake-python-styleguide
Version: 1.2.0
Version: 1.3.0
Summary: The strictest and most opinionated python linter ever

@@ -5,0 +5,0 @@ License: MIT

@@ -7,3 +7,3 @@ [build-system]

name = "wemake-python-styleguide"
version = "1.2.0"
version = "1.3.0"
description = "The strictest and most opinionated python linter ever"

@@ -184,3 +184,4 @@

# Plugin configs:
flake8-import-conventions.banned-from = [ "ast" ]
flake8-import-conventions.banned-from = [ "ast", "datetime" ]
flake8-import-conventions.aliases = { datetime = "dt" }
flake8-quotes.inline-quotes = "single"

@@ -187,0 +188,0 @@ mccabe.max-complexity = 6

@@ -127,2 +127,3 @@ """

'parameters',
'arr',
# Confusables:

@@ -136,2 +137,6 @@ 'no',

'baz',
'spam',
'ham',
'tmp',
'temp',
),

@@ -138,0 +143,0 @@ )

@@ -155,6 +155,7 @@ """

:str:`wemake_python_styleguide.options.defaults.MAX_MATCH_SUBJECTS`
- ``max-match-cases`` - maximum number of cases in a match block of code
- ``max-match-cases`` - maximum number of cases in a match block of code,
defaults to
:str:`wemake_python_styleguide.options.defaults.MAX_MATCH_CASES`
- ``max-lines-in-finally`` - maximum lines in finally block of code
- ``max-lines-in-finally`` - maximum amount of finally block body length.
Lines equal to statements.
defaults to

@@ -441,3 +442,3 @@ :str:`wemake_python_styleguide.options.defaults.MAX_LINES_IN_FINALLY`

defaults.MAX_LINES_IN_FINALLY,
'Maximum lines of expressions in a finally block.',
'Maximum amount of finally block body length.',
),

@@ -444,0 +445,0 @@ # Formatter:

@@ -59,4 +59,4 @@ """

#: Maximum lines of expressions in a `finally` block.
MAX_LINES_IN_FINALLY: Final = 2 # guessed
#: Maximum amount of `finally` block body length.
MAX_LINES_IN_FINALLY: Final = 2 # best practice

@@ -63,0 +63,0 @@ #: Maximum number of `return` statements allowed in a single function.

@@ -8,3 +8,2 @@ from typing import Final

classes,
complex_finally,
counts,

@@ -44,3 +43,2 @@ function,

pm.MatchCasesVisitor,
complex_finally.ComplexFinallyBlocksVisitor,
)

@@ -66,3 +66,3 @@ """

TooManyMatchCaseViolation
ComplexFinallyViolation
TooLongFinallyBodyViolation

@@ -114,3 +114,3 @@ Module complexity

.. autoclass:: TooManyMatchCaseViolation
.. autoclass:: ComplexFinallyViolation
.. autoclass:: TooLongFinallyBodyViolation

@@ -1407,5 +1407,5 @@ """

@final
class ComplexFinallyViolation(ASTViolation):
class TooLongFinallyBodyViolation(ASTViolation):
"""
Forbids complex ``finally`` block.
Forbid ``finally`` blocks with bodies that are too long.

@@ -1418,6 +1418,10 @@ Reasoning:

Solution:
Simplify the ``finally`` block. Use context managers, use ``ExitStack``.
Move things out of the ``finally`` block or create new functions.
The fewer lines you have in your ``finally`` block -
the safer you are from accidental errors.
Use context managers, use ``ExitStack``.
Configuration:
This rule is configurable with ``--max-lines-in-finally``.
Lines equal to statements.
Default:

@@ -1430,6 +1434,8 @@ :str:`wemake_python_styleguide.options.defaults.MAX_LINES_IN_FINALLY`

.. versionadded:: 1.2.0
.. versionchanged:: 1.3.0
Now we count statements in ``finally``, not physical lines.
"""
error_template = 'Found too many lines in `finally` block: {0}'
error_template = 'Found too long `finally` block: {0}'
code = 243

@@ -330,2 +330,4 @@ """

.. versionadded:: 0.1.0
.. versionchanged:: 1.3.0
Added more names: ``spam``, ``ham``, ``tmp``, ``temp``, ``arr``

@@ -332,0 +334,0 @@ """

@@ -194,2 +194,3 @@ import ast

self._check_try_body_length(node)
self._check_finally_body_length(node)
self._check_exceptions_count(node)

@@ -218,2 +219,12 @@ self.generic_visit(node)

def _check_finally_body_length(self, node: AnyTry) -> None:
if len(node.finalbody) > self.options.max_lines_in_finally:
self.add_violation(
complexity.TooLongFinallyBodyViolation(
node,
text=str(len(node.finalbody)),
baseline=self.options.max_lines_in_finally,
),
)
def _check_exceptions_count(self, node: AnyTry) -> None:

@@ -220,0 +231,0 @@ for except_handler in node.handlers:

import ast
from typing import final
from wemake_python_styleguide.violations import complexity
from wemake_python_styleguide.visitors.base import BaseNodeVisitor
@final
class ComplexFinallyBlocksVisitor(BaseNodeVisitor):
"""Ensures there are no complex ``finally`` blocks."""
def visit_Try(self, node: ast.Try) -> None:
"""Visits all finally nodes in the tree."""
self._check_complex_finally(node)
self.generic_visit(node)
def _check_complex_finally(self, node: ast.Try) -> None:
"""Checks complexity of finally blocks."""
if not node.finalbody:
return
first_line = node.finalbody[0].lineno
# `end_lineno` was added in 3.8, but typing is not really correct,
# we are pretty sure that it always exist in modern python versions.
last_line = getattr(node.finalbody[-1], 'end_lineno', 0) or 0
total_lines = last_line - first_line + 1
if total_lines > self.options.max_lines_in_finally:
self.add_violation(
complexity.ComplexFinallyViolation(
node,
text=str(total_lines),
baseline=self.options.max_lines_in_finally,
),
)