Skip to content

[CLI] Drop legacy printing helpers from _cli_utils.py#4285

Merged
hanouticelina merged 3 commits into
mainfrom
cli-cleanup-legacy-cli-utils-helpers
May 28, 2026
Merged

[CLI] Drop legacy printing helpers from _cli_utils.py#4285
hanouticelina merged 3 commits into
mainfrom
cli-cleanup-legacy-cli-utils-helpers

Conversation

@hanouticelina

@hanouticelina hanouticelina commented May 27, 2026

Copy link
Copy Markdown
Collaborator

let's review and merge #4286 before merging this one

Part of #3979 — the final cleanup after every command has been migrated to the out singleton.

Most of the legacy printing helpers in _cli_utils.py had zero remaining callers (verified by grep across src/ and tests/). The one exception was api_object_to_dict, still used to convert dataclasses → dicts before out.table or JSON serialization in 11 callsites — but _output.py already exposes the same conversion as _dataclass_to_dict. So this PR does a 1:1 swap and deletes the dead code.

Changes

  • Switch callers from api_object_to_dict to _dataclass_to_dict (imported from ._output) in discussions.py, models.py, datasets.py (×2), papers.py (×2), spaces.py (×4), collections.py, _file_listing.py.
  • Delete from _cli_utils.py:
    • Printing helpers: print_as_table, print_list_output, _format_cell, _format_value, _to_header, _MAX_CELL_LENGTH, _serialize_value, api_object_to_dict.
    • Legacy format types: OutputFormat (the old table | json enum), _set_output_mode, FormatOpt, FormatWithAutoOpt, QuietOpt.
    • Now-orphaned top-level imports: json, dataclasses, datetime, tabulate, Union.
  • Tests: update tests/test_cli_output.py to import OutputFormatWithAuto from ._output directly instead of via the _cli_utils re-export.

Net diff: +23 / -188 across 9 files. No behavior change.

Verification

$ pytest tests/test_cli_output.py tests/test_cli.py
======================= 278 passed, 7 warnings in 12.00s =======================

Smoke test on a list command that goes through the new path:

$ hf models ls --limit 2 --format human
Hint: Use `--no-truncate` or `--format json` to display full values.
ID   CREATED_AT DOWNLOADS LIBRARY_NAME LIKES PIPELINE_TAG PRIVATE TAGS TRENDING_SCORE
---- ---------- --------- ------------ ----- ------------ ------- ---- --------------
b... 2026-05-15 1908      Lance        901   any-to-any           L... 440
m... 2026-05-21 0         diffusers    333                        d... 323

$ hf models ls --limit 2 --format json
[
    {
        "id": "bytedance-research/Lance",
        "created_at": "2026-05-15T08:05:57+00:00",
        ...
    },
    ...
]

Note

Low Risk
Mechanical refactor with tests passing; only risk is if any external code imported removed symbols from _cli_utils.

Overview
This PR finishes CLI output consolidation after migration to the out singleton: it removes dead legacy printing code from _cli_utils.py and routes all dataclass→dict conversion through _output._dataclass_to_dict.

_cli_utils.py: Deletes unused helpers (print_as_table, print_list_output, api_object_to_dict, legacy OutputFormat / FormatOpt / QuietOpt, etc.) and drops imports that only served that code (tabulate, extra json/dataclasses/datetime usage there).

Call sites: discussions, models, datasets, papers, spaces, collections, and _file_listing now import _dataclass_to_dict from ._output instead of api_object_to_dict from ._cli_utils—same role for out.table() and JSON paths.

Tests: OutputFormatWithAuto is imported from ._output in test_cli_output.py instead of via _cli_utils.

No intended behavior change; large net deletion (~165 lines).

Reviewed by Cursor Bugbot for commit 51107e7. Configure here.

Now that every command has migrated to the `out` singleton, the legacy
helpers in `_cli_utils.py` have no callers. The one exception was
`api_object_to_dict`, which still served dataclass→dict conversion before
`out.table` / JSON serialization — and `_output.py` already exposes the
same helper as `_dataclass_to_dict`. So:

- Switch every caller (discussions, models, datasets, papers, spaces,
  collections, _file_listing) from `api_object_to_dict` to
  `_dataclass_to_dict` from `_output.py`.
- Delete from `_cli_utils.py`: `api_object_to_dict`, `_serialize_value`,
  `print_as_table`, `print_list_output`, `_format_cell`, `_format_value`,
  `_to_header`, `_MAX_CELL_LENGTH`, plus the legacy option types and
  enum (`OutputFormat`, `_set_output_mode`, `FormatOpt`, `FormatWithAutoOpt`,
  `QuietOpt`). Remove now-orphaned imports (`json`, `dataclasses`,
  `datetime`, `tabulate`, `Union`).
- Update `tests/test_cli_output.py` to import `OutputFormatWithAuto`
  from `_output.py` directly instead of via the `_cli_utils.py` re-export.

No behavior change.
@bot-ci-comment

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@Wauplin Wauplin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love cleaning PRs! 🎉 😄

The legacy `OutputFormat` (table | json) was removed in the previous
cleanup PR, freeing up the name. `OutputFormatWithAuto` was always the
"real" enum — covering auto/human/agent/json/quiet — so drop the
disambiguating suffix.

Pure rename, no behavior change. Also removes the now-stale TODO
comment in `_output.py` that referenced the (now deleted) legacy enum.
@hanouticelina hanouticelina merged commit b17ab0c into main May 28, 2026
25 checks passed
@hanouticelina hanouticelina deleted the cli-cleanup-legacy-cli-utils-helpers branch May 28, 2026 08:48
@huggingface-hub-bot

Copy link
Copy Markdown
Contributor

This PR has been shipped as part of the v1.17.0 release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants