Skip to content

Invalidate the presorted hint when clearing Stats caches - #570

Open
ac1982 wants to merge 1 commit into
mahmoud:masterfrom
ac1982:fix/stats-sorted-cache
Open

ac1982 wants to merge 1 commit into
mahmoud:masterfrom
ac1982:fix/stats-sorted-cache

Conversation

@ac1982

@ac1982 ac1982 commented Oct 8, 2026

Copy link
Copy Markdown

The change

Stats.clear_cache() now discards the is_sorted hint as well as cached measures. Its documented use after modifying Stats.data could otherwise recompute incorrect values from an order that no longer holds:

stats = Stats([1, 2, 3], is_sorted=True)
stats.data.append(0)
stats.clear_cache()
(stats.min, stats.max, stats.median)  # before: (1, 0, 2.5); expected: (0, 3, 1.5)

Reset the hint when clearing the cache, document that behavior, and cover repeated mutations with both copy modes. The is_sorted=True optimization remains available until the caller invalidates the cache.

The regression failed on master. After the fix, python -m pytest --doctest-modules boltons tests -q passed: 679 tests and 12 subtests on Python 3.12/Linux. Other platforms were not run locally.

Related #534 preserves mode tie-breaking while computing sorted measures. That PR leaves _is_sorted set after clear_cache() too, so it does not fix this presorted-input mutation case. This change does not alter mode tie-breaking or introduce a sorted-data cache. Open/closed searches for clear_cache, is_sorted, and Stats cache found no matching fix.

The backstory

Same contribution experiment and tooling as #569. While reading Stats' explicit mutation/cache contract, Codex reproduced wrong minimum, maximum, and median results after following the documented clear_cache() advice. This is a locally constructed regression, not a production incident.

AI assistance

  • Harness/tooling: OpenAI Codex, shell, pytest, GitHub CLI.
  • Model: GPT-6-based Codex.
  • Original prompt: “OK,继续今天的工作吧,magpie 只算做 addtional 任务,不记作当前天的 30,所以今天总共任务是 32 个。” Earlier instructions require substantive bug reproduction, rules/duplicate checks, minimal fixes and tests without reducing quality for quantity. Codex selected this independent cache-invalidation defect during repository inspection; no Stats-specific prompt was supplied.

@Wkhanex Wkhanex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appending out-of-order values and calling clear_cache() now agrees with Python's statistics results. I checked successive appends with both copy modes, including a new negative minimum: median, mean, minimum and maximum are all recomputed.

Checked cb330b389ef5 with standalone Python 3.12.14 checks (24 passing assertions). I haven't run the full upstream suite.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants