From 67168078af04bb4bbd26b4254d0bd4e9323ffd9f Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 14 Dec 2024 00:45:59 +0300 Subject: [PATCH] APply ruff with `preview = true` (#3156) --- .pre-commit-config.yaml | 5 +++ scripts/check_generic_visit.py | 2 +- tests/plugins/violations.py | 2 +- tests/test_checker/test_noqa.py | 4 +- tests/test_violations/test_codes.py | 2 +- .../test_useless_overwriting_method.py | 12 +++--- .../test_ast/test_compares/conftest.py | 8 ++-- .../test_complexity/test_function/conftest.py | 6 +-- .../test_ast/test_naming/conftest.py | 12 +++--- .../test_unused/test_unused_definition.py | 14 +++---- .../test_statements/test_augmented_assign.py | 2 +- .../test_tokenize/test_primitives/conftest.py | 4 +- wemake_python_styleguide/compat/routing.py | 2 +- wemake_python_styleguide/constants.py | 40 +++++++++---------- .../logic/naming/builtins.py | 2 +- .../logic/tokens/constants.py | 2 +- .../logic/tokens/strings.py | 2 +- wemake_python_styleguide/logic/tree/bools.py | 2 +- .../logic/tree/compares.py | 6 ++- .../logic/tree/exceptions.py | 2 +- .../logic/tree/getters_setters.py | 2 +- .../logic/tree/recursion.py | 2 +- wemake_python_styleguide/logic/tree/stubs.py | 3 +- .../logic/tree/variables.py | 6 ++- .../options/validation.py | 2 +- .../transformations/ast/enhancements.py | 2 +- wemake_python_styleguide/violations/base.py | 3 +- .../visitors/ast/attributes.py | 2 +- .../visitors/ast/builtins.py | 8 ++-- .../visitors/ast/classes.py | 8 ++-- .../visitors/ast/complexity/access.py | 2 +- .../visitors/ast/complexity/calls.py | 2 +- .../visitors/ast/complexity/classes.py | 2 +- .../visitors/ast/complexity/counts.py | 3 +- .../visitors/ast/complexity/function.py | 3 +- .../visitors/ast/complexity/overuses.py | 2 +- .../visitors/ast/conditions.py | 3 +- .../visitors/ast/functions.py | 8 ++-- .../visitors/ast/imports.py | 8 ++-- .../visitors/ast/keywords.py | 4 +- .../visitors/ast/loops.py | 2 +- .../visitors/ast/naming/validation.py | 6 ++- .../visitors/ast/naming/variables.py | 2 +- .../visitors/ast/redundancy.py | 3 +- .../visitors/ast/statements.py | 7 ++-- .../visitors/ast/subscripts.py | 5 ++- wemake_python_styleguide/visitors/base.py | 4 +- .../visitors/tokenize/conditions.py | 4 +- .../visitors/tokenize/functions.py | 2 +- .../visitors/tokenize/primitives.py | 4 +- .../visitors/tokenize/statements.py | 8 ++-- 51 files changed, 138 insertions(+), 115 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 510f76206..daf640813 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,6 +10,11 @@ repos: - id: mixed-line-ending args: [--fix=lf] - id: check-case-conflict + - repo: https://github.com/python-jsonschema/check-jsonschema + rev: 0.29.4 + hooks: + - id: check-dependabot + - id: check-github-workflows - repo: https://github.com/rhysd/actionlint rev: v1.7.3 hooks: diff --git a/scripts/check_generic_visit.py b/scripts/check_generic_visit.py index 2539a1664..e4a2c8e0b 100644 --- a/scripts/check_generic_visit.py +++ b/scripts/check_generic_visit.py @@ -35,7 +35,7 @@ def main() -> NoReturn: report('"self.generic_visit(node)" should be last statement here:') for fn, line in matches: - with open(fn) as fp: + with open(fn, encoding='utf8') as fp: lines = fp.read().splitlines() report( '\t{}:{}\n\t{}'.format( diff --git a/tests/plugins/violations.py b/tests/plugins/violations.py index 46e91bef6..ac3a05989 100644 --- a/tests/plugins/violations.py +++ b/tests/plugins/violations.py @@ -55,7 +55,7 @@ def _load_all_violation_classes(): classes.update( { module: sorted(only_classes, key=attrgetter('code')), - } + }, ) return classes diff --git a/tests/test_checker/test_noqa.py b/tests/test_checker/test_noqa.py index 0f7582a60..d2d6e635d 100644 --- a/tests/test_checker/test_noqa.py +++ b/tests/test_checker/test_noqa.py @@ -276,7 +276,7 @@ 'WPS614': 1, 'WPS615': 2, 'WPS616': 1, - } + }, ) # Violations which may be tweaked by `i_control_code` option: @@ -285,7 +285,7 @@ 'WPS113': 0, 'WPS412': 0, 'WPS413': 0, - } + }, ) diff --git a/tests/test_violations/test_codes.py b/tests/test_violations/test_codes.py index eee819eb5..fb9a8a788 100644 --- a/tests/test_violations/test_codes.py +++ b/tests/test_violations/test_codes.py @@ -16,7 +16,7 @@ def test_all_violations_are_final(all_violations): def test_all_unique_violation_messages(all_violations): """Ensures that all violations have unique violation messages.""" messages = Counter( - [violation.error_template for violation in all_violations] + [violation.error_template for violation in all_violations], ) for message, count in messages.items(): assert count == 1, message diff --git a/tests/test_visitors/test_ast/test_classes/test_methods/test_useless_overwriting_method.py b/tests/test_visitors/test_ast/test_classes/test_methods/test_useless_overwriting_method.py index 27319c5d8..69c58512b 100644 --- a/tests/test_visitors/test_ast/test_classes/test_methods/test_useless_overwriting_method.py +++ b/tests/test_visitors/test_ast/test_classes/test_methods/test_useless_overwriting_method.py @@ -153,7 +153,7 @@ def test_useless_overwriting( super_args=super_args, method_name='function', args_invocation=method_args.invocation, - ) + ), ) tree = parse_ast_tree(formatted_code) @@ -199,7 +199,7 @@ def test_useful_due_to_invalid_decorator( super_args=super_args, method_name='function', args_invocation=method_args.invocation, - ) + ), ) tree = parse_ast_tree(formatted_code) @@ -238,7 +238,7 @@ def test_useful_due_to_invalid_statements( super_args=super_args, method_name='function', args_invocation=method_args.invocation, - ) + ), ) tree = parse_ast_tree(formatted_code) @@ -277,7 +277,7 @@ def test_useful_due_to_invalid_super_args( super_args=super_args, method_name='function', args_invocation=method_args.invocation, - ) + ), ) tree = parse_ast_tree(formatted_code) @@ -316,7 +316,7 @@ def test_useful_due_to_invalid_method( super_args=super_args, method_name='invalid_function', args_invocation=method_args.invocation, - ) + ), ) tree = parse_ast_tree(formatted_code) @@ -355,7 +355,7 @@ def test_useful_due_to_invalid_method_args( super_args=super_args, method_name='function', args_invocation=method_args.invocation, - ) + ), ) tree = parse_ast_tree(formatted_code) diff --git a/tests/test_visitors/test_ast/test_compares/conftest.py b/tests/test_visitors/test_ast/test_compares/conftest.py index fea05ef92..c666b5360 100644 --- a/tests/test_visitors/test_ast/test_compares/conftest.py +++ b/tests/test_visitors/test_ast/test_compares/conftest.py @@ -35,7 +35,7 @@ ( if_with_is, if_with_is_not, - ) + ), ) EQUAL_COMPARES = frozenset( @@ -44,7 +44,7 @@ if_with_not_eq, assert_construct, assert_with_message, - ) + ), ) OTHER_COMPARES = frozenset( @@ -55,7 +55,7 @@ if_with_gte, ternary, while_construct, - ) + ), ) @@ -87,7 +87,7 @@ def other_conditions(request): params=[ if_with_in, if_with_not_in, - ] + ], ) def in_conditions(request): """Fixture that returns simple conditionals.""" diff --git a/tests/test_visitors/test_ast/test_complexity/test_function/conftest.py b/tests/test_visitors/test_ast/test_complexity/test_function/conftest.py index 4faf6540d..46c293a02 100644 --- a/tests/test_visitors/test_ast/test_complexity/test_function/conftest.py +++ b/tests/test_visitors/test_ast/test_complexity/test_function/conftest.py @@ -104,7 +104,7 @@ def method(arg1, arg2): ... new_method_without_arguments, metaclass_without_arguments, staticmethod_with_no_argument, - ] + ], ) def no_argument(request): """Fixture that returns different code examples that have no args.""" @@ -122,7 +122,7 @@ def no_argument(request): new_method_single_argument, metaclass_with_single_argument, staticmethod_with_single_argument, - ] + ], ) def single_argument(request): """Fixture that returns different code examples that have one arg.""" @@ -137,7 +137,7 @@ def single_argument(request): function_with_posonly, method_with_arguments, staticmethod_with_arguments, - ] + ], ) def two_arguments(request): """Fixture that returns different code examples that have two args.""" diff --git a/tests/test_visitors/test_ast/test_naming/conftest.py b/tests/test_visitors/test_ast/test_naming/conftest.py index 78a4613cf..cacf85dba 100644 --- a/tests/test_visitors/test_ast/test_naming/conftest.py +++ b/tests/test_visitors/test_ast/test_naming/conftest.py @@ -253,7 +253,7 @@ def container(): match_as_explicit, match_inner, match_star, - ) + ), ) if PY312: @@ -270,7 +270,7 @@ def container(): ( foreign_attribute, foreign_nested_attribute, - ) + ), ) _ATTRIBUTES = ( @@ -283,7 +283,7 @@ def container(): static_typed_annotation, instance_attribute, instance_typed_attribute, - ) + ), ) | _FOREIGN_NAMING_PATTERNS ) @@ -292,7 +292,7 @@ def container(): ( # Not really an attribute, but similar: type_param_class, - ) + ), ) @@ -302,7 +302,7 @@ def container(): variable_def, with_variable, for_variable, - ) + ), ) # Raw unused variables return True for logic.naming.access.is_unused(). @@ -320,7 +320,7 @@ def container(): assignment_expression, # Pattern matching: match_as_explicit, - ) + ), ) _FORBIDDEN_RAW_UNUSED = _FORBIDDEN_BOTH_RAW_AND_PROTECTED_UNUSED | { diff --git a/tests/test_visitors/test_ast/test_naming/test_unused/test_unused_definition.py b/tests/test_visitors/test_ast/test_naming/test_unused/test_unused_definition.py index be20f016a..a5a539570 100644 --- a/tests/test_visitors/test_ast/test_naming/test_unused/test_unused_definition.py +++ b/tests/test_visitors/test_ast/test_naming/test_unused/test_unused_definition.py @@ -54,7 +54,7 @@ def test_unused_variable_tuple_definition( forbidden_tuple_unused_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) @@ -98,7 +98,7 @@ def test_used_variable_tuple_definition_allowed( forbidden_tuple_unused_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) @@ -143,7 +143,7 @@ def test_raw_unused_variable_definition( forbidden_raw_unused_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) @@ -186,7 +186,7 @@ def test_raw_unused_variable_definition_allowed( allowed_raw_unused_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) @@ -227,7 +227,7 @@ def test_protected_unused_variable_definition( forbidden_protected_unused_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) @@ -269,7 +269,7 @@ def test_protected_unused_var_definition_allowed( allowed_protected_unused_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) @@ -312,7 +312,7 @@ def test_used_variable_definition_allowed( naming_template.format(bad_name), ' ' * indentation, ), - ) + ), ), ) diff --git a/tests/test_visitors/test_ast/test_statements/test_augmented_assign.py b/tests/test_visitors/test_ast/test_statements/test_augmented_assign.py index f10075522..3d32f4c35 100644 --- a/tests/test_visitors/test_ast/test_statements/test_augmented_assign.py +++ b/tests/test_visitors/test_ast/test_statements/test_augmented_assign.py @@ -20,7 +20,7 @@ '^', '>>', '<<', - ) + ), ) diff --git a/tests/test_visitors/test_tokenize/test_primitives/conftest.py b/tests/test_visitors/test_tokenize/test_primitives/conftest.py index 18207b367..0e0b8515e 100644 --- a/tests/test_visitors/test_tokenize/test_primitives/conftest.py +++ b/tests/test_visitors/test_tokenize/test_primitives/conftest.py @@ -16,7 +16,7 @@ default_param, default_param_with_type, statement_with_expression, - ] + ], ) def primitives_usages(request): """Fixture to return possible cases of promitives use cases.""" @@ -58,7 +58,7 @@ def factory(template: str) -> str: 'regular_number_wrapper', 'negative_number_wrapper', 'positive_number_wrapper', - ] + ], ) def number_sign(request): """Fixture that returns regular, negative, and positive numbers.""" diff --git a/wemake_python_styleguide/compat/routing.py b/wemake_python_styleguide/compat/routing.py index 4fbcd0e7e..50dbb0f87 100644 --- a/wemake_python_styleguide/compat/routing.py +++ b/wemake_python_styleguide/compat/routing.py @@ -13,7 +13,7 @@ str: 'Str', bytes: 'Bytes', type(...): 'Ellipsis', - } + }, ) diff --git a/wemake_python_styleguide/constants.py b/wemake_python_styleguide/constants.py index 3ab415950..591887670 100644 --- a/wemake_python_styleguide/constants.py +++ b/wemake_python_styleguide/constants.py @@ -78,7 +78,7 @@ # Mypy: 'reveal_type', 'reveal_locals', - ) + ), ) #: List of module metadata we forbid to use. @@ -88,7 +88,7 @@ '__all__', '__version__', '__about__', - ) + ), ) #: List of variable names we forbid to use. @@ -129,7 +129,7 @@ 'foo', 'bar', 'baz', - ) + ), ) #: List of character sequences that are hard to read. @@ -141,7 +141,7 @@ 'O0', # Not included: 'lI', 'l1', 'Il' # Because these names are quite common in real words. - ) + ), ) #: List of special names that are used only as first argument in methods. @@ -150,7 +150,7 @@ 'self', 'cls', 'mcs', - ) + ), ) #: List of all magic methods from the python docs. @@ -286,7 +286,7 @@ '__fspath__', # sys '__sizeof__', - ) + ), ) #: List of magic methods that are forbidden to use. @@ -301,7 +301,7 @@ '__reduce_ex__', '__dir__', # since we don't use `dir()` '__delattr__', # since we don't use `delattr()` - ) + ), ) #: List of magic methods that are not allowed to be generators. @@ -311,7 +311,7 @@ '__call__', '__iter__', '__aiter__', - } + }, ) #: List of magic methods that are not allowed to be async. @@ -325,7 +325,7 @@ '__aexit__', '__aiter__', '__call__', - } + }, ) #: List of builtin classes that are allowed to subclass. @@ -333,7 +333,7 @@ ( 'type', 'object', - ) + ), ) #: List of builtins that we allow to shadow. @@ -343,7 +343,7 @@ 'license', 'copyright', 'credits', - ) + ), ) #: List of nested functions' names we allow to use. @@ -352,7 +352,7 @@ 'decorator', 'factory', 'wrapper', - ) + ), ) #: List of allowed ``__future__`` imports. @@ -360,7 +360,7 @@ ( 'annotations', 'generator_stop', - ) + ), ) #: List of blacklisted module names. @@ -370,7 +370,7 @@ 'utils', 'utilities', 'helpers', - ) + ), ) #: List of allowed module magic names. @@ -378,7 +378,7 @@ ( '__init__', '__main__', - ) + ), ) #: List of bad magic module functions. @@ -386,7 +386,7 @@ ( '__getattr__', '__dir__', - ) + ), ) #: Regex pattern to name modules. @@ -405,7 +405,7 @@ 24, # hours 60, # seconds, minutes 1j, # imaginary part of a complex number - ) + ), ) #: Maximum amount of ``pragma`` no-cover comments per module. @@ -432,7 +432,7 @@ math.pi, math.e, math.tau, - ) + ), ) #: List of vague method names that may cause confusion if imported as is: @@ -451,7 +451,7 @@ 'dump_all', 'safe_load_all', 'safe_dump_all', - ) + ), ) #: List of functions in which arguments must be tuples. @@ -470,5 +470,5 @@ 'sns', 'tf', 'cv', - ) + ), ) diff --git a/wemake_python_styleguide/logic/naming/builtins.py b/wemake_python_styleguide/logic/naming/builtins.py index 8a4c61de8..ba8294954 100644 --- a/wemake_python_styleguide/logic/naming/builtins.py +++ b/wemake_python_styleguide/logic/naming/builtins.py @@ -20,7 +20,7 @@ # Some python version have them, some do not have them: 'async', 'await', - ) + ), ) diff --git a/wemake_python_styleguide/logic/tokens/constants.py b/wemake_python_styleguide/logic/tokens/constants.py index 6c8bbaacc..43c343fec 100644 --- a/wemake_python_styleguide/logic/tokens/constants.py +++ b/wemake_python_styleguide/logic/tokens/constants.py @@ -6,5 +6,5 @@ ( tokenize.NL, tokenize.NEWLINE, - ) + ), ) diff --git a/wemake_python_styleguide/logic/tokens/strings.py b/wemake_python_styleguide/logic/tokens/strings.py index f7121b3d0..71b2a1397 100644 --- a/wemake_python_styleguide/logic/tokens/strings.py +++ b/wemake_python_styleguide/logic/tokens/strings.py @@ -25,7 +25,7 @@ def has_triple_string_quotes(string_contents: str) -> bool: or ( string_contents.startswith("'''") and string_contents.endswith("'''") - ) + ), ) diff --git a/wemake_python_styleguide/logic/tree/bools.py b/wemake_python_styleguide/logic/tree/bools.py index 6702009ec..a0135b4fe 100644 --- a/wemake_python_styleguide/logic/tree/bools.py +++ b/wemake_python_styleguide/logic/tree/bools.py @@ -8,5 +8,5 @@ def count_boolops(node: ast.AST) -> int: subnode for subnode in ast.walk(node) if isinstance(subnode, ast.BoolOp) - ] + ], ) diff --git a/wemake_python_styleguide/logic/tree/compares.py b/wemake_python_styleguide/logic/tree/compares.py index 55912d8cb..846eb769f 100644 --- a/wemake_python_styleguide/logic/tree/compares.py +++ b/wemake_python_styleguide/logic/tree/compares.py @@ -37,7 +37,7 @@ class _Bounds: ast.GtE: (ast.Gt, ast.GtE), ast.Lt: (ast.Lt, ast.LtE), ast.LtE: (ast.Lt, ast.LtE), - } + }, ) @@ -91,7 +91,9 @@ def _find_lower_upper_bounds( ) -> None: left_operand = comparison_node.left comparators = zip( - comparison_node.ops, comparison_node.comparators, strict=False + comparison_node.ops, + comparison_node.comparators, + strict=False, ) for operator, right_operand in comparators: diff --git a/wemake_python_styleguide/logic/tree/exceptions.py b/wemake_python_styleguide/logic/tree/exceptions.py index 5c001c8d2..2d7f0d341 100644 --- a/wemake_python_styleguide/logic/tree/exceptions.py +++ b/wemake_python_styleguide/logic/tree/exceptions.py @@ -35,7 +35,7 @@ def get_all_exception_names(node: AnyTry) -> list[str]: exceptions.append(source.node_to_string(exc_handler.type)) elif isinstance(exc_handler.type, ast.Tuple): exceptions.extend( - [source.node_to_string(node) for node in exc_handler.type.elts] + [source.node_to_string(node) for node in exc_handler.type.elts], ) return exceptions diff --git a/wemake_python_styleguide/logic/tree/getters_setters.py b/wemake_python_styleguide/logic/tree/getters_setters.py index 35c6b6764..3179ae5de 100644 --- a/wemake_python_styleguide/logic/tree/getters_setters.py +++ b/wemake_python_styleguide/logic/tree/getters_setters.py @@ -48,7 +48,7 @@ def find_attributed_getters_and_setters( { instance.attr.lstrip(UNUSED_PLACEHOLDER) for instance in instance_attributes - } + }, ) for method in _find_getters_and_setters(node): diff --git a/wemake_python_styleguide/logic/tree/recursion.py b/wemake_python_styleguide/logic/tree/recursion.py index e02979338..405a0bf35 100644 --- a/wemake_python_styleguide/logic/tree/recursion.py +++ b/wemake_python_styleguide/logic/tree/recursion.py @@ -24,7 +24,7 @@ def _check_function_recursion(func: AnyFunctionDef) -> bool: for node in walk(func) if isinstance(node, Call) and given_function_called(node, {func.name}) - ] + ], ) diff --git a/wemake_python_styleguide/logic/tree/stubs.py b/wemake_python_styleguide/logic/tree/stubs.py index 03917ee7d..fe55ab76d 100644 --- a/wemake_python_styleguide/logic/tree/stubs.py +++ b/wemake_python_styleguide/logic/tree/stubs.py @@ -15,7 +15,8 @@ def is_stub(node: AnyFunctionDef) -> bool: - a docstring + a `raise` statement """ function_has_docstring = isinstance(node.body[0], Expr) and isinstance( - node.body[0].value, Str + node.body[0].value, + Str, ) if function_has_docstring: return _is_stub_with_docstring(node) diff --git a/wemake_python_styleguide/logic/tree/variables.py b/wemake_python_styleguide/logic/tree/variables.py index 8b788556f..56fa0e6b0 100644 --- a/wemake_python_styleguide/logic/tree/variables.py +++ b/wemake_python_styleguide/logic/tree/variables.py @@ -42,10 +42,12 @@ def is_getting_element_by_unpacking(targets: list[ast.expr]) -> bool: if len(targets) != 2: return False first_item = isinstance( - targets[1], ast.Starred + targets[1], + ast.Starred, ) and _is_unused_variable_name(targets[1].value) last_item = isinstance( - targets[0], ast.Starred + targets[0], + ast.Starred, ) and _is_unused_variable_name(targets[0].value) return first_item or last_item diff --git a/wemake_python_styleguide/options/validation.py b/wemake_python_styleguide/options/validation.py index fee9f5b65..a26762470 100644 --- a/wemake_python_styleguide/options/validation.py +++ b/wemake_python_styleguide/options/validation.py @@ -16,7 +16,7 @@ def factory(instance, attribute, field_value): max_contract = max is not None and field_value > max if min_contract or max_contract: raise ValueError( - f'Option {attribute.name} is out of bounds: {field_value}' + f'Option {attribute.name} is out of bounds: {field_value}', ) return factory diff --git a/wemake_python_styleguide/transformations/ast/enhancements.py b/wemake_python_styleguide/transformations/ast/enhancements.py index 06f10fc29..736d04d28 100644 --- a/wemake_python_styleguide/transformations/ast/enhancements.py +++ b/wemake_python_styleguide/transformations/ast/enhancements.py @@ -28,7 +28,7 @@ ast.BitAnd: operator.and_, ast.BitOr: operator.or_, ast.BitXor: operator.xor, - } + }, ) diff --git a/wemake_python_styleguide/violations/base.py b/wemake_python_styleguide/violations/base.py index 165c46500..b23c879eb 100644 --- a/wemake_python_styleguide/violations/base.py +++ b/wemake_python_styleguide/violations/base.py @@ -73,8 +73,7 @@ class ViolationPostfixes(enum.Enum): less_than = ' < {0}' -# TODO: remove `noqa` after a new release (0.17.0): -class BaseViolation(metaclass=abc.ABCMeta): # noqa: WPS338 +class BaseViolation(abc.ABC): """ Abstract base class for all style violations. diff --git a/wemake_python_styleguide/visitors/ast/attributes.py b/wemake_python_styleguide/visitors/ast/attributes.py index ffb346d49..bbe756f1d 100644 --- a/wemake_python_styleguide/visitors/ast/attributes.py +++ b/wemake_python_styleguide/visitors/ast/attributes.py @@ -44,7 +44,7 @@ def _ensure_attribute_type( return if isinstance(node.value, ast.Call) and self._is_super_called( - node.value + node.value, ): return diff --git a/wemake_python_styleguide/visitors/ast/builtins.py b/wemake_python_styleguide/visitors/ast/builtins.py index c86522091..34b3e25d2 100644 --- a/wemake_python_styleguide/visitors/ast/builtins.py +++ b/wemake_python_styleguide/visitors/ast/builtins.py @@ -57,7 +57,7 @@ class WrongStringVisitor(base.BaseNodeVisitor): string.printable, string.whitespace, string.punctuation, - ) + ), ) def visit_any_string(self, node: AnyText) -> None: @@ -137,7 +137,8 @@ def _check_complex_formatted_string(self, node: ast.JoinedStr) -> None: def _is_valid_formatted_value(self, format_value: ast.AST) -> bool: if isinstance( - format_value, self._chainable_types + format_value, + self._chainable_types, ) and not self._is_valid_chaining(format_value): return False return self._is_valid_final_value(format_value) @@ -396,7 +397,8 @@ def _check_float_keys(self, keys: _HashItems) -> None: real_key = operators.unwrap_unary_node(dict_key) is_float_key = isinstance(real_key, ast.Num) and isinstance( - real_key.n, float + real_key.n, + float, ) if is_float_key or evaluates_to_float: self.add_violation(best_practices.FloatKeyViolation(dict_key)) diff --git a/wemake_python_styleguide/visitors/ast/classes.py b/wemake_python_styleguide/visitors/ast/classes.py index 5d53452be..7dac08740 100644 --- a/wemake_python_styleguide/visitors/ast/classes.py +++ b/wemake_python_styleguide/visitors/ast/classes.py @@ -58,7 +58,7 @@ def _is_correct_base_class(self, base_class: ast.AST) -> bool: filter( lambda part: isinstance(part, ast.Subscript), parts, - ) + ), ) correct_items = all( isinstance(sub_node, ast.Name | ast.Attribute | ast.Subscript) @@ -124,7 +124,7 @@ def _check_getters_setters_methods(self, node: ast.ClassDef) -> None: ).union( set( # To delete duplicated violations getters_setters.find_attributed_getters_and_setters(node), - ) + ), ) for method in getters_and_setters: self.add_violation( @@ -395,7 +395,9 @@ def _check_method_order(self, node: ast.ClassDef) -> None: ideal = sorted(method_nodes, key=self._ideal_order, reverse=True) for existing_order, ideal_order in zip( - method_nodes, ideal, strict=False + method_nodes, + ideal, + strict=False, ): if existing_order != ideal_order: self.add_violation(consistency.WrongMethodOrderViolation(node)) diff --git a/wemake_python_styleguide/visitors/ast/complexity/access.py b/wemake_python_styleguide/visitors/ast/complexity/access.py index df2e6f728..f21981e8d 100644 --- a/wemake_python_styleguide/visitors/ast/complexity/access.py +++ b/wemake_python_styleguide/visitors/ast/complexity/access.py @@ -49,7 +49,7 @@ def _check_consecutive_access_number(self, node: AnyAccess) -> None: takewhile( self._is_any_access, attributes.parts(node), - ) + ), ), ) diff --git a/wemake_python_styleguide/visitors/ast/complexity/calls.py b/wemake_python_styleguide/visitors/ast/complexity/calls.py index d438a0067..68ae81c6f 100644 --- a/wemake_python_styleguide/visitors/ast/complexity/calls.py +++ b/wemake_python_styleguide/visitors/ast/complexity/calls.py @@ -35,7 +35,7 @@ def _check_consecutive_call_number(self, node: ast.Call) -> None: takewhile( self._is_call, parts(node), - ) + ), ) self._visited_calls.update(consecutive_calls) diff --git a/wemake_python_styleguide/visitors/ast/complexity/classes.py b/wemake_python_styleguide/visitors/ast/complexity/classes.py index 799077a7a..76331b78b 100644 --- a/wemake_python_styleguide/visitors/ast/complexity/classes.py +++ b/wemake_python_styleguide/visitors/ast/complexity/classes.py @@ -62,7 +62,7 @@ def _check_public_attributes(self, node: ast.ClassDef) -> None: attr.attr for attr in instance_attributes if access.is_public(attr.attr) - } + }, ) if attrs_count > self.options.max_attributes: diff --git a/wemake_python_styleguide/visitors/ast/complexity/counts.py b/wemake_python_styleguide/visitors/ast/complexity/counts.py index 1bf96e83e..398ab8ef6 100644 --- a/wemake_python_styleguide/visitors/ast/complexity/counts.py +++ b/wemake_python_styleguide/visitors/ast/complexity/counts.py @@ -48,7 +48,8 @@ def _check_members_count(self, node: _ModuleMembers) -> None: return if isinstance( - node, FunctionNodes + node, + FunctionNodes, ) and decorators.has_overload_decorator(node): return # We don't count `@overload` defs as real defs diff --git a/wemake_python_styleguide/visitors/ast/complexity/function.py b/wemake_python_styleguide/visitors/ast/complexity/function.py index 6bab131d9..207c65aa0 100644 --- a/wemake_python_styleguide/visitors/ast/complexity/function.py +++ b/wemake_python_styleguide/visitors/ast/complexity/function.py @@ -90,7 +90,8 @@ def _check_sub_node( sub_node: ast.AST, ) -> None: if isinstance(sub_node, ast.Name) and isinstance( - sub_node.ctx, ast.Store + sub_node.ctx, + ast.Store, ): self._update_variables(node, sub_node) diff --git a/wemake_python_styleguide/visitors/ast/complexity/overuses.py b/wemake_python_styleguide/visitors/ast/complexity/overuses.py index 3d6726681..2dd50a22d 100644 --- a/wemake_python_styleguide/visitors/ast/complexity/overuses.py +++ b/wemake_python_styleguide/visitors/ast/complexity/overuses.py @@ -58,7 +58,7 @@ class StringOveruseVisitor(base.BaseNodeVisitor): b'\n', b'\r\n', b'\t', - ) + ), ) def __init__(self, *args, **kwargs) -> None: diff --git a/wemake_python_styleguide/visitors/ast/conditions.py b/wemake_python_styleguide/visitors/ast/conditions.py index fe083e6e6..3817607b4 100644 --- a/wemake_python_styleguide/visitors/ast/conditions.py +++ b/wemake_python_styleguide/visitors/ast/conditions.py @@ -54,7 +54,8 @@ def _check_negated_conditions(self, node: AnyIf) -> None: def _check_useless_len(self, node: AnyIf) -> None: if isinstance(node.test, ast.Call) and given_function_called( - node.test, {'len'} + node.test, + {'len'}, ): self.add_violation(refactoring.UselessLenCompareViolation(node)) diff --git a/wemake_python_styleguide/visitors/ast/functions.py b/wemake_python_styleguide/visitors/ast/functions.py index c8516f4a5..36999c733 100644 --- a/wemake_python_styleguide/visitors/ast/functions.py +++ b/wemake_python_styleguide/visitors/ast/functions.py @@ -207,7 +207,8 @@ def _check_open_call_context(self, node: ast.Call) -> None: return if_exp_inside_with = isinstance(parent_node, ast.IfExp) and isinstance( - nodes.get_parent(parent_node), ast.withitem + nodes.get_parent(parent_node), + ast.withitem, ) if if_exp_inside_with: @@ -275,7 +276,7 @@ class FunctionDefinitionVisitor(base.BaseNodeVisitor): 'classmethod', 'staticmethod', 'property', - ) + ), ) def visit_any_function(self, node: AnyFunctionDef) -> None: @@ -341,7 +342,8 @@ def _maybe_update_variable( return is_name_def = isinstance(sub_node, ast.Name) and isinstance( - sub_node.ctx, ast.Store + sub_node.ctx, + ast.Store, ) if is_name_def or isinstance(sub_node, ast.ExceptHandler): diff --git a/wemake_python_styleguide/visitors/ast/imports.py b/wemake_python_styleguide/visitors/ast/imports.py index a97038818..31c5b7960 100644 --- a/wemake_python_styleguide/visitors/ast/imports.py +++ b/wemake_python_styleguide/visitors/ast/imports.py @@ -85,7 +85,7 @@ def _check_protected_import(self, node: ast.Import) -> None: [ alias.name.split(_MODULE_MEMBERS_SEPARATOR) for alias in node.names - ] + ], ) for name in names: if access.is_protected(name): @@ -170,7 +170,7 @@ def validate(self) -> None: ImportCollisionViolation( first.node, second.module, - ) + ), ) def add_import(self, node: ast.Import) -> None: @@ -181,7 +181,7 @@ def add_import(self, node: ast.Import) -> None: imports.ImportedObjectInfo( alias.name, node, - ) + ), ) def add_import_from(self, node: ast.ImportFrom) -> None: @@ -202,7 +202,7 @@ def add_import_from(self, node: ast.ImportFrom) -> None: filter(None, (node.module, alias.name)), ), node, - ) + ), ) def _does_collide( diff --git a/wemake_python_styleguide/visitors/ast/keywords.py b/wemake_python_styleguide/visitors/ast/keywords.py index e8a4d8eb4..1eb0903d4 100644 --- a/wemake_python_styleguide/visitors/ast/keywords.py +++ b/wemake_python_styleguide/visitors/ast/keywords.py @@ -96,8 +96,8 @@ def _check_last_return_in_function(self, node: ast.Return) -> None: filter( lambda return_node: return_node.value is not None, walk.get_subnodes_by_type(parent, ast.Return), - ) - ) + ), + ), ) last_value_return = ( diff --git a/wemake_python_styleguide/visitors/ast/loops.py b/wemake_python_styleguide/visitors/ast/loops.py index 342955c97..eb025f26c 100644 --- a/wemake_python_styleguide/visitors/ast/loops.py +++ b/wemake_python_styleguide/visitors/ast/loops.py @@ -166,7 +166,7 @@ def _check_useless_continue(self, node: AnyLoop) -> None: if lineno is not None: nodes_at_line[lineno].append(sub_node) - last_line = nodes_at_line[sorted(nodes_at_line.keys())[-1]] + last_line = nodes_at_line[max(nodes_at_line.keys())] if any(isinstance(last, ast.Continue) for last in last_line): self.add_violation(UselessContinueViolation(node)) diff --git a/wemake_python_styleguide/visitors/ast/naming/validation.py b/wemake_python_styleguide/visitors/ast/naming/validation.py index f5f95b0d8..5fbf08c97 100644 --- a/wemake_python_styleguide/visitors/ast/naming/validation.py +++ b/wemake_python_styleguide/visitors/ast/naming/validation.py @@ -198,7 +198,8 @@ class _FunctionNameValidator(_RegularNameValidator): def check_function_signature(self, node: AnyFunctionDefAndLambda) -> None: for arg in functions.get_all_arguments(node): is_first_argument = functions.is_first_argument( - node, arg.arg + node, + arg.arg, ) and not isinstance(node, ast.Lambda) self.check_name( arg, @@ -347,7 +348,8 @@ def visit_named_match(self, node: NamedMatch) -> None: self.generic_visit(node) def visit_TypeAlias( - self, node: TypeAliasNode + self, + node: TypeAliasNode, ) -> None: # pragma: >=3.12 cover """Visit PEP695 type aliases.""" self._type_params_validator.check_type_params(node) diff --git a/wemake_python_styleguide/visitors/ast/naming/variables.py b/wemake_python_styleguide/visitors/ast/naming/variables.py index c8f9b700c..fe03fbfaf 100644 --- a/wemake_python_styleguide/visitors/ast/naming/variables.py +++ b/wemake_python_styleguide/visitors/ast/naming/variables.py @@ -176,7 +176,7 @@ def visit_any_for(self, node: AnyFor) -> None: """Checks that we cannot create explicit unused loops.""" target_names = name_nodes.get_variables_from_node(node.target) is_target_no_op_variable = len(target_names) == 1 and access.is_unused( - target_names[0] + target_names[0], ) if not is_target_no_op_variable: # see issue 1406 self._check_assign_unused( diff --git a/wemake_python_styleguide/visitors/ast/redundancy.py b/wemake_python_styleguide/visitors/ast/redundancy.py index ba58404ec..c6824e002 100644 --- a/wemake_python_styleguide/visitors/ast/redundancy.py +++ b/wemake_python_styleguide/visitors/ast/redundancy.py @@ -42,7 +42,8 @@ def visit_any_for(self, node: AnyFor) -> None: self.generic_visit(node) def _check_for_redundant_enumerate( - self, node: AnyFor | ast.comprehension + self, + node: AnyFor | ast.comprehension, ) -> None: # noqa: E501 if not isinstance(node.iter, ast.Call): return diff --git a/wemake_python_styleguide/visitors/ast/statements.py b/wemake_python_styleguide/visitors/ast/statements.py index 744a9808c..97fbaa9f9 100644 --- a/wemake_python_styleguide/visitors/ast/statements.py +++ b/wemake_python_styleguide/visitors/ast/statements.py @@ -238,7 +238,8 @@ def _check_self_misrefactored_assignment( node_value = node.value.left if isinstance( - node.value, self._blocked_self_assignment + node.value, + self._blocked_self_assignment, ) and name_nodes.is_same_variable(node.target, node_value): self.add_violation(MisrefactoredAssignmentViolation(node)) @@ -280,7 +281,7 @@ def _check_starred_args( ) -> None: for node in args: if isinstance(node, ast.Starred) and self._is_pointless_star( - node.value + node.value, ): self.add_violation(PointlessStarredViolation(node)) @@ -336,7 +337,7 @@ def _has_wrong_keys(self, node: ast.keyword) -> bool: for key_node in node.value.keys: if isinstance(key_node, ast.Str) and not str.isidentifier( - key_node.s + key_node.s, ): return True return False diff --git a/wemake_python_styleguide/visitors/ast/subscripts.py b/wemake_python_styleguide/visitors/ast/subscripts.py index 8d1706f57..1f70116c4 100644 --- a/wemake_python_styleguide/visitors/ast/subscripts.py +++ b/wemake_python_styleguide/visitors/ast/subscripts.py @@ -65,7 +65,7 @@ def _check_redundant_subscript(self, node: ast.Subscript) -> None: ) upper_ok = node.slice.upper is None or not self._is_none( - node.slice.upper + node.slice.upper, ) step_ok = node.slice.step is None or ( @@ -88,7 +88,8 @@ def _check_slice_assignment(self, node: ast.Subscript) -> None: slice_expr = node.slice slice_function_assignment = isinstance( - slice_expr, ast.Call + slice_expr, + ast.Call, ) and functions.given_function_called(slice_expr, {'slice'}) if subscript_slice_assignment or slice_function_assignment: diff --git a/wemake_python_styleguide/visitors/base.py b/wemake_python_styleguide/visitors/base.py index c777f44b8..076a2ab88 100644 --- a/wemake_python_styleguide/visitors/base.py +++ b/wemake_python_styleguide/visitors/base.py @@ -74,7 +74,7 @@ from wemake_python_styleguide.violations.base import BaseViolation -class BaseVisitor(metaclass=abc.ABCMeta): +class BaseVisitor(abc.ABC): """ Abstract base class for different types of visitors. @@ -194,7 +194,7 @@ def run(self) -> None: self._post_visit() -class BaseFilenameVisitor(BaseVisitor, metaclass=abc.ABCMeta): +class BaseFilenameVisitor(BaseVisitor, abc.ABC): """ Abstract base class that allows to visit and check module file names. diff --git a/wemake_python_styleguide/visitors/tokenize/conditions.py b/wemake_python_styleguide/visitors/tokenize/conditions.py index cc060bcf7..518542862 100644 --- a/wemake_python_styleguide/visitors/tokenize/conditions.py +++ b/wemake_python_styleguide/visitors/tokenize/conditions.py @@ -43,7 +43,7 @@ class IfElseVisitor(BaseTokenVisitor): ( tokenize.INDENT, tokenize.DEDENT, - ) + ), ) _allowed_token_types: ClassVar[frozenset[int]] = frozenset( @@ -52,7 +52,7 @@ class IfElseVisitor(BaseTokenVisitor): tokenize.NL, tokenize.COLON, tokenize.INDENT, - ) + ), ) def visit_name(self, token: tokenize.TokenInfo) -> None: diff --git a/wemake_python_styleguide/visitors/tokenize/functions.py b/wemake_python_styleguide/visitors/tokenize/functions.py index dc9de0cad..04d8111cb 100644 --- a/wemake_python_styleguide/visitors/tokenize/functions.py +++ b/wemake_python_styleguide/visitors/tokenize/functions.py @@ -108,7 +108,7 @@ def analyze(self) -> Iterable[best_practices.WrongEmptyLinesCountViolation]: for function in self._file_functions.search_functions(): splitted_function_body = function.body().strip().split('\n') empty_lines_count = len( - [line for line in splitted_function_body if line == ''] + [line for line in splitted_function_body if line == ''], ) if not empty_lines_count: continue diff --git a/wemake_python_styleguide/visitors/tokenize/primitives.py b/wemake_python_styleguide/visitors/tokenize/primitives.py index c0c12264c..6cbbbc52d 100644 --- a/wemake_python_styleguide/visitors/tokenize/primitives.py +++ b/wemake_python_styleguide/visitors/tokenize/primitives.py @@ -91,7 +91,7 @@ class _StringTokenChecker: 'F', 'B', 'U', - ) + ), ) _unicode_escapes: ClassVar[frozenset[str]] = frozenset( @@ -99,7 +99,7 @@ class _StringTokenChecker: 'u', 'U', 'N', - ) + ), ) _implicit_raw_strings: ClassVar[re.Pattern[str]] = re.compile(r'\\{2}.+') diff --git a/wemake_python_styleguide/visitors/tokenize/statements.py b/wemake_python_styleguide/visitors/tokenize/statements.py index 3586ed335..4885ce152 100644 --- a/wemake_python_styleguide/visitors/tokenize/statements.py +++ b/wemake_python_styleguide/visitors/tokenize/statements.py @@ -72,15 +72,15 @@ def _post_visit(self) -> None: previous_line_token = None next_line_token = None if index != 0: - previous_line_token = sorted( + previous_line_token = max( self._lines[linenos[index - 1]], key=attrgetter('start'), - )[-1] + ) if index + 1 < len(linenos): - next_line_token = sorted( + next_line_token = min( self._lines[linenos[index + 1]], key=attrgetter('start'), - )[0] + ) self._check_individual_line( line_tokens, previous_line_token,