Skip to content

Commit

Permalink
exclude other references
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrugman committed Dec 10, 2023
1 parent 19012e8 commit 68dda8a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions refurb/checks/readability/fluid_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ReturnStmt,
)

from refurb.checks.common import check_block_like
from refurb.checks.common import check_block_like, ReadCountVisitor
from refurb.error import Error
from refurb.visitor import TraverserVisitor

Expand Down Expand Up @@ -80,9 +80,17 @@ def check_call(node, name: str | None = None) -> bool:
match node:
# Single chain
case CallExpr(callee=MemberExpr(expr=NameExpr(name=x), name=_)):
return name is None or name == x
if name is None or name == x:
# Exclude other references
x_expr = NameExpr(x)
x_expr.fullname = x
visitor = ReadCountVisitor(x_expr)
visitor.accept(node)
return visitor.read_count == 1
return False

# Nested
case CallExpr(callee=MemberExpr(expr=call_node, name=_)):
case CallExpr(callee=MemberExpr(expr=call_node, name=y)):
return check_call(call_node)

return False
Expand All @@ -92,7 +100,7 @@ class NameReferenceVisitor(TraverserVisitor):
name: NameExpr
referenced: bool

def __init__(self, name: NameExpr, stmt: Statement) -> None:
def __init__(self, name: NameExpr, stmt: Statement | None = None) -> None:
super().__init__()
self.name = name
self.stmt = stmt
Expand Down
5 changes: 5 additions & 0 deletions test/data/err_184.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def assign_alternating(df, df2):


# these will not
def _(x):
y = x.m()
return y.operation(*[v for v in y])


def assign_multiple_referenced(df, df2):
df = df.select("column")
result_df = df.select("another_column")
Expand Down

0 comments on commit 68dda8a

Please sign in to comment.