From 165e06f7cbb9fd1861aea0cc08ebd331597afff5 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Sun, 5 Oct 2014 16:28:21 -0400 Subject: Address PR comments --- documentation/index.rst | 6 +++--- six.py | 22 ++++++---------------- test_six.py | 30 ++++++++++++++++-------------- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/documentation/index.rst b/documentation/index.rst index 594976b..1cd87de 100644 --- a/documentation/index.rst +++ b/documentation/index.rst @@ -205,21 +205,21 @@ functions and methods is the stdlib :mod:`py3:inspect` module. .. function:: viewkeys(dictionary, **kwargs) - Returns a view over *dictionary*\'s keys. This replaces + Return a view over *dictionary*\'s keys. This replaces ``dictionary.viewkeys()`` on Python 2.7 and ``dictionary.keys()`` on Python 3. *kwargs* are passed through to the underlying method. .. function:: viewvalues(dictionary, **kwargs) - Returns a view over *dictionary*\'s values. This replaces + Return a view over *dictionary*\'s values. This replaces ``dictionary.viewvalues()`` on Python 2.7 and ``dictionary.values()`` on Python 3. *kwargs* are passed through to the underlying method. .. function:: viewitems(dictionary, **kwargs) - Returns a view over *dictionary*\'s items. This replaces + Return a view over *dictionary*\'s items. This replaces ``dictionary.viewitems()`` on Python 2.7 and ``dictionary.items()`` on Python 3. *kwargs* are passed through to the underlying method. diff --git a/six.py b/six.py index 5efb62b..4252fdf 100644 --- a/six.py +++ b/six.py @@ -555,14 +555,11 @@ if PY3: def iterlists(d, **kw): return iter(d.lists(**kw)) - def viewkeys(d, **kw): - return d.keys(**kw) + viewkeys = operator.methodcaller("keys") - def viewvalues(d, **kw): - return d.values(**kw) + viewvalues = operator.methodcaller("values") - def viewitems(d, **kw): - return d.items(**kw) + viewitems = operator.methodcaller("items") else: def iterkeys(d, **kw): return iter(d.iterkeys(**kw)) @@ -576,14 +573,11 @@ else: def iterlists(d, **kw): return iter(d.iterlists(**kw)) - def viewkeys(d, **kw): - return d.viewkeys(**kw) + viewkeys = operator.methodcaller("viewkeys") - def viewvalues(d, **kw): - return d.viewvalues(**kw) + viewvalues = operator.methodcaller("viewvalues") - def viewitems(d, **kw): - return d.viewitems(**kw) + viewitems = operator.methodcaller("viewitems") _add_doc(iterkeys, "Return an iterator over the keys of a dictionary.") _add_doc(itervalues, "Return an iterator over the values of a dictionary.") @@ -591,10 +585,6 @@ _add_doc(iteritems, "Return an iterator over the (key, value) pairs of a dictionary.") _add_doc(iterlists, "Return an iterator over the (key, [values]) pairs of a dictionary.") -_add_doc(viewkeys, "Return a view over the keys of a dictionary.") -_add_doc(viewvalues, "Retun a view over the values of a dictionary.") -_add_doc(viewitems, - "Return a view over the (key, value) pairs of a dictionary.") if PY3: diff --git a/test_six.py b/test_six.py index 4ca3200..6b2b97e 100644 --- a/test_six.py +++ b/test_six.py @@ -389,9 +389,9 @@ def test_dictionary_iterators(monkeypatch): monkeypatch.undo() -def test_dictionary_views(monkeypatch): - if sys.version_info[:2] <= (2, 6): - py.test.skip("view methods on dictionaries only available on 2.7+") +@py.test.mark.skipIf(sys.version_info[:2] <= (2, 6), + "view methods on dictionaries only available on 2.7+") +def test_dictionary_views(): def stock_method_name(viewwhat): """Given a method suffix like "keys" or "values", return the name of the dict method that delivers those on the version of Python @@ -401,22 +401,24 @@ def test_dictionary_views(monkeypatch): return 'view' + viewwhat class MyDict(dict): - pass + def viewkeys(self, *args, **kw): + record.append(kw) + return super(MyDict, self).viewkeys() - d = MyDict(zip(range(10), reversed(range(10)))) + def keys(self, *args, **kw): + record.append(kw) + return super().keys() + + d = dict(zip(range(10), reversed(range(10)))) for name in "keys", "values", "items": meth = getattr(six, "view" + name) view = meth(d) assert set(view) == set(getattr(d, name)()) - record = [] - def with_kw(*args, **kw): - record.append(kw["kw"]) - return old(*args) - old = getattr(MyDict, stock_method_name(name)) - monkeypatch.setattr(MyDict, stock_method_name(name), with_kw) - meth(d, kw=42) - assert record == [42] - monkeypatch.undo() + d = MyDict() + record = [] + six.viewkeys(d, kw=42) + assert record == [{'kw': 42}] + def test_advance_iterator(): assert six.next is six.advance_iterator -- cgit v1.2.1