Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental/use standalone visitor #309

Merged
merged 4 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fastapi==0.100.0
iniconfig==2.0.0
isort==5.12.0
mypy-extensions==1.0.0
mypy==1.6.0
mypy==1.7.0
packaging==23.1
pathspec==0.11.2
platformdirs==3.11.0
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ classifiers = [

[tool.poetry.dependencies]
python = ">=3.10"
mypy = ">=0.981,<1.7.0"
mypy = ">=0.981"
tomli = {version = "^2.0.1", python = "<3.11"}

[tool.poetry.dev-dependencies]
Expand Down Expand Up @@ -51,7 +51,8 @@ allow_untyped_defs = true
[tool.coverage.run]
omit = [
"refurb/__main__.py",
"refurb/gen.py"
"refurb/gen.py",
"refurb/visitor/traverser.py"
]

[tool.coverage.report]
Expand Down Expand Up @@ -118,6 +119,7 @@ target-version = "py310"
[tool.ruff.per-file-ignores]
"test/*" = ["ANN201", "ARG001", "E501", "TCH001", "TCH002"]
"refurb/main.py" = ["E501"]
"refurb/visitor/traverser.py" = ["ALL"]
"test/e2e/gbk.py" = ["FURB105"]

[build-system]
Expand Down
8 changes: 4 additions & 4 deletions refurb/checks/builtin/no_slice_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
SliceExpr,
Var,
)
from mypy.traverser import TraverserVisitor

from refurb.error import Error
from refurb.visitor import TraverserVisitor


@dataclass
Expand Down Expand Up @@ -58,11 +58,11 @@ def __init__(self, errors: list[Error]) -> None:
self.errors = errors

def visit_assignment_stmt(self, node: AssignmentStmt) -> None:
node.rvalue.accept(self)
self.accept(node.rvalue)

def visit_del_stmt(self, node: DelStmt) -> None:
if not isinstance(node.expr, IndexExpr):
node.expr.accept(self)
self.accept(node.expr)

def visit_index_expr(self, node: IndexExpr) -> None:
index = node.index
Expand All @@ -83,4 +83,4 @@ def visit_index_expr(self, node: IndexExpr) -> None:


def check(node: MypyFile, errors: list[Error]) -> None:
node.accept(SliceExprVisitor(errors))
SliceExprVisitor(errors).accept(node)
4 changes: 2 additions & 2 deletions refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
TupleExpr,
UnaryExpr,
)
from mypy.traverser import TraverserVisitor

from refurb.error import Error
from refurb.visitor import TraverserVisitor


def extract_binary_oper(
Expand Down Expand Up @@ -244,7 +244,7 @@ def is_name_unused_in_contexts(name: NameExpr, contexts: list[Node]) -> bool:

for ctx in contexts:
visitor = ReadCountVisitor(name)
ctx.accept(visitor)
visitor.accept(ctx)

if visitor.was_read:
return False
Expand Down
11 changes: 5 additions & 6 deletions refurb/checks/readability/no_len_cmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
Var,
WhileStmt,
)
from mypy.traverser import TraverserVisitor

from refurb.error import Error
from refurb.visitor import METHOD_NODE_MAPPINGS
from refurb.visitor import METHOD_NODE_MAPPINGS, TraverserVisitor


@dataclass
Expand Down Expand Up @@ -204,24 +203,24 @@ def check_condition_like(
match node:
case IfStmt(expr=exprs):
for expr in exprs:
expr.accept(visitor)
visitor.accept(expr)

case MatchStmt(guards=guards) if guards:
for guard in guards:
if guard:
guard.accept(visitor)
visitor.accept(guard)

case (
GeneratorExpr(condlists=conditions)
| DictionaryComprehension(condlists=conditions)
):
for condition in conditions:
for expr in condition:
expr.accept(visitor)
visitor.accept(expr)

case (
ConditionalExpr(cond=expr)
| WhileStmt(expr=expr)
| AssertStmt(expr=expr)
):
expr.accept(visitor)
visitor.accept(expr)
4 changes: 2 additions & 2 deletions refurb/checks/readability/use_comprehension.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def check_stmts(stmts: list[Statement], errors: list[Error]) -> None:
and not isinstance(if_expr, AssignmentExpr)
):
name_visitor = ReadCountVisitor(name)
stmt.accept(name_visitor)
name_visitor.accept(stmt)

if name_visitor.read_count == 1:
errors.append(ErrorInfo.from_node(assign))
Expand All @@ -101,7 +101,7 @@ def check_stmts(stmts: list[Statement], errors: list[Error]) -> None:
and name.fullname == assign.fullname
):
name_visitor = ReadCountVisitor(name)
stmt.accept(name_visitor)
name_visitor.accept(stmt)

if name_visitor.read_count == 1:
errors.append(ErrorInfo.from_node(assign))
Expand Down
2 changes: 1 addition & 1 deletion refurb/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def run_refurb(settings: Settings) -> Sequence[Error | str]:

# See: https://github.com/dosisod/refurb/issues/302
with suppress(RecursionError):
tree.accept(visitor)
visitor.accept(tree)

elapsed = time.time() - start

Expand Down
3 changes: 2 additions & 1 deletion refurb/visitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .mapping import METHOD_NODE_MAPPINGS
from .traverser import TraverserVisitor
from .visitor import RefurbVisitor

__all__ = ("METHOD_NODE_MAPPINGS", "RefurbVisitor")
__all__ = ("METHOD_NODE_MAPPINGS", "RefurbVisitor", "TraverserVisitor")
Loading