diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ddf8724e..bba260093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -127,7 +127,7 @@ Semantic versioning in our case means: - Adds a new rule to find too complex `except` with too many exceptions - Adds a new rule to find too many `PEP695` type params - Adds a new rule to find useless ternary expressions, #1706 -- Adds a new rule to forbid `raise SystemError`, use `sys.exit` instead, #1786 +- Adds a new rule to forbid `raise SystemExit`, use `sys.exit` instead, #1786 - Adds a new rule to forbid extra syntax in `match ...` subjects, #3217 - Adds new `--allowed-module-metadata` and `--forbidden-module-metadata` configuration options for `WPS410`, #3060 diff --git a/tests/fixtures/noqa/noqa.py b/tests/fixtures/noqa/noqa.py index 1afd18d34..d7b7c03df 100644 --- a/tests/fixtures/noqa/noqa.py +++ b/tests/fixtures/noqa/noqa.py @@ -562,8 +562,8 @@ def set_attribute(self): # noqa: WPS615 a_list[slice(1)] = [1, 2] # noqa: WPS362 -def function_with_systemerror(): - raise SystemError(1) # noqa: WPS363 +def function_with_SystemExit(): + raise SystemExit(1) # noqa: WPS363 try: diff --git a/tests/test_visitors/test_ast/test_keywords/test_raise/test_system_error_violation.py b/tests/test_visitors/test_ast/test_keywords/test_raise/test_system_error_violation.py index a6a24bad6..2384b71f3 100644 --- a/tests/test_visitors/test_ast/test_keywords/test_raise/test_system_error_violation.py +++ b/tests/test_visitors/test_ast/test_keywords/test_raise/test_system_error_violation.py @@ -1,7 +1,7 @@ import pytest from wemake_python_styleguide.violations.consistency import ( - RaiseSystemErrorViolation, + RaiseSystemExitViolation, ) from wemake_python_styleguide.visitors.ast.keywords import WrongRaiseVisitor @@ -11,10 +11,10 @@ @pytest.mark.parametrize( 'code', [ - 'SystemError', - 'SystemError()', - 'SystemError(0)', - 'SystemError(code)', + 'SystemExit', + 'SystemExit()', + 'SystemExit(0)', + 'SystemExit(code)', ], ) def test_raise_system_error( @@ -23,13 +23,13 @@ def test_raise_system_error( code, default_options, ): - """Testing `raise SystemError` is restricted.""" + """Testing `raise SystemExit` is restricted.""" tree = parse_ast_tree(template.format(code)) visitor = WrongRaiseVisitor(default_options, tree=tree) visitor.run() - assert_errors(visitor, [RaiseSystemErrorViolation]) + assert_errors(visitor, [RaiseSystemExitViolation]) @pytest.mark.parametrize( @@ -37,10 +37,11 @@ def test_raise_system_error( [ 'NotImplementedError', 'NotImplementedError()', - 'CustomSystemError', - 'CustomSystemError()', - 'custom.SystemError', - 'custom.SystemError()', + 'CustomSystemExit', + 'CustomSystemExit()', + 'custom.SystemExit', + 'custom.SystemExit()', + 'SystemError', ], ) def test_raise_good_errors( diff --git a/wemake_python_styleguide/violations/consistency.py b/wemake_python_styleguide/violations/consistency.py index f3ff4b74f..77929dc23 100644 --- a/wemake_python_styleguide/violations/consistency.py +++ b/wemake_python_styleguide/violations/consistency.py @@ -86,7 +86,7 @@ RawStringNotNeededViolation InconsistentComprehensionViolation AssignToSliceViolation - RaiseSystemErrorViolation + RaiseSystemExitViolation Consistency checks ------------------ @@ -154,7 +154,7 @@ .. autoclass:: RawStringNotNeededViolation .. autoclass:: InconsistentComprehensionViolation .. autoclass:: AssignToSliceViolation -.. autoclass:: RaiseSystemErrorViolation +.. autoclass:: RaiseSystemExitViolation """ @@ -2492,9 +2492,9 @@ class AssignToSliceViolation(ASTViolation): @final -class RaiseSystemErrorViolation(ASTViolation): +class RaiseSystemExitViolation(ASTViolation): """ - Forbid raising :exc:`SystemError`. + Forbid raising :exc:`SystemExit`. Reasoning: For consistency. @@ -2508,11 +2508,11 @@ class RaiseSystemErrorViolation(ASTViolation): sys.exit(code) # Wrong: - raise SystemError(code) + raise SystemExit(code) .. versionadded:: 1.0.0 """ - error_template = 'Found `raise SystemError`, instead of using `sys.exit`' + error_template = 'Found `raise SystemExit`, instead of using `sys.exit`' code = 363 diff --git a/wemake_python_styleguide/visitors/ast/keywords.py b/wemake_python_styleguide/visitors/ast/keywords.py index 0b2630238..fb548e7d6 100644 --- a/wemake_python_styleguide/visitors/ast/keywords.py +++ b/wemake_python_styleguide/visitors/ast/keywords.py @@ -26,7 +26,7 @@ InconsistentReturnViolation, InconsistentYieldViolation, IncorrectYieldFromTargetViolation, - RaiseSystemErrorViolation, + RaiseSystemExitViolation, ) from wemake_python_styleguide.visitors.base import BaseNodeVisitor from wemake_python_styleguide.visitors.decorators import alias @@ -41,7 +41,7 @@ class WrongRaiseVisitor(BaseNodeVisitor): """Finds wrong ``raise`` keywords.""" - _system_error_name: ClassVar[str] = 'SystemError' + _system_error_name: ClassVar[str] = 'SystemExit' def visit_Raise(self, node: ast.Raise) -> None: """Checks how ``raise`` keyword is used.""" @@ -57,7 +57,7 @@ def _check_raise_from_itself(self, node: ast.Raise) -> None: def _check_raise_system_error(self, node: ast.Raise) -> None: if get_exception_name(node) == self._system_error_name: - self.add_violation(RaiseSystemErrorViolation(node)) + self.add_violation(RaiseSystemExitViolation(node)) @final