-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added exception-without-raise codemod
- Loading branch information
1 parent
906c6ad
commit cef2e83
Showing
9 changed files
with
149 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from core_codemods.exception_without_raise import ExceptionWithoutRaise | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestExceptionWithoutRaise(BaseIntegrationTest): | ||
codemod = ExceptionWithoutRaise | ||
code_path = "tests/samples/exception_without_raise.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
(1, """ raise ValueError\n"""), | ||
], | ||
) | ||
|
||
# fmt: off | ||
expected_diff =( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -1,4 +1,4 @@\n""" | ||
""" try:\n""" | ||
"""- ValueError\n""" | ||
"""+ raise ValueError\n""" | ||
""" except:\n""" | ||
""" pass\n""" | ||
) | ||
# fmt: on | ||
|
||
expected_line_change = "2" | ||
change_description = ExceptionWithoutRaise.CHANGE_DESCRIPTION | ||
num_changed_files = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/core_codemods/docs/pixee_python_exception-without-raise.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
An statement with an exception without raising it is most likely an error. Our changes look something like this: | ||
|
||
```diff | ||
try: | ||
- ValueError | ||
+ raise ValueError | ||
except: | ||
pass | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from typing import Union | ||
import libcst as cst | ||
from codemodder.codemods.api import BaseCodemod | ||
from codemodder.codemods.base_codemod import ReviewGuidance | ||
|
||
from codemodder.codemods.utils_mixin import NameResolutionMixin | ||
from codemodder.utils.utils import list_subclasses | ||
|
||
|
||
class ExceptionWithoutRaise(BaseCodemod, NameResolutionMixin): | ||
NAME = "exception-without-raise" | ||
SUMMARY = "Added raise statement to exception creation" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
DESCRIPTION = SUMMARY | ||
REFERENCES = [ | ||
{ | ||
"url": "https://docs.python.org/3/tutorial/errors.html#raising-exceptions", | ||
"description": "", | ||
}, | ||
] | ||
CHANGE_DESCRIPTION = "Wrapped exception in a raise statement" | ||
|
||
def leave_SimpleStatementLine( | ||
self, | ||
original_node: cst.SimpleStatementLine, | ||
updated_node: cst.SimpleStatementLine, | ||
) -> Union[ | ||
cst.BaseStatement, cst.FlattenSentinel[cst.BaseStatement], cst.RemovalSentinel | ||
]: | ||
match original_node: | ||
case cst.SimpleStatementLine( | ||
body=[cst.Expr(cst.Name() | cst.Attribute() as name)] | ||
): | ||
true_name = self.find_base_name(name) | ||
if true_name: | ||
true_name = true_name.split(".")[-1] | ||
all_exceptions = list_subclasses(BaseException) | ||
if true_name in all_exceptions: | ||
self.report_change(original_node) | ||
return updated_node.with_changes(body=[cst.Raise(exc=name)]) | ||
return updated_node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from tests.codemods.base_codemod_test import BaseCodemodTest | ||
from core_codemods.exception_without_raise import ExceptionWithoutRaise | ||
from textwrap import dedent | ||
|
||
|
||
class TestExceptionWithoutRaise(BaseCodemodTest): | ||
codemod = ExceptionWithoutRaise | ||
|
||
def test_name(self): | ||
assert self.codemod.name() == "exception-without-raise" | ||
|
||
def test_simple(self, tmpdir): | ||
input_code = """\ | ||
ValueError | ||
""" | ||
expected = """\ | ||
raise ValueError | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected)) | ||
assert len(self.file_context.codemod_changes) == 1 | ||
|
||
def test_alias(self, tmpdir): | ||
input_code = """\ | ||
from libcst import CSTValidationError as error | ||
error | ||
""" | ||
expected = """\ | ||
from libcst import CSTValidationError as error | ||
raise error | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected)) | ||
assert len(self.file_context.codemod_changes) == 1 | ||
|
||
def test_unknown_exception(self, tmpdir): | ||
input_code = """\ | ||
Something | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code)) | ||
assert len(self.file_context.codemod_changes) == 0 | ||
|
||
def test_raised_exception(self, tmpdir): | ||
input_code = """\ | ||
raise ValueError | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(input_code)) | ||
assert len(self.file_context.codemod_changes) == 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
try: | ||
ValueError | ||
except: | ||
pass |