Update statistics.py from 3.14.3

This commit is contained in:
ShaharNaveh authored and Jeong, YunWon committed 2026-02-11 18:01:41 +09:00
1 parent fa59402def
commit a9c98d2666
2 files changed
+948 -873

No files matched your search

+922 -850
View File
File diff suppressed because it is too large. Load diff
+26 -23
View File
@@ -645,7 +645,7 @@ class TestNumericTestCase(unittest.TestCase):
def test_numerictestcase_is_testcase(self):
# Ensure that NumericTestCase actually is a TestCase.
self.assertTrue(issubclass(NumericTestCase, unittest.TestCase))
self.assertIsSubclass(NumericTestCase, unittest.TestCase)
def test_error_msg_numeric(self):
# Test the error message generated for numeric comparisons.
@@ -683,32 +683,23 @@ class GlobalsTest(unittest.TestCase):
def test_meta(self):
# Test for the existence of metadata.
for meta in self.expected_metadata:
self.assertTrue(hasattr(self.module, meta),
"%s not present" % meta)
self.assertHasAttr(self.module, meta)
def test_check_all(self):
# Check everything in __all__ exists and is public.
module = self.module
for name in module.__all__:
# No private names in __all__:
self.assertFalse(name.startswith("_"),
self.assertNotStartsWith(name, "_",
'private name "%s" in __all__' % name)
# And anything in __all__ must exist:
self.assertTrue(hasattr(module, name),
'missing name "%s" in __all__' % name)
self.assertHasAttr(module, name)
class StatisticsErrorTest(unittest.TestCase):
def test_has_exception(self):
errmsg = (
"Expected StatisticsError to be a ValueError, but got a"
" subclass of %r instead."
)
self.assertTrue(hasattr(statistics, 'StatisticsError'))
self.assertTrue(
issubclass(statistics.StatisticsError, ValueError),
errmsg % statistics.StatisticsError.__base__
)
self.assertHasAttr(statistics, 'StatisticsError')
self.assertIsSubclass(statistics.StatisticsError, ValueError)
# === Tests for private utility functions ===
@@ -2014,7 +2005,6 @@ class VarianceStdevMixin(UnivariateCommonMixin):
expected = self.func(data)
self.assertEqual(self.func(iter(data)), expected)
class TestPVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
# Tests for population variance.
def setUp(self):
@@ -2122,6 +2112,14 @@ class TestPStdev(VarianceStdevMixin, NumericTestCase):
self.assertEqual(self.func(data), 2.5)
self.assertEqual(self.func(data, mu=0.5), 6.5)
def test_gh_140938(self):
# Inputs with inf/nan should raise a ValueError
with self.assertRaises(ValueError):
self.func([1.0, math.inf])
with self.assertRaises(ValueError):
self.func([1.0, math.nan])
class TestSqrtHelpers(unittest.TestCase):
def test_integer_sqrt_of_frac_rto(self):
@@ -2435,17 +2433,22 @@ class TestKDE(unittest.TestCase):
data.append(100)
self.assertGreater(f_hat(100), 0.0)
def test_kde_kernel_invcdfs(self):
kernel_invcdfs = statistics._kernel_invcdfs
kde = statistics.kde
def test_kde_kernel_specs(self):
# White-box test for the kernel formulas in isolation from
# their downstream use in kde() and kde_random()
kernel_specs = statistics._kernel_specs
# Verify that cdf / invcdf will round trip
xarr = [i/100 for i in range(-100, 101)]
for kernel, invcdf in kernel_invcdfs.items():
parr = [i/1000 + 5/10000 for i in range(1000)]
for kernel, spec in kernel_specs.items():
cdf = spec['cdf']
invcdf = spec['invcdf']
with self.subTest(kernel=kernel):
cdf = kde([0.0], h=1.0, kernel=kernel, cumulative=True)
for x in xarr:
self.assertAlmostEqual(invcdf(cdf(x)), x, places=5)
self.assertAlmostEqual(invcdf(cdf(x)), x, places=6)
for p in parr:
self.assertAlmostEqual(cdf(invcdf(p)), p, places=11)
@support.requires_resource('cpu')
def test_kde_random(self):
@@ -2797,7 +2800,7 @@ class TestCorrelationAndCovariance(unittest.TestCase):
@requires_IEEE_754
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,
"accuracy not guaranteed on machines with double rounding")
@support.cpython_only # Allow for a weaker sumprod() implmentation
@support.cpython_only # Allow for a weaker sumprod() implementation
def test_sqrtprod_helper_function_improved_accuracy(self):
# Test a known example where accuracy is improved
x, y, target = 0.8035720646477457, 0.7957468097636939, 0.7996498651651661