From 4d3e3f44a1dbcf7671d93b9062f7c968d29a3588 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 3 Jul 2026 12:28:39 +0200 Subject: [PATCH] fix(checker): correctly check length of frame/column --- midas/checker/frames/column_methods.py | 25 ++++++++++++++++-------- midas/checker/frames/frame_methods.py | 27 ++++++++++++++++---------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/midas/checker/frames/column_methods.py b/midas/checker/frames/column_methods.py index 02de5a3..95bce9e 100644 --- a/midas/checker/frames/column_methods.py +++ b/midas/checker/frames/column_methods.py @@ -355,6 +355,21 @@ class ColumnMethodRegistry(MethodRegistry[Call]): def _assert_same_length(self, call_expr: p.Expr, column1: p.Expr, column2: p.Expr): func_name: str = "__midas_column_same_length__" + + # Efficiently compute length + # https://stackoverflow.com/a/15943975/11109181 + def len_of_col(col: ast.expr) -> ast.expr: + return ast.Call( + func=ast.Name(id="len"), + args=[ + ast.Attribute( + value=col, + attr="index", + ) + ], + keywords=[], + ) + self.assertions.define( func_name, ast.FunctionDef( @@ -372,16 +387,10 @@ class ColumnMethodRegistry(MethodRegistry[Call]): body=[ ast.Return( value=ast.Compare( - left=ast.Attribute( - value=ast.Name(id="column1"), - attr="size", - ), + left=len_of_col(ast.Name(id="column1")), ops=[ast.Eq()], comparators=[ - ast.Attribute( - value=ast.Name(id="column2"), - attr="size", - ) + len_of_col(ast.Name(id="column2")), ], ) ) diff --git a/midas/checker/frames/frame_methods.py b/midas/checker/frames/frame_methods.py index bd9e3ee..1be481f 100644 --- a/midas/checker/frames/frame_methods.py +++ b/midas/checker/frames/frame_methods.py @@ -434,6 +434,21 @@ class FrameMethodRegistry(MethodRegistry[Call]): def _assert_same_length(self, call_expr: p.Expr, frame1: p.Expr, frame2: p.Expr): func_name: str = "__midas_frame_same_length__" + + # Efficiently compute length + # https://stackoverflow.com/a/15943975/11109181 + def len_of_df(df: ast.expr) -> ast.expr: + return ast.Call( + func=ast.Name(id="len"), + args=[ + ast.Attribute( + value=df, + attr="index", + ) + ], + keywords=[], + ) + self.assertions.define( func_name, ast.FunctionDef( @@ -451,17 +466,9 @@ class FrameMethodRegistry(MethodRegistry[Call]): body=[ ast.Return( value=ast.Compare( - left=ast.Attribute( - value=ast.Name(id="frame1"), - attr="size", - ), + left=len_of_df(ast.Name(id="frame1")), ops=[ast.Eq()], - comparators=[ - ast.Attribute( - value=ast.Name(id="frame2"), - attr="size", - ) - ], + comparators=[len_of_df(ast.Name(id="frame2"))], ) ) ],