Skip to content

Commit

Permalink
chore: improve error handling missing dataframe method (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
eakmanrq authored Jun 4, 2024
1 parent 3c6c7af commit 53af6df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sqlframe/base/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/standalone/test_dataframe.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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")

0 comments on commit 53af6df

Please sign in to comment.