diff --git a/CHANGELOG.md b/CHANGELOG.md index 50e5966..fc33a51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. ## [Unreleased] - Adds an optional parameter to DataTable to disable rendering of string data as Rich Markup. +- Fixes a bug where None could be casted to a string and displayed as "None" ([tconbeer/harlequin#658](https://github.com/tconbeer/harlequin/issues/658)) ## [0.9.0] - 2024-07-23 diff --git a/src/textual_fastdatatable/backend.py b/src/textual_fastdatatable/backend.py index ff57908..39f5db3 100644 --- a/src/textual_fastdatatable/backend.py +++ b/src/textual_fastdatatable/backend.py @@ -252,7 +252,10 @@ def from_pydict( except (pal.ArrowInvalid, pal.ArrowTypeError): # one or more fields has mixed types, like int and # string. Cast all to string for safety - new_data = {k: [str(val) for val in v] for k, v in data.items()} + new_data = { + k: [str(val) if val is not None else None for val in v] + for k, v in data.items() + } tbl = pa.Table.from_pydict(new_data) return cls(tbl, max_rows=max_rows) diff --git a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr index 25b8d4e..e47cac6 100644 --- a/tests/snapshot_tests/__snapshots__/test_snapshots.ambr +++ b/tests/snapshot_tests/__snapshots__/test_snapshots.ambr @@ -1967,6 +1967,165 @@ ''' # --- +# name: test_datatable_null_mixed_cols + ''' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + TableApp + + + + + + + + + +  lane   swimmer               country        time   +  3      Li Zhuhao             China          51.26  +  eight ∅ null France         51.58  +  seven  Tom Shields           United States ∅  +  1      Aleksandr Sadovnikov  Russia         51.84  + ∅  Darren Burns          Scotland       51.84  + + + + + + + + + + + + + + + + + + + + + + + ''' +# --- # name: test_datatable_range_cursor_render ''' diff --git a/tests/snapshot_tests/snapshot_apps/data_table_null_mixed_cols.py b/tests/snapshot_tests/snapshot_apps/data_table_null_mixed_cols.py new file mode 100644 index 0000000..9158364 --- /dev/null +++ b/tests/snapshot_tests/snapshot_apps/data_table_null_mixed_cols.py @@ -0,0 +1,22 @@ +from textual.app import App, ComposeResult +from textual_fastdatatable import ArrowBackend, DataTable + +ROWS = [ + ("lane", "swimmer", "country", "time"), + (3, "Li Zhuhao", "China", 51.26), + ("eight", None, "France", 51.58), + ("seven", "Tom Shields", "United States", None), + (1, "Aleksandr Sadovnikov", "Russia", 51.84), + (None, "Darren Burns", "Scotland", 51.84), +] + + +class TableApp(App): + def compose(self) -> ComposeResult: + backend = ArrowBackend.from_records(ROWS, has_header=True) + yield DataTable(backend=backend, null_rep="[dim]∅ null[/]") + + +app = TableApp() +if __name__ == "__main__": + app.run() diff --git a/tests/snapshot_tests/test_snapshots.py b/tests/snapshot_tests/test_snapshots.py index 75758bc..ad8a235 100644 --- a/tests/snapshot_tests/test_snapshots.py +++ b/tests/snapshot_tests/test_snapshots.py @@ -25,6 +25,10 @@ def test_datatable_no_render_markup(snap_compare: Callable) -> None: assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_no_render_markup.py") +def test_datatable_null_mixed_cols(snap_compare: Callable) -> None: + assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_null_mixed_cols.py") + + def test_datatable_range_cursor_render(snap_compare: Callable) -> None: press = ["right", "down", "shift+right", "shift+down", "shift+down"] assert snap_compare(SNAPSHOT_APPS_DIR / "data_table_range_cursor.py", press=press)