Skip to content

Commit

Permalink
SystemExit and not SystemError (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn authored Dec 27, 2024
1 parent 2f04255 commit b252227
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/noqa/noqa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from wemake_python_styleguide.violations.consistency import (
RaiseSystemErrorViolation,
RaiseSystemExitViolation,
)
from wemake_python_styleguide.visitors.ast.keywords import WrongRaiseVisitor

Expand All @@ -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(
Expand All @@ -23,24 +23,25 @@ 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(
'code',
[
'NotImplementedError',
'NotImplementedError()',
'CustomSystemError',
'CustomSystemError()',
'custom.SystemError',
'custom.SystemError()',
'CustomSystemExit',
'CustomSystemExit()',
'custom.SystemExit',
'custom.SystemExit()',
'SystemError',
],
)
def test_raise_good_errors(
Expand Down
12 changes: 6 additions & 6 deletions wemake_python_styleguide/violations/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
RawStringNotNeededViolation
InconsistentComprehensionViolation
AssignToSliceViolation
RaiseSystemErrorViolation
RaiseSystemExitViolation
Consistency checks
------------------
Expand Down Expand Up @@ -154,7 +154,7 @@
.. autoclass:: RawStringNotNeededViolation
.. autoclass:: InconsistentComprehensionViolation
.. autoclass:: AssignToSliceViolation
.. autoclass:: RaiseSystemErrorViolation
.. autoclass:: RaiseSystemExitViolation
"""

Expand Down Expand Up @@ -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.
Expand All @@ -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
6 changes: 3 additions & 3 deletions wemake_python_styleguide/visitors/ast/keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."""
Expand All @@ -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
Expand Down

0 comments on commit b252227

Please sign in to comment.