jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/443034 )
Change subject: Try to import abstract base classes from collections.abc ......................................................................
Try to import abstract base classes from collections.abc
Bug: T198388 Change-Id: I65d6d9ba3c96d7fa4bd38d26b2bb9201efef8186 --- M pywikibot/data/api.py M pywikibot/diff.py M pywikibot/i18n.py M pywikibot/site.py M pywikibot/textlib.py M pywikibot/tools/__init__.py M scripts/replace.py M tests/namespace_tests.py M tests/site_tests.py M tests/tools_tests.py M tests/utils.py 11 files changed, 59 insertions(+), 21 deletions(-)
Approvals: Zhuyifei1999: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index 8d7d3c1..0d1785c 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -16,7 +16,10 @@ import re import traceback
-from collections import Container, MutableMapping +try: + from collections.abc import Container, MutableMapping +except ImportError: # Python 2.7 + from collections import Container, MutableMapping from email.mime.nonmultipart import MIMENonMultipart from warnings import warn
diff --git a/pywikibot/diff.py b/pywikibot/diff.py index 1b199f3..d8bf5f4 100644 --- a/pywikibot/diff.py +++ b/pywikibot/diff.py @@ -11,7 +11,10 @@ import math import sys
-from collections import Sequence +try: + from collections.abc import Sequence +except ImportError: # Python 2.7 + from collections import Sequence from difflib import _format_range_unified as format_range_unified
import pywikibot diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 95afaf5..336dbbd 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -28,7 +28,11 @@ import pkgutil import re
-from collections import defaultdict, Mapping +from collections import defaultdict +try: + from collections.abc import Mapping +except ImportError: # Python 2.7 + from collections import Mapping from warnings import warn
import pywikibot diff --git a/pywikibot/site.py b/pywikibot/site.py index 41fb961..6552537 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -25,7 +25,11 @@ import threading import time
-from collections import Iterable, Container, namedtuple, Mapping +try: + from collections.abc import Iterable, Container, Mapping +except ImportError: # Python 2.7 + from collections import Iterable, Container, Mapping +from collections import namedtuple from warnings import warn
import pywikibot diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py index 180461b..050ee5a 100644 --- a/pywikibot/textlib.py +++ b/pywikibot/textlib.py @@ -13,7 +13,11 @@ # from __future__ import absolute_import, unicode_literals
-from collections import OrderedDict, Sequence +from collections import OrderedDict +try: + from collections.abc import Sequence +except ImportError: # Python 2.7 + from collections import Sequence import datetime import re import sys diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py index c221b6c..34ffe3c 100644 --- a/pywikibot/tools/__init__.py +++ b/pywikibot/tools/__init__.py @@ -21,6 +21,10 @@ import time import types
+try: + from collections.abc import Iterator, Mapping +except ImportError: # Python 2.7 + from collections import Iterator, Mapping from distutils.version import Version from functools import wraps from warnings import catch_warnings, showwarning, warn @@ -888,7 +892,7 @@ Note: This is not thread safe.
@param iterable: the source iterable - @type iterable: collections.Iterable + @type iterable: collections.abc.Iterable @param container: storage of seen items @type container: type @param key: function to convert the item to a key @@ -926,7 +930,7 @@ """An error that gets caught by both KeyError and IndexError."""
-class EmptyDefault(str, collections.Mapping): +class EmptyDefault(str, Mapping):
""" A default for a not existing siteinfo property. @@ -984,7 +988,7 @@ """Unicode string with SelfCallMixin."""
-class IteratorNextMixin(collections.Iterator): +class IteratorNextMixin(Iterator):
"""Backwards compatibility for Iterators."""
diff --git a/scripts/replace.py b/scripts/replace.py index fe56b4c..78cd405 100755 --- a/scripts/replace.py +++ b/scripts/replace.py @@ -139,7 +139,10 @@ from __future__ import absolute_import, unicode_literals
import codecs -import collections +try: + from collections.abc import Sequence +except ImportError: # Python 2.7 + from collections import Sequence import re import sys import time @@ -553,7 +556,7 @@ **kwargs)
for i, replacement in enumerate(replacements): - if isinstance(replacement, collections.Sequence): + if isinstance(replacement, Sequence): if len(replacement) != 2: raise ValueError('Replacement number {0} does not have ' 'exactly two elements: {1}'.format( diff --git a/tests/namespace_tests.py b/tests/namespace_tests.py index 48aac4f..edf39aa 100644 --- a/tests/namespace_tests.py +++ b/tests/namespace_tests.py @@ -7,7 +7,10 @@ # from __future__ import absolute_import, unicode_literals
-from collections import Iterable +try: + from collections.abc import Iterable +except ImportError: # Python 2.7 + from collections import Iterable
from pywikibot.site import Namespace, NamespacesDict from pywikibot.tools import ( diff --git a/tests/site_tests.py b/tests/site_tests.py index 2bd64d4..b11501b 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -12,7 +12,10 @@ import re import sys
-from collections import Iterable, Mapping +try: + from collections.abc import Iterable, Mapping +except ImportError: # Python 2.7 + from collections import Iterable, Mapping from datetime import datetime
import pywikibot diff --git a/tests/tools_tests.py b/tests/tools_tests.py index 430ee50..5850c81 100644 --- a/tests/tools_tests.py +++ b/tests/tools_tests.py @@ -7,7 +7,11 @@ # Distributed under the terms of the MIT license. from __future__ import absolute_import, unicode_literals
-import collections +try: + from collections.abc import Mapping +except ImportError: # Python 2.7 + from collections import Mapping +from collections import OrderedDict import decimal import inspect import os.path @@ -449,9 +453,9 @@ self.assertEqual(next(deduper), 3)
if key in (hash, passthrough): - if isinstance(deduped, collections.OrderedDict): + if isinstance(deduped, OrderedDict): self.assertEqual(list(deduped.keys()), [1, 3]) - elif isinstance(deduped, collections.Mapping): + elif isinstance(deduped, Mapping): self.assertCountEqual(list(deduped.keys()), [1, 3]) else: self.assertEqual(deduped, {1, 3}) @@ -460,9 +464,9 @@ self.assertEqual(next(deduper), 4)
if key in (hash, passthrough): - if isinstance(deduped, collections.OrderedDict): + if isinstance(deduped, OrderedDict): self.assertEqual(list(deduped.keys()), [1, 3, 2, 4]) - elif isinstance(deduped, collections.Mapping): + elif isinstance(deduped, Mapping): self.assertCountEqual(list(deduped.keys()), [1, 2, 3, 4]) else: self.assertEqual(deduped, {1, 2, 3, 4}) @@ -480,7 +484,7 @@ self.assertEqual(next(deduper), '3')
if key in (hash, passthrough): - if isinstance(deduped, collections.Mapping): + if isinstance(deduped, Mapping): self.assertEqual(deduped.keys(), [key('1'), key('3')]) else: self.assertEqual(deduped, {key('1'), key('3')}) @@ -489,7 +493,7 @@ self.assertEqual(next(deduper), '4')
if key in (hash, passthrough): - if isinstance(deduped, collections.Mapping): + if isinstance(deduped, Mapping): self.assertEqual(deduped.keys(), [key(i) for i in self.strs]) else: self.assertEqual(deduped, {key(i) for i in self.strs}) @@ -510,7 +514,7 @@
def test_OrderedDict(self): """Test filter_unique with a OrderedDict.""" - deduped = collections.OrderedDict() + deduped = OrderedDict() deduper = tools.filter_unique(self.ints, container=deduped) self._test_dedup_int(deduped, deduper)
diff --git a/tests/utils.py b/tests/utils.py index 00133dd..fe8a9ed 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -17,7 +17,10 @@ import traceback import warnings
-from collections import Mapping +try: + from collections.abc import Mapping +except ImportError: # Python 2.7 + from collections import Mapping from types import ModuleType
import pywikibot
pywikibot-commits@lists.wikimedia.org