-
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.
- Loading branch information
Showing
7 changed files
with
163 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,36 @@ | ||
from textwrap import dedent | ||
|
||
from core_codemods.remove_future_imports import RemoveFutureImports | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestRemoveFutureImports(BaseIntegrationTest): | ||
codemod = RemoveFutureImports | ||
code_path = "tests/samples/future_imports.py" | ||
|
||
original_code, _ = original_and_expected_from_code_path(code_path, []) | ||
expected_new_code = dedent( | ||
"""\ | ||
from __future__ import annotations | ||
print("HEY") | ||
""" | ||
) | ||
|
||
expected_diff = """\ | ||
--- | ||
+++ | ||
@@ -1,4 +1,3 @@ | ||
-from __future__ import absolute_import | ||
-from __future__ import * | ||
+from __future__ import annotations | ||
print("HEY") | ||
""" | ||
|
||
num_changes = 2 | ||
expected_line_change = "1" | ||
change_description = RemoveFutureImports.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
11 changes: 11 additions & 0 deletions
11
src/core_codemods/docs/pixee_python_remove-future-imports.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,11 @@ | ||
Many older codebases have `__future__` imports for forwards compatibility with features. As of this writing, all but one of those features is now stable in all currently supported versions of Python and so the imports are no longer needed. While such imports are harmless, they are also unnecessary and in most cases you probably just forgot to remove them. | ||
|
||
This codemod removes all such `__future__` imports, preserving only those that are still necessary for forwards compatibility. | ||
|
||
Our changes look like the following: | ||
```diff | ||
import os | ||
-from __future__ import print_function | ||
|
||
print("HELLO") | ||
``` |
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,59 @@ | ||
import libcst as cst | ||
|
||
from codemodder.codemods.api import BaseCodemod, ReviewGuidance | ||
|
||
|
||
DEPRECATED_NAMES = [ | ||
"print_function", | ||
"unicode_literals", | ||
"division", | ||
"absolute_import", | ||
"generators", | ||
"nested_scopes", | ||
"with_statement", | ||
"generator_stop", | ||
] | ||
CURRENT_NAMES = [ | ||
"annotations", | ||
] | ||
|
||
|
||
class RemoveFutureImports(BaseCodemod): | ||
NAME = "remove-future-imports" | ||
SUMMARY = "Remove deprecated `__future__` imports" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
DESCRIPTION = "Remove deprecated `__future__` imports" | ||
REFERENCES = [ | ||
{ | ||
"url": "https://docs.python.org/3/library/__future__.html", | ||
"description": "", | ||
}, | ||
] | ||
|
||
def leave_ImportFrom( | ||
self, original_node: cst.ImportFrom, updated_node: cst.ImportFrom | ||
): | ||
match original_node.module: | ||
case cst.Name(value="__future__"): | ||
match original_node.names: | ||
case cst.ImportStar(): | ||
names = [ | ||
cst.ImportAlias(name=cst.Name(value=name)) | ||
for name in CURRENT_NAMES | ||
] | ||
self.add_change(original_node, self.CHANGE_DESCRIPTION) | ||
return original_node.with_changes(names=names) | ||
|
||
updated_names: list[cst.ImportAlias] = [ | ||
name | ||
for name in original_node.names | ||
if name.name.value not in DEPRECATED_NAMES | ||
] | ||
self.add_change(original_node, self.CHANGE_DESCRIPTION) | ||
return ( | ||
updated_node.with_changes(names=updated_names) | ||
if updated_names | ||
else cst.RemoveFromParent() | ||
) | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import pytest | ||
|
||
from core_codemods.remove_future_imports import RemoveFutureImports, DEPRECATED_NAMES | ||
from tests.codemods.base_codemod_test import BaseCodemodTest | ||
|
||
|
||
class TestRemoveFutureImports(BaseCodemodTest): | ||
codemod = RemoveFutureImports | ||
|
||
@pytest.mark.parametrize("name", DEPRECATED_NAMES) | ||
def test_remove_future_imports(self, tmpdir, name): | ||
original_code = f""" | ||
import os | ||
from __future__ import {name} | ||
print("HEY") | ||
""" | ||
expected_code = """ | ||
import os | ||
print("HEY") | ||
""" | ||
self.run_and_assert(tmpdir, original_code, expected_code) | ||
|
||
def test_update_import_star(self, tmpdir): | ||
original_code = """ | ||
from __future__ import * | ||
""" | ||
expected_code = """ | ||
from __future__ import annotations | ||
""" | ||
self.run_and_assert(tmpdir, original_code, expected_code) | ||
|
||
def test_update_import_deprecated_and_annotations(self, tmpdir): | ||
original_code = """ | ||
from __future__ import print_function, annotations | ||
""" | ||
expected_code = """ | ||
from __future__ import annotations | ||
""" | ||
self.run_and_assert(tmpdir, original_code, expected_code) | ||
|
||
def test_not_from_future(self, tmpdir): | ||
original_code = """ | ||
import os | ||
from __footure__ import print_function | ||
print("HEY") | ||
""" | ||
self.run_and_assert(tmpdir, original_code, original_code) |
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 @@ | ||
from __future__ import absolute_import | ||
from __future__ import * | ||
|
||
print("HEY") |