Page MenuHomePhabricator

Replace recursive upload orchestration with explicit states
Open, In Progress, MediumPublic

Description

Uploader._upload() in pywikibot/site/_upload.py has 48 branches and approximately 138 statements.

Fresh uploads, chunked transfers, stash validation and resumption, warning callbacks, offset correction, polling, and final publication are currently mixed into one recursive workflow.
Warning handling can restart or resume an upload by recursively calling _upload(), making state transitions difficult to follow and modify safely.

Suggested improvements:

  • Introduce explicit stages for validation, stash recovery, chunk transfer, warning resolution, polling, and finalization.
  • Keep upload progress such as file key and offset in a private state object.
  • Replace recursive restarts with iterative stage transitions.
  • Extract focused helpers for each stage.
  • Preserve existing public behavior, exceptions, logging, warning callbacks, and request parameters.
  • Reuse existing upload coverage and add focused offline tests only where state transitions are not already characterized.

Acceptance criteria:

  • Upload restarts and resumptions no longer call _upload() recursively.
  • Fresh, URL, chunked, resumed, warning, polling, and finalization paths use explicit transitions.
  • Uploader.upload() retains its current public interface and behavior.
  • _upload() complexity is substantially reduced without transferring it into one large helper.
  • Focused tests, Ruff, Flake8, and git diff --check pass.

Benefits:

  • Upload state becomes visible and testable.
  • Resumption and warning handling are easier to reason about.
  • Future upload fixes are less likely to break another upload mode.

Event Timeline

Mahveotm triaged this task as Medium priority.Aug 14 2026, 7:28 PM

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

[pywikibot/core@master] upload: Replace recursive orchestration

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

Mahveotm changed the task status from Open to In Progress.Aug 14 2026, 11:09 PM
Mahveotm moved this task from Backlog to Needs Review on the Pywikibot board.

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

[pywikibot/core@master] upload: Add orchestration regression tests

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

Change #1326074 merged by jenkins-bot:

[pywikibot/core@master] upload: Add orchestration regression tests

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