Skip to content

Commit

Permalink
fix: replace assert and AssertionError with appropriate exceptions (#…
Browse files Browse the repository at this point in the history
…2286)

# Description

- Replaces assert and AssertionError with built-in exceptions
- Amended tests to reference new exception types
- Following conventions in file of using built-in exceptions rather than
custom exceptions

# Related Issue(s)
Closes #2242

# Documentation

<!---
Share links to useful documentation
--->
  • Loading branch information
joe-sharman authored Mar 13, 2024
1 parent 6b39cb9 commit 25962a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions python/deltalake/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,16 +354,18 @@ def sort_arrow_schema(schema: pa.schema) -> pa.schema:
f"Data schema:\n{schema}\nTable Schema:\n{table.schema().to_pyarrow(as_large_types=large_dtypes)}"
)
if mode == "error":
raise AssertionError("DeltaTable already exists.")
raise FileExistsError(
"Delta table already exists, write mode set to error."
)
elif mode == "ignore":
return

current_version = table.version()

if partition_by:
assert (
partition_by == table.metadata().partition_columns
), f"Partition columns should be {table.metadata().partition_columns} but is {partition_by}"
if partition_by and partition_by != table.metadata().partition_columns:
raise ValueError(
f"Partition columns should be {table.metadata().partition_columns} but is {partition_by}"
)
else:
partition_by = table.metadata().partition_columns

Expand Down
4 changes: 2 additions & 2 deletions python/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def test_write_modes(tmp_path: pathlib.Path, sample_data: pa.Table, engine):
assert DeltaTable(tmp_path).to_pyarrow_table() == sample_data

if engine == "pyarrow":
with pytest.raises(AssertionError):
with pytest.raises(FileExistsError):
write_deltalake(tmp_path, sample_data, mode="error")
elif engine == "rust":
with pytest.raises(DeltaError):
Expand Down Expand Up @@ -481,7 +481,7 @@ def test_fails_wrong_partitioning(
existing_table: DeltaTable, sample_data: pa.Table, engine
):
if engine == "pyarrow":
with pytest.raises(AssertionError):
with pytest.raises(ValueError):
write_deltalake(
existing_table,
sample_data,
Expand Down

0 comments on commit 25962a0

Please sign in to comment.