classification
Title: max function reports type errors in incorrect order
Type: behavior Stage:
Components: Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: eric.smith, nkobald, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-12-04 00:57 by nkobald, last changed 2020-12-04 16:18 by eric.smith.

Messages (3)
msg382463 - (view) Author: Nicholas Kobald (nkobald) Date: 2020-12-04 00:57
I'm not _sure_ this is a bug, but I thought the behaviour was a bit odd. If you run max with a str and an int in different orders, you get this behaviour: 

>>> max(123, 'hello')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int'
>>> max(123, 'hello')

>>> max('hello', 123)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'int' and 'str'

Note that order of the error message: 'int' and 'str' is the inverse of the order the str and int really are. 

Did a search for max and didn't find this.
msg382469 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-12-04 07:53
This is because the code of the main() function is roughly equivalent to

    result = next(iter)
    for item in iter:
        if item > result:
            result = item

It can be changed to use "result < item" instead of "item > result", but it can has subtle differences in behavior. I am not sure what is better.

If we change it, it can only be in 3.10, because of the risk of affecting user code.
msg382501 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-12-04 16:18
I don't think making a change would be worth the risk.
History
Date User Action Args
2020-12-04 16:18:21eric.smithsetpriority: low -> normal
versions: + Python 3.8, - Python 3.10
nosy: + eric.smith

messages: + msg382501
2020-12-04 07:53:27serhiy.storchakasetpriority: normal -> low
versions: + Python 3.10, - Python 3.8
nosy: + serhiy.storchaka

messages: + msg382469
2020-12-04 00:57:36nkobaldcreate