Skip to content

gh-154945: add regression test for NormalDist.quantiles(n < 1) - #155015

Closed
nikolauspschuetz wants to merge 1 commit into
python:mainfrom
nikolauspschuetz:gh154945-normaldist-quantiles
Closed

gh-154945: add regression test for NormalDist.quantiles(n < 1)#155015
nikolauspschuetz wants to merge 1 commit into
python:mainfrom
nikolauspschuetz:gh154945-normaldist-quantiles

Conversation

@nikolauspschuetz

@nikolauspschuetz nikolauspschuetz commented Aug 1, 2026

Copy link
Copy Markdown

Adds a regression test for the inconsistency reported in gh-154945: NormalDist.quantiles(n) accepts n < 1 and silently returns [], whereas the module-level statistics.quantiles(data, n) rejects it with StatisticsError('n must be at least 1').

>>> from statistics import NormalDist, quantiles
>>> NormalDist().quantiles(0)
[]                                   # NormalDist: silently wrong
>>> quantiles([1, 2, 3, 4], n=0)
StatisticsError: n must be at least 1   # module-level: validated

NormalDist.quantiles computes [self.inv_cdf(i / n) for i in range(1, n)]; with n=0 the range is empty, so the invalid request produces an empty list instead of an error, and the two sibling APIs disagree.

This PR is the failing test only, so CI shows it red against main — that's the reproduction. It goes green with a two-line guard at the top of NormalDist.quantiles, mirroring the module-level validation so both APIs behave identically:

--- a/Lib/statistics.py
+++ b/Lib/statistics.py
@@ -1293,6 +1293,8 @@ def quantiles(self, n=4):
         Set *n* to 100 for percentiles which gives the 99 cuts points that
         separate the normal distribution in to 100 equal sized groups.
         """
+        if n < 1:
+            raise StatisticsError('n must be at least 1')
         return [self.inv_cdf(i / n) for i in range(1, n)]

Notes on the choice:

  • Message and exception type are copied verbatim from the existing check in the module-level quantiles() (Lib/statistics.py), so the two entry points raise identically — that consistency is the whole point of the report.
  • Only n < 1 is guarded. n == 1 correctly returns [] (one interval ⇒ zero cut points), matching the module-level function, so it is intentionally left unchanged.
  • The issue also observes that inv_cdf re-checks p <= 0 or p >= 1 every loop iteration even though i / n is always in (0, 1). That's a separate micro-optimization and is deliberately not included here to keep this change to the reported correctness fix.

@ayishaatwork — you asked to work on this on the issue; the guard above is the whole fix, so please feel free to send it as the source-side change and this test will turn green. I've verified both states locally on a built interpreter (./python -m test test_statistics): the test fails on main and passes with the guard, for both the pure-Python and the C-accelerated NormalDist.

@bedevere-app bedevere-app Bot added the tests Tests in the Lib/test dir label Aug 1, 2026
@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Adds test_quantiles_invalid_n to verify that NormalDist.quantiles(n)
raises StatisticsError when n < 1, mirroring the module-level quantiles()
validation. Currently fails (test is red) — quantiles(0) silently returns []
instead of raising.
@nikolauspschuetz
nikolauspschuetz force-pushed the gh154945-normaldist-quantiles branch from a9051c2 to 9b48d46 Compare August 1, 2026 09:11
@bedevere-app

bedevere-app Bot commented Aug 1, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz

picnixz commented Aug 1, 2026

Copy link
Copy Markdown
Member

We should just raise because statistics.quantile() raises already IIRC.

@picnixz picnixz closed this Aug 1, 2026
@picnixz

picnixz commented Aug 1, 2026

Copy link
Copy Markdown
Member

Also:

  • we reject fully AI driven PRs especially if communication was done through agents.
  • do not open PRs for partial work or before the issue has been discussed and acknowledged by a triager/core dev
  • read the devguide about AI usage and our contribution process

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

Labels

tests Tests in the Lib/test dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants