diff --git a/dataframely/_base_schema.py b/dataframely/_base_schema.py index a0d4594..81e6cb1 100644 --- a/dataframely/_base_schema.py +++ b/dataframely/_base_schema.py @@ -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: diff --git a/tests/schema/test_base.py b/tests/schema/test_base.py index 36cd085..15d6289 100644 --- a/tests/schema/test_base.py +++ b/tests/schema/test_base.py @@ -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()