bpo-34149: Behavior of the min/max with key=None by Amper · Pull Request #8328 · python/cpython

https://bugs.python.org/issue34149

I was faced with the fact that the behavior of the functions "min"/"max" and "sorted" is a little different.

For example, this code works fine:

>>> sorted([3, 2, 1], key=None)
[1, 2, 3]

But the same example for "min" and "max" doesn't work:

>>> min([3, 2, 1], key=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable

That is why the heapq library has this code:

...
def nsmallest(n, iterable, key=None):
    ...
        if key is None:
            result = min(it, default=sentinel)
        else:
            result = min(it, default=sentinel, key=key)
    ...

At the same time, there are many places where such checks are not performed for the "sorted" function. I think the behavior of the "min" / "max" / "sorted" functions should be unified. That is, they should work as if "None" is the default value for "key".

https://bugs.python.org/issue34149