diff --git a/docs/checks.md b/docs/checks.md index c3f4668..f364b1e 100644 --- a/docs/checks.md +++ b/docs/checks.md @@ -2180,4 +2180,56 @@ Good: ```python nums = [123, 456] num = str(num[0]) +``` + +## FURB184: `use-fluid-interface` + +Categories: `readability` + +When an API has a Fluent Interface (the ability to chain multiple calls together), you should +chain those calls instead of repeatedly assigning and using the value. +Sometimes a return statement can be written more succinctly: + +Bad: + +```pythonpython +def get_tensors(device: str) -> torch.Tensor: + t1 = torch.ones(2, 1) + t2 = t1.long() + t3 = t2.to(device) + return t3 + +def process(file_name: str): + common_columns = ["col1_renamed", "col2_renamed", "custom_col"] + df = spark.read.parquet(file_name) + df = df \ + .withColumnRenamed('col1', 'col1_renamed') \ + .withColumnRenamed('col2', 'col2_renamed') + df = df \ + .select(common_columns) \ + .withColumn('service_type', F.lit('green')) + return df +``` + +Good: + +```pythonpython +def get_tensors(device: str) -> torch.Tensor: + t3 = ( + torch.ones(2, 1) + .long() + .to(device) + ) + return t3 + +def process(file_name: str): + common_columns = ["col1_renamed", "col2_renamed", "custom_col"] + df = ( + spark.read.parquet(file_name) + .withColumnRenamed('col1', 'col1_renamed') + .withColumnRenamed('col2', 'col2_renamed') + .select(common_columns) + .withColumn('service_type', F.lit('green')) + ) + return df ``` \ No newline at end of file diff --git a/refurb/checks/readability/fluid_interface.py b/refurb/checks/readability/fluid_interface.py index f2d8f34..aa9a45a 100644 --- a/refurb/checks/readability/fluid_interface.py +++ b/refurb/checks/readability/fluid_interface.py @@ -111,10 +111,6 @@ def visit_name_expr(self, node: NameExpr) -> None: if not self.referenced and node.fullname == self.name.fullname: self.referenced = True - @property - def was_referenced(self) -> bool: - return self.referenced - def check_stmts(stmts: list[Statement], errors: list[Error]) -> None: last = ""