From 53af6dfe7bdab0b6ae97fd7703bb31b6efe779ae Mon Sep 17 00:00:00 2001 From: Ryan Eakman <6326532+eakmanrq@users.noreply.github.com> Date: Mon, 3 Jun 2024 21:17:52 -0700 Subject: [PATCH] chore: improve error handling missing dataframe method (#49) --- sqlframe/base/column.py | 6 ++++++ tests/unit/standalone/test_dataframe.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/sqlframe/base/column.py b/sqlframe/base/column.py index 8d089ac..dc0b8a7 100644 --- a/sqlframe/base/column.py +++ b/sqlframe/base/column.py @@ -12,6 +12,7 @@ from sqlglot.optimizer.normalize_identifiers import normalize_identifiers from sqlframe.base.decorators import normalize +from sqlframe.base.exceptions import UnsupportedOperationError from sqlframe.base.types import DataType from sqlframe.base.util import get_func_from_session, quote_preserving_alias_or_name @@ -121,6 +122,11 @@ def __rand__(self, other: ColumnOrLiteral) -> Column: def __ror__(self, other: ColumnOrLiteral) -> Column: return self.inverse_binary_op(exp.Or, other) + def __call__(self, *args, **kwargs): + raise UnsupportedOperationError( + "Tried to call a column which is unexpected. Did you mean to call a method on a DataFrame? If so, make sure the method is typed correctly and is supported. If not, please open an issue requesting support: https://github.com/eakmanrq/sqlframe/issues" + ) + @classmethod def ensure_col(cls, value: t.Optional[t.Union[ColumnOrName, exp.Expression]]) -> Column: col = get_func_from_session("col") diff --git a/tests/unit/standalone/test_dataframe.py b/tests/unit/standalone/test_dataframe.py index 7a97506..b3b42db 100644 --- a/tests/unit/standalone/test_dataframe.py +++ b/tests/unit/standalone/test_dataframe.py @@ -1,7 +1,9 @@ import typing as t +import pytest from sqlglot import expressions as exp +from sqlframe.base.exceptions import UnsupportedOperationError from sqlframe.standalone import functions as F from sqlframe.standalone.dataframe import StandaloneDataFrame @@ -114,3 +116,10 @@ def test_where_expr(standalone_employee: StandaloneDataFrame): df.sql(pretty=False) == "SELECT `a1`.`employee_id` AS `employee_id`, CAST(`a1`.`fname` AS STRING) AS `fname`, CAST(`a1`.`lname` AS STRING) AS `lname`, `a1`.`age` AS `age`, `a1`.`store_id` AS `store_id` FROM VALUES (1, 'Jack', 'Shephard', 37, 1), (2, 'John', 'Locke', 65, 1), (3, 'Kate', 'Austen', 37, 2), (4, 'Claire', 'Littleton', 27, 2), (5, 'Hugo', 'Reyes', 29, 100) AS `a1`(`employee_id`, `fname`, `lname`, `age`, `store_id`) WHERE `a1`.`age` = 37 AND CAST(`a1`.`fname` AS STRING) = 'Jack'" ) + + +def test_missing_method(standalone_employee: StandaloneDataFrame): + with pytest.raises( + UnsupportedOperationError, match="Tried to call a column which is unexpected.*" + ): + standalone_employee.missing_method("blah")