Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(python): improved exceptions on attempt to use invalid schema/dtypes #6653

Merged
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
22 changes: 12 additions & 10 deletions py-polars/polars/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,11 +687,14 @@ def dtype_to_py_type(dtype: PolarsDataType) -> PythonDataType:

def is_polars_dtype(data_type: Any, include_unknown: bool = False) -> bool:
"""Indicate whether the given input is a Polars dtype, or dtype specialisation."""
if data_type == Unknown:
# does not represent a realisable dtype, so ignore by default
return include_unknown
else:
return isinstance(data_type, (DataType, DataTypeClass))
try:
if data_type == Unknown:
# does not represent a realisable dtype, so ignore by default
return include_unknown
else:
return isinstance(data_type, (DataType, DataTypeClass))
except ValueError:
return False


@overload
Expand Down Expand Up @@ -733,12 +736,11 @@ def py_type_to_dtype(
data_type = possible_types[0]
try:
return DataTypeMappings.PY_TYPE_TO_DTYPE[data_type]
except KeyError: # pragma: no cover
except (KeyError, TypeError): # pragma: no cover
if not raise_unmatched:
return None
raise NotImplementedError(
f"Conversion of Python data type '{data_type}' to Polars data type not"
" implemented."
raise ValueError(
f"Cannot infer dtype from '{data_type}' (type: {type(data_type).__name__})"
) from None


Expand Down Expand Up @@ -780,7 +782,7 @@ def numpy_char_code_to_dtype(dtype: str) -> PolarsDataType:
try:
return DataTypeMappings.NUMPY_CHAR_CODE_TO_DTYPE[dtype]
except KeyError: # pragma: no cover
raise NotImplementedError(
raise ValueError(
f"Cannot parse numpy data type {dtype} into Polars data type."
) from None

Expand Down
5 changes: 4 additions & 1 deletion py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,17 @@ def _unpack_schema(
column_dtypes = {
lookup.get(col[0], col[0]): col[1]
for col in (schema or [])
if not isinstance(col, str) and col[1]
if not isinstance(col, str) and col[1] is not None
}
if schema_overrides:
column_dtypes.update(schema_overrides)
if schema and include_overrides_in_columns:
column_names = column_names + [
col for col in column_dtypes if col not in column_names
]
for col, dtype in column_dtypes.items():
if not is_polars_dtype(dtype, include_unknown=True) and dtype is not None:
column_dtypes[col] = py_type_to_dtype(dtype)

return (
column_names or None, # type: ignore[return-value]
Expand Down