Page MenuHomePhabricator

Reduce code complexity of category.main()
Open, In Progress, MediumPublicSpike

Description

Reduce code complexity of category.main()

Description:
The main() function in scripts/category.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.

Event Timeline

Xqt triaged this task as Medium priority.Aug 11 2026, 12:39 PM
Xqt changed the subtype of this task from "Task" to "Spike".

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

[pywikibot/core@master] category: Reduce main complexity

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

Mahveotm changed the task status from Open to In Progress.Aug 13 2026, 9:54 AM
Mahveotm moved this task from Backlog to Needs Review on the Pywikibot board.

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

[pywikibot/core@master] category: Extract argument handling

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

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

[pywikibot/core@master] category: Extract bot construction

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

Change #1326255 merged by jenkins-bot:

[pywikibot/core@master] category: Extract argument handling

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

Change #1326256 merged by jenkins-bot:

[pywikibot/core@master] category: Extract bot construction

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