Skip to content

Commit 503548b

Browse files
authored
fix: python schema repr does not truncate output (#628)
Fixes #466. ``` >>> import nanoarrow as na >>> url = "https://github.com/apache/arrow-experiments/raw/main/data/arrow-commits/arrow-commits.arrows" >>> schema = na.ArrayStream.from_url(url).schema >>> schema <Schema> non-nullable struct<commit: string, time: timestamp('us', 'UTC'), files: int32, merge: bool, message: string> ```
1 parent 08d14b3 commit 503548b

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

python/src/nanoarrow/_repr_utils.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ def make_class_label(obj, module=None):
2727

2828

2929
def c_schema_to_string(obj, max_char_width=80):
30-
max_char_width = max(max_char_width, 10)
31-
c_schema_string = obj._to_string(recursive=True, max_chars=max_char_width + 1)
32-
if len(c_schema_string) > max_char_width:
33-
return c_schema_string[: (max_char_width - 3)] + "..."
30+
c_schema_string = ""
31+
if max_char_width == 0:
32+
c_schema_string = obj._to_string(recursive=True, max_chars=max_char_width)
3433
else:
35-
return c_schema_string
34+
max_char_width = max(max_char_width, 10)
35+
c_schema_string = obj._to_string(recursive=True, max_chars=max_char_width + 1)
36+
if len(c_schema_string) > max_char_width:
37+
c_schema_string = c_schema_string[: (max_char_width - 3)] + "..."
38+
return c_schema_string
3639

3740

3841
def metadata_repr(obj, indent=0, max_char_width=80):

python/src/nanoarrow/schema.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,8 @@ def serialize(self, dst=None) -> Union[bytes, None]:
498498
writer.write_stream(empty)
499499

500500
def __repr__(self) -> str:
501-
return _schema_repr(self)
501+
# An empty max_char_width prints the entire schema
502+
return _schema_repr(self, max_char_width=0)
502503

503504
def __arrow_c_schema__(self):
504505
return self._c_schema.__arrow_c_schema__()
@@ -1302,10 +1303,9 @@ def _schema_repr(obj, max_char_width=80, prefix="<Schema> ", include_metadata=Tr
13021303

13031304
modifiers_str = " ".join(modifiers)
13041305
first_line_prefix = f"{prefix}{modifiers_str}"
1306+
max_char_width = max(max_char_width - len(first_line_prefix), 0)
13051307

1306-
schema_str = _repr_utils.c_schema_to_string(
1307-
obj._c_schema, max_char_width - len(first_line_prefix)
1308-
)
1308+
schema_str = _repr_utils.c_schema_to_string(obj._c_schema, max_char_width)
13091309
lines.append(f"{first_line_prefix}{schema_str}")
13101310

13111311
if include_metadata:

python/tests/test_schema.py

+18
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,21 @@ def test_schema_serialize():
259259
schema.serialize(out)
260260
schema_roundtrip = na.ArrayStream.from_readable(out.getvalue()).schema
261261
assert repr(schema_roundtrip) == repr(schema)
262+
263+
264+
def test_schema_repr():
265+
schema = na.struct(
266+
{
267+
"col1": na.int32(),
268+
"col2": na.int16(),
269+
"col3": na.string(),
270+
"col4": na.timestamp(unit=na.TimeUnit.SECOND),
271+
},
272+
nullable=False,
273+
)
274+
275+
assert repr(schema) == (
276+
"<Schema> non-nullable struct"
277+
"<col1: int32, col2: int16, col3: string, "
278+
"col4: timestamp('s', '')>"
279+
)

0 commit comments

Comments
 (0)