Skip to content

Commit

Permalink
feat(python): improved exceptions on attempt to use invalid schema/dt…
Browse files Browse the repository at this point in the history
…ypes (#6653)
  • Loading branch information
alexander-beedie authored Feb 3, 2023
1 parent 66dae4d commit d9fe8ff
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
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 @@ -609,14 +609,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

0 comments on commit d9fe8ff

Please sign in to comment.