From b6926d1a260f7f02dcdba6327df0198fbcc60f7e Mon Sep 17 00:00:00 2001 From: Pranav Negi Date: Mon, 28 Sep 2026 04:49:19 +0530 Subject: [PATCH 1/2] [datasets sql] Run the DuckDB CLI subprocess with UTF-8 encoding --- src/huggingface_hub/_dataset_viewer.py | 1 + tests/test_cli.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/huggingface_hub/_dataset_viewer.py b/src/huggingface_hub/_dataset_viewer.py index ada15a7f0c3..8c3a773df47 100644 --- a/src/huggingface_hub/_dataset_viewer.py +++ b/src/huggingface_hub/_dataset_viewer.py @@ -147,6 +147,7 @@ def execute(self) -> list[dict[str, Any]]: input=full_query, capture_output=True, text=True, + encoding="utf-8", check=False, ) if result.returncode != 0: diff --git a/tests/test_cli.py b/tests/test_cli.py index 8d9b9766df9..cb62b0b88a0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,7 @@ import json import os import re +import subprocess import sys import warnings from contextlib import contextmanager @@ -2514,6 +2515,24 @@ def test_datasets_sql_json(self, runner: CliRunner) -> None: assert isinstance(item["count"], int) +class TestDuckDBCliRelationEncoding: + def test_execute_uses_utf8_for_duckdb_subprocess(self) -> None: + """DuckDB's stdin/stdout are UTF-8 on every platform; the subprocess must not + use the locale encoding (mojibake or UnicodeDecodeError on Windows cp1252). + + Regression test for https://github.com/huggingface/huggingface_hub/issues/5019. + """ + from huggingface_hub._dataset_viewer import _DuckDBCliRelation + + relation = _DuckDBCliRelation(binary_path="duckdb", query="SELECT chr(233) AS city", setup_statements=[]) + completed = subprocess.CompletedProcess(args=[], returncode=0, stdout='[{"city": "é"}]', stderr="") + with patch("huggingface_hub._dataset_viewer.subprocess.run", return_value=completed) as mock_run: + rows = relation.execute() + + assert rows == [{"city": "é"}] + assert mock_run.call_args.kwargs["encoding"] == "utf-8" + + class TestSpacesLsCommand: def test_spaces_ls(self, runner: CliRunner) -> None: with patch("huggingface_hub.cli.spaces.get_hf_api") as api_cls: From 243947df227fe9c9ce73dac1d797b1fe64568c70 Mon Sep 17 00:00:00 2001 From: Lucain Date: Mon, 28 Sep 2026 12:02:51 +0200 Subject: [PATCH 2/2] Apply batched suggestions from code review Co-authored-by: Lucain --- tests/test_cli.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index cb62b0b88a0..8d9b9766df9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,7 +1,6 @@ import json import os import re -import subprocess import sys import warnings from contextlib import contextmanager @@ -2515,24 +2514,6 @@ def test_datasets_sql_json(self, runner: CliRunner) -> None: assert isinstance(item["count"], int) -class TestDuckDBCliRelationEncoding: - def test_execute_uses_utf8_for_duckdb_subprocess(self) -> None: - """DuckDB's stdin/stdout are UTF-8 on every platform; the subprocess must not - use the locale encoding (mojibake or UnicodeDecodeError on Windows cp1252). - - Regression test for https://github.com/huggingface/huggingface_hub/issues/5019. - """ - from huggingface_hub._dataset_viewer import _DuckDBCliRelation - - relation = _DuckDBCliRelation(binary_path="duckdb", query="SELECT chr(233) AS city", setup_statements=[]) - completed = subprocess.CompletedProcess(args=[], returncode=0, stdout='[{"city": "é"}]', stderr="") - with patch("huggingface_hub._dataset_viewer.subprocess.run", return_value=completed) as mock_run: - rows = relation.execute() - - assert rows == [{"city": "é"}] - assert mock_run.call_args.kwargs["encoding"] == "utf-8" - - class TestSpacesLsCommand: def test_spaces_ls(self, runner: CliRunner) -> None: with patch("huggingface_hub.cli.spaces.get_hf_api") as api_cls: