jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[cleanup] Remove deprecated twntranslate method

This method was deprecated about six years ago. Also cleaning up a couple bits
of code that were commented as being for it.

Change-Id: I0b31aaeff0f175ca2ed6f6200b61ba47c3f1879d
---
M pywikibot/i18n.py
M tests/i18n_tests.py
2 files changed, 4 insertions(+), 213 deletions(-)

diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 5399c85..4554f3a7 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -28,7 +28,6 @@
from contextlib import suppress
from textwrap import fill
from typing import Optional, Union
-from warnings import warn

import pywikibot
from pywikibot import __url__, config
@@ -36,7 +35,6 @@
from pywikibot.plural import plural_rule
from pywikibot.tools import (
ModuleDeprecationWrapper,
- deprecated,
deprecated_args,
issue_deprecation_warning,
)
@@ -710,21 +708,9 @@
'See {}/i18n'
.format(_messages_package_name, twtitle, __url__))

- source_needed = False
- # If a site is given instead of a lang, use its language
- if hasattr(source, 'lang'):
- lang = source.lang
- # check whether we need the language code back
- elif isinstance(source, list):
- # For backwards compatibility still support lists, when twntranslate
- # was not deprecated and needed a way to get the used language code
- # back.
- warn('The source argument should not be a list but either a BaseSite '
- 'or a str/unicode.', DeprecationWarning, 2)
- lang = source.pop()
- source_needed = True
- else:
- lang = source
+ # if source is a site then use its lang attribute, otherwise it's a str
+
+ lang = getattr(source, 'lang', source)

# There are two possible failure modes: the translation dict might not have
# the language altogether, or a specific key could be untranslated. Both
@@ -743,9 +729,6 @@
'outdated submodule. See {}/i18n'
.format('English' if 'en' in langs else "'{}'".format(lang),
twtitle, __url__)))
- # send the language code back via the given mutable list parameter
- if source_needed:
- source.append(alt)

if '{{PLURAL:' in trans:
# _extract_plural supports in theory non-mappings, but they are
@@ -754,16 +737,6 @@
raise TypeError('parameters must be a mapping.')
trans = _extract_plural(alt, trans, parameters)

- # this is only the case when called in twntranslate, and that didn't apply
- # parameters when it wasn't a dict
- if isinstance(parameters, _PluralMappingAlias):
- # This is called due to the old twntranslate function which ignored
- # KeyError. Instead only_plural should be used.
- if isinstance(parameters.source, dict):
- with suppress(KeyError):
- trans %= parameters.source
- parameters = None
-
if parameters is not None and not isinstance(parameters, Mapping):
raise ValueError('parameters should be a mapping, not {}'
.format(type(parameters).__name__))
@@ -773,16 +746,6 @@
return trans


-@deprecated('twtranslate', since='20151009', future_warning=True)
-@deprecated_args(code='source')
-def twntranslate(source, twtitle: str,
- parameters: Optional[Mapping] = None) -> Optional[str]:
- """DEPRECATED: Get translated string for the key."""
- if parameters is not None:
- parameters = _PluralMappingAlias(parameters)
- return twtranslate(source, twtitle, parameters)
-
-
@deprecated_args(code='source')
def twhas_key(source, twtitle: str) -> bool:
"""
diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py
index 2c9bc72..b80d6fb 100644
--- a/tests/i18n_tests.py
+++ b/tests/i18n_tests.py
@@ -7,10 +7,9 @@
from contextlib import suppress

import pywikibot
-from pywikibot import bot, config, i18n, plural
+from pywikibot import bot, config, i18n
from pywikibot.exceptions import TranslationError
from tests.aspects import (
- AutoDeprecationTestCase,
DefaultSiteTestCase,
PwbTestCase,
TestCase,
@@ -296,177 +295,6 @@
i18n.twtranslate('en', 'test-no-english')


-class TestTWNTranslate(TWNTestCaseBase, AutoDeprecationTestCase):
-
- """Test {{PLURAL:}} support."""
-
- net = False
- message_package = 'tests.i18n'
-
- def testNumber(self):
- """Use a number."""
- self.assertEqual(
- i18n.twntranslate('de', 'test-plural', 0) % {'num': 0},
- 'Bot: Ändere 0 Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-plural', 1) % {'num': 1},
- 'Bot: Ändere 1 Seite.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-plural', 2) % {'num': 2},
- 'Bot: Ändere 2 Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-plural', 3) % {'num': 3},
- 'Bot: Ändere 3 Seiten.')
- self.assertEqual(
- i18n.twntranslate('en', 'test-plural', 0) % {'num': 'no'},
- 'Bot: Changing no pages.')
- self.assertEqual(
- i18n.twntranslate('en', 'test-plural', 1) % {'num': 'one'},
- 'Bot: Changing one page.')
- self.assertEqual(
- i18n.twntranslate('en', 'test-plural', 2) % {'num': 'two'},
- 'Bot: Changing two pages.')
- self.assertEqual(
- i18n.twntranslate('en', 'test-plural', 3) % {'num': 'three'},
- 'Bot: Changing three pages.')
-
- def testString(self):
- """Use a string."""
- self.assertEqual(
- i18n.twntranslate('en', 'test-plural', '1') % {'num': 'one'},
- 'Bot: Changing one page.')
-
- def testDict(self):
- """Use a dictionary."""
- self.assertEqual(
- i18n.twntranslate('en', 'test-plural', {'num': 2}),
- 'Bot: Changing 2 pages.')
-
- def testExtended(self):
- """Use additional format strings."""
- self.assertEqual(
- i18n.twntranslate('fr', 'test-plural',
- {'num': 1, 'descr': 'seulement'}),
- 'Robot: Changer seulement une page.')
-
- def testExtendedOutside(self):
- """Use additional format strings also outside."""
- self.assertEqual(
- i18n.twntranslate('fr', 'test-plural', 1) % {'descr': 'seulement'},
- 'Robot: Changer seulement une page.')
-
- def testMultiple(self):
- """Test using multiple plural entries."""
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', 1)
- % {'action': 'Ändere', 'line': 'eine'},
- 'Bot: Ändere eine Zeile von einer Seite.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', 2)
- % {'action': 'Ändere', 'line': 'zwei'},
- 'Bot: Ändere zwei Zeilen von mehreren Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', 3)
- % {'action': 'Ändere', 'line': 'drei'},
- 'Bot: Ändere drei Zeilen von mehreren Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', (1, 2, 2))
- % {'action': 'Ändere', 'line': 'eine'},
- 'Bot: Ändere eine Zeile von mehreren Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', [3, 1, 1])
- % {'action': 'Ändere', 'line': 'drei'},
- 'Bot: Ändere drei Zeilen von einer Seite.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', ['3', 1, 1])
- % {'action': 'Ändere', 'line': 'drei'},
- 'Bot: Ändere drei Zeilen von einer Seite.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals', '321')
- % {'action': 'Ändere', 'line': 'dreihunderteinundzwanzig'},
- 'Bot: Ändere dreihunderteinundzwanzig Zeilen von mehreren Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals',
- {'action': 'Ändere', 'line': 1, 'page': 1}),
- 'Bot: Ändere 1 Zeile von einer Seite.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals',
- {'action': 'Ändere', 'line': 1, 'page': 2}),
- 'Bot: Ändere 1 Zeile von mehreren Seiten.')
- self.assertEqual(
- i18n.twntranslate('de', 'test-multiple-plurals',
- {'action': 'Ändere', 'line': '11', 'page': 2}),
- 'Bot: Ändere 11 Zeilen von mehreren Seiten.')
-
- def testMultipleWrongParameterLength(self):
- """Test wrong parameter length."""
- err_msg = 'Length of parameter does not match PLURAL occurrences'
- with self.assertRaisesRegex(ValueError, err_msg):
- i18n.twntranslate('de', 'test-multiple-plurals', (1, 2))
-
- with self.assertRaisesRegex(ValueError, err_msg):
- i18n.twntranslate('de', 'test-multiple-plurals', ['321'])
-
- def testMultipleNonNumbers(self):
- """Test error handling for multiple non-numbers."""
- with self.assertRaisesRegex(
- ValueError, r"invalid literal for int\(\) with base 10: 'drei'"
- ):
- i18n.twntranslate('de', 'test-multiple-plurals', ['drei', '1', 1])
- with self.assertRaisesRegex(
- ValueError, r"invalid literal for int\(\) with base 10: 'elf'"
- ):
- i18n.twntranslate('de', 'test-multiple-plurals',
- {'action': 'Ändere', 'line': 'elf', 'page': 2})
-
- def testAllParametersExist(self):
- """Test that all parameters are required when using a dict."""
- # all parameters must be inside twntranslate
- self.assertEqual(i18n.twntranslate('de', 'test-multiple-plurals',
- {'line': 1, 'page': 1}),
- 'Bot: %(action)s %(line)s Zeile von einer Seite.')
-
- def test_fallback_lang(self):
- """
- Test that twntranslate uses the translation's language.
-
- twntranslate calls _twtranslate which might return the translation for
- a different language and then the plural rules from that language need
- to be applied.
- """
- # co has fr as altlang but has no plural rules defined (otherwise this
- # test might not catch problems) so it's using the plural variant for 0
- # although French uses the plural variant for numbers > 1 (so not 0)
- assert 'co' not in plural.plural_rules
- assert plural.plural_rules['fr']['plural'](0) is False
- self.assertEqual(
- i18n.twntranslate('co', 'test-plural',
- {'num': 0, 'descr': 'seulement'}),
- 'Robot: Changer seulement une page.')
- self.assertEqual(
- i18n.twntranslate('co', 'test-plural',
- {'num': 1, 'descr': 'seulement'}),
- 'Robot: Changer seulement une page.')
-
-
-class ScriptMessagesTestCase(TWNTestCaseBase, AutoDeprecationTestCase):
-
- """Real messages test."""
-
- net = False
- message_package = 'scripts.i18n'
-
- def test_basic(self):
- """Verify that real messages are able to be loaded."""
- self.assertEqual(i18n.twntranslate('en', 'pywikibot-enter-new-text'),
- 'Please enter the new text:')
-
- def test_missing(self):
- """Test a missing message from a real message bundle."""
- with self.assertRaises(TranslationError):
- i18n.twntranslate('en', 'pywikibot-missing-key')
-
-
class InputTestCase(TWNTestCaseBase, UserInterfaceLangTestCase, PwbTestCase):

"""Test i18n.input."""

To view, visit change 693509. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I0b31aaeff0f175ca2ed6f6200b61ba47c3f1879d
Gerrit-Change-Number: 693509
Gerrit-PatchSet: 2
Gerrit-Owner: Damian <atagar1@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged