[Cleanup] formatter.py: remove 3 dead print methods, collapse log-strip regexes - #1856
Open
agu2347 wants to merge 1 commit into
Open
[Cleanup] formatter.py: remove 3 dead print methods, collapse log-strip regexes#1856agu2347 wants to merge 1 commit into
agu2347 wants to merge 1 commit into
Conversation
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.
|
Hello there, thank you for opening an PR ! 🙏🏻 The team was notified and they will get back to you asap. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1838.
1. Dead print methods (142 lines)
Removed
print_progress,print_panel_token_by_token, andprint_plan_treefromswarms/utils/formatter.py. Verified 0 callers acrossswarms/,tests/, andexamples/before removing. Also dropped the now-unusedimport timeand theProgress/SpinnerColumn/TextColumnimports, which onlyprint_progressused.2.
print_markdownredundant aliasprint_markdown'smarkdown_handlerbranch was byte-identical toprint_panel's markdown branch (:434-437 in the original). Its only caller outside this file wastests/utils/test_formatter.py. Rather than delete it and touch the test, I kept it as a one-line documented alias forwarding toprint_panel, so existing/external callers keep working unchanged.3.
_clean_output— 8 regexes → 2The two blocks of four near-identical
re.subcalls (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 —
swarmsuseslogger.success(...)throughout (e.g.graph_workflow.py), so SUCCESS-level lines were passing through_clean_outputunstripped, 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:
swarms/utils/formatter.pydirectly viaimportlib(bypassingswarms/__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.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_stringtest_dead_print_methods_are_removedtest_print_markdown_still_works_as_aliasAlso ran
ruff checkandblack --checkagainst the project's actual configured rules (pyproject.toml:line-length = 70, CI pinsruff==0.2.1with no explicit[tool.ruff.lint] select, so only the defaultE4,E7,E9,Frules apply) — both clean on the two changed files.black --line-length 70was applied to the new test additions.