Page MenuHomePhabricator

Reduce code complexity of replace.main()
Closed, ResolvedPublicSpike

Description

Reduce code complexity of replace.main()

Description:
The main() function in scripts/replace.py has grown too large and complex over time. It currently triggers a C901 ("function is too complex") warning when running code quality checks (flake8/mccabe).

High complexity makes the function harder to understand, test, and maintain. In particular:

  • Multiple nested conditionals and loops
  • Several distinct responsibilities handled in one function
  • Many edge cases mixed into the main execution flow

Suggested improvements:

  • Split main() into smaller helper functions, each handling a specific concern (argument parsing, summary handling, replacements iteration, etc.).
  • Reduce cyclomatic complexity below the flake8/mccabe threshold.
  • Add or update unit tests to cover the extracted functionality.

Benefits:

  • Improves readability and maintainability.
  • Easier testing of individual parts.
  • Aligns with Python style guidelines and helps keep CI checks green.

Details

Related Changes in Gerrit:

Event Timeline

Xqt triaged this task as High priority.Aug 18 2025, 4:14 AM
Xqt moved this task from Backlog to CI test failures on the Pywikibot-tests board.
Xqt changed the subtype of this task from "Task" to "Spike".Aug 18 2025, 4:22 AM

Change #1323821 had a related patch set uploaded (by Mahveotm; author: Mahveotm):

[pywikibot/core@master] replace: Reduce complexity of main

https://gerrit.wikimedia.org/r/1323821

Mahveotm changed the task status from Open to In Progress.Aug 9 2026, 8:01 PM
Mahveotm claimed this task.
Mahveotm moved this task from Backlog to Needs Review on the Pywikibot board.

Change #1323821 merged by jenkins-bot:

[pywikibot/core@master] replace: Reduce complexity of main

https://gerrit.wikimedia.org/r/1323821