From 726a985b3bd15294e64d6417901bc1106d6bced7 Mon Sep 17 00:00:00 2001 From: "joe.sharman" Date: Wed, 13 Mar 2024 21:31:38 +0000 Subject: [PATCH] fix: replace assert and AssertionError with appropriate exceptions # Description - Replaces assert and AssertionError with built-in exceptions - Amended tests to reference new exception types # Related Issue(s) Closes https://github.com/delta-io/delta-rs/issues/2242 --- python/deltalake/writer.py | 12 +++++++----- python/tests/test_writer.py | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/python/deltalake/writer.py b/python/deltalake/writer.py index 4a61254a23..feb1ff720e 100644 --- a/python/deltalake/writer.py +++ b/python/deltalake/writer.py @@ -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 diff --git a/python/tests/test_writer.py b/python/tests/test_writer.py index d41ef93225..9d3dbeb77f 100644 --- a/python/tests/test_writer.py +++ b/python/tests/test_writer.py @@ -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): @@ -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,