-
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.
New codemod: fix-deprecated-abstractproperty
- Loading branch information
Showing
7 changed files
with
248 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,52 @@ | ||
from textwrap import dedent | ||
|
||
from core_codemods.fix_deprecated_abstractproperty import FixDeprecatedAbstractproperty | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestFixDeprecatedAbstractproperty(BaseIntegrationTest): | ||
codemod = FixDeprecatedAbstractproperty | ||
code_path = "tests/samples/deprecated_abstractproperty.py" | ||
|
||
original_code, _ = original_and_expected_from_code_path(code_path, []) | ||
expected_new_code = dedent( | ||
"""\ | ||
from abc import abstractmethod | ||
import abc | ||
class A: | ||
@property | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
@abstractmethod | ||
def bar(self): | ||
pass | ||
""" | ||
) | ||
|
||
expected_diff = """\ | ||
--- | ||
+++ | ||
@@ -1,8 +1,10 @@ | ||
-from abc import abstractproperty as ap, abstractmethod | ||
+from abc import abstractmethod | ||
+import abc | ||
class A: | ||
- @ap | ||
+ @property | ||
+ @abc.abstractmethod | ||
def foo(self): | ||
pass | ||
""" | ||
|
||
expected_line_change = "5" | ||
change_description = FixDeprecatedAbstractproperty.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
13 changes: 13 additions & 0 deletions
13
src/core_codemods/docs/pixee_python_fix-deprecated-abstractproperty.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,13 @@ | ||
The `@abstractproperty` decorator from `abc` has been [deprecated](https://docs.python.org/3/library/abc.html#abc.abstractproperty) since Python 3.3. This is because it's possible to use `@property` in combination with `@abstractmethod`. | ||
|
||
Our changes look like the following: | ||
```diff | ||
import abc | ||
|
||
class Foo: | ||
- @abc.abstractproperty | ||
+ @property | ||
+ @abc.abstractmethod | ||
def bar(): | ||
... | ||
``` |
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,43 @@ | ||
import libcst as cst | ||
|
||
from codemodder.codemods.api import BaseCodemod, ReviewGuidance | ||
from codemodder.codemods.utils_mixin import NameResolutionMixin | ||
|
||
|
||
class FixDeprecatedAbstractproperty(BaseCodemod, NameResolutionMixin): | ||
NAME = "fix-deprecated-abstractproperty" | ||
SUMMARY = "Replace deprecated abstractproperty" | ||
REVIEW_GUIDANCE = ReviewGuidance.MERGE_WITHOUT_REVIEW | ||
DESCRIPTION = "Replace deprecated abstractproperty with property and abstractmethod" | ||
REFERENCES = [ | ||
{ | ||
"url": "https://docs.python.org/3/library/abc.html#abc.abstractproperty", | ||
"description": "", | ||
}, | ||
] | ||
|
||
def leave_Decorator( | ||
self, original_node: cst.Decorator, updated_node: cst.Decorator | ||
): | ||
if ( | ||
base_name := self.find_base_name(original_node.decorator) | ||
) and base_name == "abc.abstractproperty": | ||
self.add_needed_import("abc") | ||
self.remove_unused_import(original_node) | ||
self.add_change(original_node, self.DESCRIPTION) | ||
return cst.FlattenSentinel( | ||
[ | ||
cst.Decorator( | ||
decorator=cst.Name(value="property"), | ||
trailing_whitespace=updated_node.trailing_whitespace, | ||
), | ||
cst.Decorator( | ||
decorator=cst.Attribute( | ||
value=cst.Name(value="abc"), | ||
attr=cst.Name(value="abstractmethod"), | ||
) | ||
), | ||
] | ||
) | ||
|
||
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,123 @@ | ||
from core_codemods.fix_deprecated_abstractproperty import FixDeprecatedAbstractproperty | ||
from tests.codemods.base_codemod_test import BaseCodemodTest | ||
|
||
|
||
class TestFixDeprecatedAbstractproperty(BaseCodemodTest): | ||
codemod = FixDeprecatedAbstractproperty | ||
|
||
def test_import(self, tmpdir): | ||
original_code = """ | ||
import abc | ||
class A: | ||
@abc.abstractproperty | ||
def foo(self): | ||
pass | ||
""" | ||
new_code = """ | ||
import abc | ||
class A: | ||
@property | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
""" | ||
self.run_and_assert(tmpdir, original_code, new_code) | ||
|
||
def test_import_from(self, tmpdir): | ||
original_code = """ | ||
from abc import abstractproperty | ||
class A: | ||
@abstractproperty | ||
def foo(self): | ||
pass | ||
""" | ||
new_code = """ | ||
import abc | ||
class A: | ||
@property | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
""" | ||
self.run_and_assert(tmpdir, original_code, new_code) | ||
|
||
def test_import_alias(self, tmpdir): | ||
original_code = """ | ||
from abc import abstractproperty as ap | ||
class A: | ||
@ap | ||
def foo(self): | ||
pass | ||
""" | ||
new_code = """ | ||
import abc | ||
class A: | ||
@property | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
""" | ||
self.run_and_assert(tmpdir, original_code, new_code) | ||
|
||
def test_different_abstractproperty(self, tmpdir): | ||
new_code = original_code = """ | ||
from xyz import abstractproperty | ||
class A: | ||
@abstractproperty | ||
def foo(self): | ||
pass | ||
@property | ||
def bar(self): | ||
pass | ||
""" | ||
self.run_and_assert(tmpdir, original_code, new_code) | ||
|
||
def test_preserve_decorator_order(self, tmpdir): | ||
original_code = """ | ||
import abc | ||
class A: | ||
@whatever | ||
@abc.abstractproperty | ||
def foo(self): | ||
pass | ||
""" | ||
new_code = """ | ||
import abc | ||
class A: | ||
@whatever | ||
@property | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
""" | ||
self.run_and_assert(tmpdir, original_code, new_code) | ||
|
||
def test_preserve_comments(self, tmpdir): | ||
original_code = """ | ||
import abc | ||
class A: | ||
@abc.abstractproperty # comment | ||
def foo(self): | ||
pass | ||
""" | ||
new_code = """ | ||
import abc | ||
class A: | ||
@property # comment | ||
@abc.abstractmethod | ||
def foo(self): | ||
pass | ||
""" | ||
self.run_and_assert(tmpdir, original_code, new_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,11 @@ | ||
from abc import abstractproperty as ap, abstractmethod | ||
|
||
|
||
class A: | ||
@ap | ||
def foo(self): | ||
pass | ||
|
||
@abstractmethod | ||
def bar(self): | ||
pass |