-
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.
add remove global at mod level codemod
- Loading branch information
1 parent
bf7d7cb
commit 53b844f
Showing
7 changed files
with
124 additions
and
0 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,19 @@ | ||
from core_codemods.remove_module_global import RemoveModuleGlobal | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestRemoveModuleGlobal(BaseIntegrationTest): | ||
codemod = RemoveModuleGlobal | ||
code_path = "tests/samples/module_global.py" | ||
original_code, _ = original_and_expected_from_code_path(code_path, []) | ||
expected_new_code = """ | ||
price = 25 | ||
print("hello") | ||
price = 30 | ||
""".lstrip() | ||
expected_diff = '--- \n+++ \n@@ -1,4 +1,3 @@\n price = 25\n print("hello")\n-global price\n price = 30\n' | ||
expected_line_change = "3" | ||
change_description = RemoveModuleGlobal.CHANGE_DESCRIPTION |
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
10 changes: 10 additions & 0 deletions
10
src/core_codemods/docs/pixee_python_remove-module-global.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,10 @@ | ||
Using the `global` keyword is necessary only when you intend to modify a module-level (aka global) variable within a non-global scope, such as within a class or function. It is unnecessary to call `global` at the module-level. | ||
|
||
Our changes look something like this: | ||
|
||
```diff | ||
price = 25 | ||
print("hello") | ||
- global price | ||
price = 30 | ||
``` |
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,22 @@ | ||
import libcst as cst | ||
from libcst.metadata import GlobalScope, ScopeProvider | ||
from typing import Union | ||
from codemodder.codemods.api import BaseCodemod, ReviewGuidance | ||
from codemodder.codemods.utils_mixin import NameResolutionMixin | ||
|
||
|
||
class RemoveModuleGlobal(BaseCodemod, NameResolutionMixin): | ||
NAME = "remove-module-global" | ||
SUMMARY = "Remove Module-level Global Call" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
DESCRIPTION = "Remove Lines with `global` keyword at Module Level" | ||
REFERENCES: list = [] | ||
|
||
def leave_Global( | ||
self, original_node: cst.Global, _ | ||
) -> Union[cst.Global, cst.RemovalSentinel,]: | ||
scope = self.get_metadata(ScopeProvider, original_node) | ||
if isinstance(scope, GlobalScope): | ||
self.report_change(original_node) | ||
return cst.RemovalSentinel.REMOVE | ||
return original_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from textwrap import dedent | ||
from core_codemods.remove_module_global import RemoveModuleGlobal | ||
from tests.codemods.base_codemod_test import BaseCodemodTest | ||
|
||
|
||
class TestJwtDecodeVerify(BaseCodemodTest): | ||
codemod = RemoveModuleGlobal | ||
|
||
def test_name(self): | ||
assert self.codemod.name() == "remove-module-global" | ||
|
||
def test_simple(self, tmpdir): | ||
input_code = """\ | ||
price = 25 | ||
global price | ||
""" | ||
expected = """\ | ||
price = 25 | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected)) | ||
assert len(self.file_context.codemod_changes) == 1 | ||
|
||
def test_reassigned(self, tmpdir): | ||
input_code = """\ | ||
price = 25 | ||
print("hello") | ||
global price | ||
price = 30 | ||
""" | ||
expected = """\ | ||
price = 25 | ||
print("hello") | ||
price = 30 | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected)) | ||
assert len(self.file_context.codemod_changes) == 1 | ||
|
||
def test_attr_call(self, tmpdir): | ||
input_code = """\ | ||
class Price: | ||
pass | ||
PRICE = Price() | ||
global PRICE | ||
PRICE.__repr__ | ||
""" | ||
expected = """\ | ||
class Price: | ||
pass | ||
PRICE = Price() | ||
PRICE.__repr__ | ||
""" | ||
self.run_and_assert(tmpdir, dedent(input_code), dedent(expected)) | ||
assert len(self.file_context.codemod_changes) == 1 | ||
|
||
def test_correct_scope(self, tmpdir): | ||
input_code = """\ | ||
price = 25 | ||
def change_price(): | ||
global price | ||
price = 30 | ||
""" | ||
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 @@ | ||
price = 25 | ||
print("hello") | ||
global price | ||
price = 30 |