It was not documented and only supported in the C implementation.
(Contributed by Serhiy Storchaka in :gh:`89902`.)
+* Emit deprecation warning for non-integer numbers in :mod:`gettext` functions
+ and methods that consider plural forms even if the translation was not found.
+ (Contributed by Serhiy Storchaka in :gh:`88434`.)
+
Pending Removal in Python 3.14
------------------------------
except TypeError:
raise TypeError('Plural value must be an integer, got %s' %
(n.__class__.__name__,)) from None
+ return _as_int2(n)
+
+def _as_int2(n):
+ try:
+ return operator.index(n)
+ except TypeError:
+ pass
import warnings
frame = sys._getframe(1)
def ngettext(self, msgid1, msgid2, n):
if self._fallback:
return self._fallback.ngettext(msgid1, msgid2, n)
+ n = _as_int2(n)
if n == 1:
return msgid1
else:
def npgettext(self, context, msgid1, msgid2, n):
if self._fallback:
return self._fallback.npgettext(context, msgid1, msgid2, n)
+ n = _as_int2(n)
if n == 1:
return msgid1
else:
try:
t = translation(domain, _localedirs.get(domain, None))
except OSError:
+ n = _as_int2(n)
if n == 1:
return msgid1
else:
try:
t = translation(domain, _localedirs.get(domain, None))
except OSError:
+ n = _as_int2(n)
if n == 1:
return msgid1
else:
x = gettext(singular)
self.assertEqual(x, tsingular)
+ lineno = self._test_plural_forms.__code__.co_firstlineno + 12
+ with self.assertWarns(DeprecationWarning) as cm:
+ x = ngettext(singular, plural, 1.0)
+ self.assertEqual(cm.filename, __file__)
+ self.assertEqual(cm.lineno, lineno)
+ self.assertEqual(x, tsingular)
+ with self.assertWarns(DeprecationWarning) as cm:
+ x = ngettext(singular, plural, 1.1)
+ self.assertEqual(cm.filename, __file__)
+ self.assertEqual(cm.lineno, lineno + 5)
+ self.assertEqual(x, tplural)
+
if numbers_only:
- lineno = self._test_plural_forms.__code__.co_firstlineno + 9
- with self.assertWarns(DeprecationWarning) as cm:
- x = ngettext(singular, plural, 1.0)
- self.assertEqual(cm.filename, __file__)
- self.assertEqual(cm.lineno, lineno + 4)
- self.assertEqual(x, tsingular)
- with self.assertWarns(DeprecationWarning) as cm:
- x = ngettext(singular, plural, 1.1)
- self.assertEqual(cm.filename, __file__)
- self.assertEqual(cm.lineno, lineno + 9)
- self.assertEqual(x, tplural)
with self.assertRaises(TypeError):
ngettext(singular, plural, None)
else:
- x = ngettext(singular, plural, None)
+ with self.assertWarns(DeprecationWarning) as cm:
+ x = ngettext(singular, plural, None)
self.assertEqual(x, tplural)
def test_plural_forms(self):