Skip to content
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
2 changes: 2 additions & 0 deletions python/fastexcel/_fastexcel.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ class CellError:
def offset_position(self) -> tuple[int, int]: ...
@property
def detail(self) -> str: ...
def __repr__(self) -> str: ...

class CellErrors:
@property
def errors(self) -> list[CellError]: ...
def __repr__(self) -> str: ...

class _ExcelSheet:
@property
Expand Down
10 changes: 10 additions & 0 deletions python/tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
from .utils import path_for_fixture


def test_cell_error_repr() -> None:
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-type-errors.xlsx"))
_, cell_errors = excel_reader.load_sheet(0, dtypes={"Column": "int"}).to_arrow_with_errors()
assert cell_errors is not None
assert (
repr(cell_errors.errors[0])
== """CellError(position=(2, 0), offset_position=(1, 0), row_offset=1, detail="Expected int but got 'String(\\"foo\\")'")""" # noqa: E501
)


def test_read_excel_bad_type() -> None:
expected_message = "source must be a string or bytes"
with pytest.raises(fastexcel.InvalidParametersError, match=expected_message):
Expand Down
15 changes: 15 additions & 0 deletions src/types/excelsheet/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ impl CellError {
let (row, col) = self.position;
(row - self.row_offset, col)
}

pub fn __repr__(&self) -> String {
let (row, col) = self.position;
let (offset_row, offset_col) = self.offset_position();
format!(
"CellError(position=({row}, {col}), offset_position=({offset_row}, {offset_col}), row_offset={row_offset}, detail={detail:?})",
row_offset = self.row_offset,
detail = &self.detail,
)
}
}

#[pyclass]
Expand All @@ -155,6 +165,11 @@ impl CellErrors {
pub fn errors<'p>(&'p self, _py: Python<'p>) -> Vec<CellError> {
self.errors.clone()
}

pub fn __repr__(&self) -> String {
let errors_repr: Vec<String> = self.errors.iter().map(|e| e.__repr__()).collect();
format!("CellErrors(errors=[{}])", errors_repr.join(", "))
}
}

impl FromPyObject<'_> for SkipRows {
Expand Down