Issue 19768: Not so correct error message when giving incorrect type to maxlen in deque (original) (raw)

from collections import deque deque('abc', maxlen='a') Traceback (most recent call last): File "", line 1, in TypeError: an integer is required

But it's a lie. You can give None to maxlen

deque('abc', maxlen=None) deque(['a', 'b', 'c'])

maxlen with None value means unlimited.

You can give boolean value too to maxlen.

deque('abc', maxlen=True) deque(['c'], maxlen=1)

But since we use boolean and integer interchangeably in Python, so this should not matter in this case.

So, after the patch:

deque('abc', maxlen='a') Traceback (most recent call last): File "", line 1, in ValueError: maxlen must be integer or None

Don't worry, I only overrode the TypeError one. So overflow error should be kept intact.

deque('abc', maxlen=2**68) Traceback (most recent call last): File "", line 1, in OverflowError: Python int too large to convert to C ssize_t

I did not override the negative integer error message, because I assume when people give negative integer, they are in the mindset of giving integer value to maxlen.

deque('abc', maxlen=-3) Traceback (most recent call last): File "", line 1, in ValueError: maxlen must be non-negative

I believe that returning a TypeError instead of a ValueError is better in this situation. Technically, passing 'a' as maxlen makes that value inappropiate, thus the use of TypeError. It will also be backward compatible. Also, your patch needs test updates.