Skip to content

bpo-34149: Behavior of the min/max with key=None #8328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])

Expand Down
10 changes: 2 additions & 8 deletions Lib/heapq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ def __getitem__(self, index):
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__
Expand Down Expand Up @@ -957,6 +959,8 @@ def __getitem__(self, index):
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__
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix min and max functions to get default behavior when key is None.
4 changes: 4 additions & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) )) {
Expand Down