From b99d1c6630daa0e3d034ea6a374583648ac1444b Mon Sep 17 00:00:00 2001 From: istommao Date: Mon, 24 Dec 2018 16:49:05 +0800 Subject: [PATCH] Fix collections ABCs deprecation warning --- CHANGELOG.md | 8 ++++++++ pydu/__init__.py | 2 +- pydu/dict.py | 19 ++++++++++++++----- pydu/list.py | 9 ++++++++- tests/test_system.py | 6 +++--- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4e4ca5..4414cb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +v0.7.2 (2018-12-24) +------------------- + +**Bug fixes** + +* Fix collections ABCs deprecation warning + + v0.7.0 (2018-05-14) ------------------- diff --git a/pydu/__init__.py b/pydu/__init__.py index d3e8c3e..deafb01 100644 --- a/pydu/__init__.py +++ b/pydu/__init__.py @@ -3,7 +3,7 @@ Useful data structures, utils for Python. """ from __future__ import absolute_import -__version__ = '0.7.1' +__version__ = '0.7.2' # Set logging handler to avoid "No handler found" warnings. diff --git a/pydu/dict.py b/pydu/dict.py index ef2522e..3840545 100644 --- a/pydu/dict.py +++ b/pydu/dict.py @@ -1,6 +1,13 @@ # coding: utf-8 import collections +try: + # Python 3 + from collections.abc import Callable, Mapping, MutableMapping +except ImportError: + # Python 2.7 + from collections import Callable, Mapping, MutableMapping + from .compat import PY2 @@ -44,11 +51,11 @@ class AttrDict(dict): return '' -class CaseInsensitiveDict(collections.MutableMapping): +class CaseInsensitiveDict(MutableMapping): """ A case-insensitive ``dict``-like object. Implements all methods and operations of - ``collections.MutableMapping`` as well as dict's ``copy``. Also + ``MutableMapping`` as well as dict's ``copy``. Also provides ``lower_items``. All keys are expected to be strings. The structure remembers the case of the last key to be set, and ``iter(instance)``, @@ -66,6 +73,7 @@ class CaseInsensitiveDict(collections.MutableMapping): operations are given keys that have equal ``.lower()``s, the behavior is undefined. """ + def __init__(self, data=None, **kwargs): self._store = {} if data is None: @@ -98,7 +106,7 @@ class CaseInsensitiveDict(collections.MutableMapping): ) def __eq__(self, other): - if isinstance(other, collections.Mapping): + if isinstance(other, Mapping): other = CaseInsensitiveDict(other) else: return NotImplemented @@ -107,7 +115,7 @@ class CaseInsensitiveDict(collections.MutableMapping): # Copy is required def copy(self): - return CaseInsensitiveDict(self._store.values()) + return CaseInsensitiveDict(self._store.values()) def __repr__(self): return '%s(%r)' % (self.__class__.__name__, dict(self.items())) @@ -144,9 +152,10 @@ class OrderedDefaultDict(collections.OrderedDict): as if they were passed to the `defaultdict` constructor, including keyword arguments. """ + def __init__(self, default_factory=None, *args, **kwds): if (default_factory is not None and - not isinstance(default_factory, collections.Callable)): + not isinstance(default_factory, Callable)): raise TypeError('First argument must be callable') super(OrderedDefaultDict, self).__init__(*args, **kwds) self.default_factory = default_factory diff --git a/pydu/list.py b/pydu/list.py index 97c7169..c086841 100644 --- a/pydu/list.py +++ b/pydu/list.py @@ -1,4 +1,11 @@ -from collections import Iterable + +try: + # Python 3 + from collections.abc import Iterable +except ImportError: + # Python 2.7 + from collections import Iterable + from pydu.compat import strbytes_types diff --git a/tests/test_system.py b/tests/test_system.py index af57f5c..c148523 100644 --- a/tests/test_system.py +++ b/tests/test_system.py @@ -197,7 +197,7 @@ class TestLink: touch(f) link(f, link_f) t1 = os.path.getctime(link_f) - time.sleep(0.1) + time.sleep(1) link(f, link_f, overwrite=True) t2 = os.path.getctime(link_f) assert t1 != t2 @@ -225,7 +225,7 @@ class TestSymLink: touch(f) symlink(f, link_f) t1 = os.lstat(link_f).st_ctime - time.sleep(0.01) + time.sleep(1) symlink(f, link_f, overwrite=True) t2 = os.lstat(link_f).st_ctime assert t1 != t2 @@ -357,7 +357,7 @@ class TestWhich: assert which('mycmd', path=path) == mycmd os.environ['PATH'] = path + os.pathsep + \ - os.environ.get('PATH', os.defpath) + os.environ.get('PATH', os.defpath) assert which('mycmd') == mycmd