Skip to content

[Cleanup] formatter.py: remove 3 dead print methods, collapse log-strip regexes - #1856

Open
agu2347 wants to merge 1 commit into
kyegomez:masterfrom
agu2347:cleanup/formatter-dead-code-and-log-regex
Open

[Cleanup] formatter.py: remove 3 dead print methods, collapse log-strip regexes#1856
agu2347 wants to merge 1 commit into
kyegomez:masterfrom
agu2347:cleanup/formatter-dead-code-and-log-regex

Conversation

@agu2347

@agu2347 agu2347 commented Aug 9, 2026

Copy link
Copy Markdown

Closes #1838.

1. Dead print methods (142 lines)

Removed print_progress, print_panel_token_by_token, and print_plan_tree from swarms/utils/formatter.py. Verified 0 callers across swarms/, tests/, and examples/ before removing. Also dropped the now-unused import time and the Progress/SpinnerColumn/TextColumn imports, which only print_progress used.

2. print_markdown redundant alias

print_markdown's markdown_handler branch was byte-identical to print_panel's markdown branch (:434-437 in the original). Its only caller outside this file was tests/utils/test_formatter.py. Rather than delete it and touch the test, I kept it as a one-line documented alias forwarding to print_panel, so existing/external callers keep working unchanged.

3. _clean_output — 8 regexes → 2

The two blocks of four near-identical re.sub calls (differing only by level name: INFO/DEBUG/WARNING/ERROR) collapse to two regexes using an alternation group, per the issue's suggestion.

While doing this I noticed the hardcoded level list only ever covered 4 of loguru's 7 levels — swarms uses logger.success(...) throughout (e.g. graph_workflow.py), so SUCCESS-level lines were passing through _clean_output unstripped, and same for TRACE/CRITICAL. The alternation now includes all 7: INFO|DEBUG|WARNING|ERROR|SUCCESS|TRACE|CRITICAL. This is a small behavior improvement bundled with the consolidation, not just a refactor.

I verified byte-identical output vs. the original 8-regex version for every case the original code path already covered (empty string, plain text with no matching pattern, INFO/DEBUG/WARNING/ERROR lines, "Generated content:" stripping, whitespace collapsing) — the consolidation changes nothing except adding coverage for the 3 previously-missed levels.

Testing

Couldn't fully install the project's heavier dependencies (litellm, mcp, opentelemetry) in my sandbox, so I verified two ways:

  1. Loaded swarms/utils/formatter.py directly via importlib (bypassing swarms/__init__.py, which only formatter.py's own top-level imports are needed for) and ran the new assertions directly against both the original and modified file to confirm identical behavior on shared cases and correct new behavior on the 3 added levels.
  2. Confirmed via git blob SHA-1 that the pushed file content is byte-identical to what was locally verified (no transcription drift).

Added to tests/utils/test_formatter.py:

  • test_clean_output_strips_every_log_level (parametrized over all 7 loguru levels)
  • test_clean_output_handles_empty_string
  • test_dead_print_methods_are_removed
  • test_print_markdown_still_works_as_alias

Also ran ruff check and black --check against the project's actual configured rules (pyproject.toml: line-length = 70, CI pins ruff==0.2.1 with no explicit [tool.ruff.lint] select, so only the default E4,E7,E9,F rules apply) — both clean on the two changed files. black --line-length 70 was applied to the new test additions.

swarms/utils/formatter.py (987 lines) carried three public methods with
zero callers anywhere in swarms/, tests/, or examples/, plus a hardcoded
regex block that collapses to two patterns:

1. Dead print methods (142 lines): print_progress, print_panel_token_by_token,
   and print_plan_tree. Verified 0 hits via repo-wide search.

2. print_markdown was a redundant alias: its markdown_handler branch was
   byte-identical to print_panel's markdown branch, and its only caller
   outside this file was tests/utils/test_formatter.py. Kept as a documented
   one-line alias forwarding to print_panel rather than deleted outright,
   so the existing test and any external callers keep working unchanged.

3. _clean_output's 8 near-identical log-strip regexes (differing only by
   level name: INFO/DEBUG/WARNING/ERROR, x2 blocks) collapse to 2 regexes
   using an alternation group. The alternation also now includes
   SUCCESS/TRACE/CRITICAL, the rest of loguru's level vocabulary -- swarms
   uses logger.success(...) throughout (e.g. graph_workflow.py), so those
   lines previously passed through _clean_output unstripped. Verified byte-
   identical output vs. the original regexes for every previously-covered
   case (INFO/DEBUG/WARNING/ERROR, empty input, plain text, no-match input).

Also drops the now-unused `import time` and the `Progress`/`SpinnerColumn`/
`TextColumn` imports (only used by the removed print_progress).

Adds tests: every loguru level is stripped by _clean_output, empty-string
handling, the three dead methods are actually gone, and print_markdown
still works post-refactor.
@agu2347
agu2347 requested a review from kyegomez as a code owner August 9, 2026 16:34
@github-actions

github-actions Bot commented Aug 9, 2026

Copy link
Copy Markdown

Hello there, thank you for opening an PR ! 🙏🏻 The team was notified and they will get back to you asap.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Cleanup] formatter.py — 3 dead print methods (~142 lines) and 8 collapsible log-strip regexes

1 participant