Skip to content

Commit

Permalink
Added new testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
surge119 committed Jun 5, 2024
1 parent 7368528 commit b4908da
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/fixit/rules/deprecated_abc_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ class DeprecatedABCImport(LintRule):
Valid("import collections"),
Valid("import collections.abc"),
Valid("import collections.abc.Container"),
Valid(
"""
class MyTest(collections.Something):
def test(self):
pass
"""
),
]
INVALID = [
Invalid(
Expand Down Expand Up @@ -94,6 +101,18 @@ class DeprecatedABCImport(LintRule):
"from collections import defaultdict\nfrom collections import Container",
expected_replacement="from collections import defaultdict\nfrom collections.abc import Container",
),
Invalid(
"""
class MyTest(collections.Container):
def test(self):
pass
""",
expected_replacement="""
class MyTest(collections.abc.Container):
def test(self):
pass
""",
),
]

def __init__(self) -> None:
Expand Down Expand Up @@ -263,3 +282,50 @@ def visit_ImportAlias(self, node: cst.ImportAlias) -> None:
)
),
)

def visit_ClassDef(self, node: cst.ClassDef) -> None:
# Iterate over inherited Classes and search for `collections.<ABC>`
for base in node.bases:
if m.matches(
base,
m.Arg(
value=m.Attribute(
value=m.Name("collections"),
attr=m.OneOf(*[m.Name(abc) for abc in ABCS]),
)
),
):
# Report + replace `collections.<ABC>` with `collections.abc.<ABC>`
# while keeping the remaining classes.
self.report(
node,
replacement=node.with_changes(
bases=[
(
cst.Arg(
value=cst.Attribute(
value=cst.Attribute(
value=cst.Name("collections"),
attr=cst.Name("abc"),
),
attr=base.value.attr,
),
)
if m.matches(
base,
m.Arg(
value=m.Attribute(
value=m.Name("collections"),
attr=m.OneOf(
*[m.Name(abc) for abc in ABCS]
),
)
),
)
and isinstance(base.value, cst.Attribute)
else base
)
for base in node.bases
]
),
)

0 comments on commit b4908da

Please sign in to comment.