]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-34149: Behavior of the min/max with key=None (GH-8328)
authorAlexander Marshalov <_@marshalov.org>
Tue, 24 Jul 2018 03:58:21 +0000 (10:58 +0700)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 24 Jul 2018 03:58:21 +0000 (20:58 -0700)
Improve consistency with the signature for sorted(), heapq.nsmallest(), heapq.nlargest(), and itertools.groupby().

Doc/library/functions.rst
Lib/heapq.py
Lib/test/test_builtin.py
Misc/NEWS.d/next/Core and Builtins/2018-07-23-21-49-05.bpo-34149.WSV-_g.rst [new file with mode: 0644]
Python/bltinmodule.c

index 68a5dce5d09b720cb2b498585559111f17f7c4d2..e52b0900156ebb3535de2dd82103da52112fcffb 100644 (file)
@@ -869,6 +869,9 @@ are always available.  They are listed here in alphabetical order.
    .. versionadded:: 3.4
       The *default* keyword-only argument.
 
+   .. versionchanged:: 3.8
+      The *key* can be ``None``.
+
 
 .. _func-memoryview:
 .. function:: memoryview(obj)
@@ -903,6 +906,9 @@ are always available.  They are listed here in alphabetical order.
    .. versionadded:: 3.4
       The *default* keyword-only argument.
 
+   .. versionchanged:: 3.8
+      The *key* can be ``None``.
+
 
 .. function:: next(iterator[, default])
 
index b31f4186cf66efe130a6ef4eb28ff35edd524e0c..0e3555cf9118eaabb5033bbccbcf2f3607716221 100644 (file)
@@ -468,10 +468,7 @@ def nsmallest(n, iterable, key=None):
     if n == 1:
         it = iter(iterable)
         sentinel = object()
-        if key is None:
-            result = min(it, default=sentinel)
-        else:
-            result = min(it, default=sentinel, key=key)
+        result = min(it, default=sentinel, key=key)
         return [] if result is sentinel else [result]
 
     # When n>=size, it's faster to use sorted()
@@ -531,10 +528,7 @@ def nlargest(n, iterable, key=None):
     if n == 1:
         it = iter(iterable)
         sentinel = object()
-        if key is None:
-            result = max(it, default=sentinel)
-        else:
-            result = max(it, default=sentinel, key=key)
+        result = max(it, default=sentinel, key=key)
         return [] if result is sentinel else [result]
 
     # When n>=size, it's faster to use sorted()
index 8f91bc9bf919b742afdf6d17236855bbb175ce65..233c579356530ca94793d507210cae644160b0d3 100644 (file)
@@ -905,6 +905,8 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(max((), default=1, key=neg), 1)
         self.assertEqual(max((1, 2), default=3, key=neg), 1)
 
+        self.assertEqual(max((1, 2), key=None), 2)
+
         data = [random.randrange(200) for i in range(100)]
         keys = dict((elem, random.randrange(50)) for elem in data)
         f = keys.__getitem__
@@ -957,6 +959,8 @@ class BuiltinTest(unittest.TestCase):
         self.assertEqual(min((), default=1, key=neg), 1)
         self.assertEqual(min((1, 2), default=1, key=neg), 2)
 
+        self.assertEqual(min((1, 2), key=None), 1)
+
         data = [random.randrange(200) for i in range(100)]
         keys = dict((elem, random.randrange(50)) for elem in data)
         f = keys.__getitem__
diff --git a/Misc/NEWS.d/next/Core and Builtins/2018-07-23-21-49-05.bpo-34149.WSV-_g.rst b/Misc/NEWS.d/next/Core and Builtins/2018-07-23-21-49-05.bpo-34149.WSV-_g.rst
new file mode 100644 (file)
index 0000000..9672bcf
--- /dev/null
@@ -0,0 +1 @@
+Fix min and max functions to get default behavior when key is None.
index 88a4bf991d88a408b3daa09df6c49ef130bb4f07..7bb5687d9ae0ac8e498c4c13bbbc203c490389a3 100644 (file)
@@ -1639,6 +1639,10 @@ min_max(PyObject *args, PyObject *kwds, int op)
         return NULL;
     }
 
+    if (keyfunc == Py_None) {
+        keyfunc = NULL;
+    }
+
     maxitem = NULL; /* the result */
     maxval = NULL;  /* the value associated with the result */
     while (( item = PyIter_Next(it) )) {