jenkins-bot merged this change.

View Change

Approvals: Huji: Looks good to me, approved jenkins-bot: Verified
[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/
M tests/
2 files changed, 6 insertions(+), 11 deletions(-)

diff --git a/pywikibot/ b/pywikibot/
index 607553b..73fb7f0 100644
--- a/pywikibot/
+++ b/pywikibot/
@@ -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]
- 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/ b/tests/
index 2d2d32d..6e21cbe 100644
--- a/tests/
+++ b/tests/
@@ -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.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Ia80d1bf9e2eede9959ec955874caa3915054a0b7
Gerrit-Change-Number: 589557
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <>
Gerrit-Reviewer: Huji <>
Gerrit-Reviewer: Isaacandy <>
Gerrit-Reviewer: Siebrand <>
Gerrit-Reviewer: jenkins-bot (75)