Skip to content

Commit c11386a

Browse files
authored
feat(python): implement __repr__ for CellError and CellErrors (#461)
relates to #458 Signed-off-by: Luka Peschke <luka.peschke@toucantoco.com>
1 parent 9a03504 commit c11386a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

python/fastexcel/_fastexcel.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ class CellError:
7676
def offset_position(self) -> tuple[int, int]: ...
7777
@property
7878
def detail(self) -> str: ...
79+
def __repr__(self) -> str: ...
7980

8081
class CellErrors:
8182
@property
8283
def errors(self) -> list[CellError]: ...
84+
def __repr__(self) -> str: ...
8385

8486
class _ExcelSheet:
8587
@property

python/tests/test_errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66
from .utils import path_for_fixture
77

88

9+
def test_cell_error_repr() -> None:
10+
excel_reader = fastexcel.read_excel(path_for_fixture("fixture-type-errors.xlsx"))
11+
_, cell_errors = excel_reader.load_sheet(0, dtypes={"Column": "int"}).to_arrow_with_errors()
12+
assert cell_errors is not None
13+
assert (
14+
repr(cell_errors.errors[0])
15+
== """CellError(position=(2, 0), offset_position=(1, 0), row_offset=1, detail="Expected int but got 'String(\\"foo\\")'")""" # noqa: E501
16+
)
17+
18+
919
def test_read_excel_bad_type() -> None:
1020
expected_message = "source must be a string or bytes"
1121
with pytest.raises(fastexcel.InvalidParametersError, match=expected_message):

src/types/excelsheet/python.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,16 @@ impl CellError {
142142
let (row, col) = self.position;
143143
(row - self.row_offset, col)
144144
}
145+
146+
pub fn __repr__(&self) -> String {
147+
let (row, col) = self.position;
148+
let (offset_row, offset_col) = self.offset_position();
149+
format!(
150+
"CellError(position=({row}, {col}), offset_position=({offset_row}, {offset_col}), row_offset={row_offset}, detail={detail:?})",
151+
row_offset = self.row_offset,
152+
detail = &self.detail,
153+
)
154+
}
145155
}
146156

147157
#[pyclass]
@@ -155,6 +165,11 @@ impl CellErrors {
155165
pub fn errors<'p>(&'p self, _py: Python<'p>) -> Vec<CellError> {
156166
self.errors.clone()
157167
}
168+
169+
pub fn __repr__(&self) -> String {
170+
let errors_repr: Vec<String> = self.errors.iter().map(|e| e.__repr__()).collect();
171+
format!("CellErrors(errors=[{}])", errors_repr.join(", "))
172+
}
158173
}
159174

160175
impl FromPyObject<'_> for SkipRows {

0 commit comments

Comments
 (0)