Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions dataframely/_base_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ def __new__(
f"Did you forget to add parentheses?"
)

# Check for pl.DataType instance or type (e.g., pl.String() or pl.String instead of dy.String())
if isinstance(value, pl.DataType) or (
isinstance(value, type) and issubclass(value, pl.DataType)
):
value_type = "instance" if isinstance(value, pl.DataType) else "type"
example = (
"pl.String()" if isinstance(value, pl.DataType) else "pl.String"
)
raise TypeError(
f"Schema member '{attr}' is a polars DataType {value_type}. "
f"Use dataframely column types (e.g., dy.String()) instead of polars types (e.g., {example})."
)

return cls

if not TYPE_CHECKING:
Expand Down
16 changes: 16 additions & 0 deletions tests/schema/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,19 @@ def test_user_error_column_type_not_instance() -> None:
class MySchemaWithColumnTypeNotInstance(dy.Schema):
a = dy.Int32(nullable=False, primary_key=True)
b = dy.Float64 # User error: Forgot parentheses!


def test_user_error_polars_datatype_instance() -> None:
with pytest.raises(TypeError, match="polars DataType instance"):

class MySchemaWithPolarsDataTypeInstance(dy.Schema):
a = dy.Int32(nullable=False)
b = pl.String() # User error: Used pl.String() instead of dy.String()


def test_user_error_polars_datatype_type() -> None:
with pytest.raises(TypeError, match="polars DataType type"):

class MySchemaWithPolarsDataTypeType(dy.Schema):
a = dy.Int32(nullable=False)
b = pl.String # User error: Used pl.String instead of dy.String()
Loading