jenkins-bot merged this change.
[bugfix] Do not return a random i18n.translation() result
As noted in i18n comment for i18n.translate() this function shouldn't
simply return "any one" result from given translation dict. The
result is also not deterministic and may differ between several
Python versions.
Now just return None if fallback is False by default or raise a
KeyError exception if fallback is not False and no fallback dict
entry is given.
Update test accordingly.
Bug: T220099
Change-Id: Ia80d1bf9e2eede9959ec955874caa3915054a0b7
---
M pywikibot/i18n.py
M tests/i18n_tests.py
2 files changed, 6 insertions(+), 11 deletions(-)
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 607553b..73fb7f0 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -17,7 +17,7 @@
messages. See L{twtranslate} for more information on the messages.
"""
#
-# (C) Pywikibot team, 2004-2019
+# (C) Pywikibot team, 2004-2020
#
# Distributed under the terms of the MIT license.
#
@@ -614,14 +614,10 @@
trans = xdict[code]
break
else:
- if fallback is not True:
- # this shouldn't simply return "any one" code but when fallback
- # was True before 65518573d2b0, it did just that. When False it
- # did just return None. It's now also returning None in the new
- # iterable mode.
+ if fallback is False:
return None
- code = list(xdict.keys())[0]
- trans = xdict[code]
+ raise KeyError('No fallback key found in lookup dict for "{}"'
+ .format(code))
if trans is None:
return None # return None if we have no translation found
if parameters is None:
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 2d2d32d..6e21cbe 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -70,9 +70,8 @@
"""Test translate with missing English text."""
for code in ('en', 'fy', 'nl'):
with self.subTest(code=code):
- self.assertEqual(i18n.translate(code, self.msg_no_english,
- fallback=True),
- 'test-no-english JA')
+ with self.assertRaises(KeyError):
+ i18n.translate(code, self.msg_no_english, fallback=True)
class UserInterfaceLangTestCase(TestCase):
To view, visit change 589557. To unsubscribe, or for help writing mail filters, visit settings.