Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions src/future/builtins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# The isinstance import is no longer needed. We provide it only for
# backward-compatibility with future v0.8.2. It will be removed in future v1.0.
from future.builtins.misc import (ascii, chr, hex, input, isinstance, next,
oct, open, pow, round, super)
oct, open, pow, round, super, max, min)
from future.utils import PY3

if PY3:
Expand Down Expand Up @@ -43,7 +43,7 @@
__all__ = ['filter', 'map', 'zip',
'ascii', 'chr', 'hex', 'input', 'next', 'oct', 'open', 'pow',
'round', 'super',
'bytes', 'dict', 'int', 'list', 'object', 'range', 'str',
'bytes', 'dict', 'int', 'list', 'object', 'range', 'str', 'max', 'min'
]

else:
Expand Down
9 changes: 8 additions & 1 deletion src/future/builtins/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- ``open`` (equivalent to io.open on Py2)
- ``super`` (backport of Py3's magic zero-argument super() function
- ``round`` (new "Banker's Rounding" behaviour from Py3)
- ``max`` (new max behaviour from Py3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- ``max`` (new max behaviour from Py3)
- ``max`` (new default option from Py3)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

- ``min`` (new min behaviour from Py3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- ``min`` (new min behaviour from Py3)
- ``min`` (new default option from Py3)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


``isinstance`` is also currently exported for backwards compatibility
with v0.8.2, although this has been deprecated since v0.9.
Expand Down Expand Up @@ -59,6 +61,8 @@
from future.builtins.newnext import newnext as next
from future.builtins.newround import newround as round
from future.builtins.newsuper import newsuper as super
from future.builtins.new_min_max import newmax as max
from future.builtins.new_min_max import newmin as min
from future.types.newint import newint

_SENTINEL = object()
Expand Down Expand Up @@ -89,11 +93,12 @@ def pow(x, y, z=_SENTINEL):
else:
return _builtin_pow(x+0j, y, z)


# ``future`` doesn't support Py3.0/3.1. If we ever did, we'd add this:
# callable = __builtin__.callable

__all__ = ['ascii', 'chr', 'hex', 'input', 'isinstance', 'next', 'oct',
'open', 'pow', 'round', 'super']
'open', 'pow', 'round', 'super', 'max', 'min']

else:
import builtins
Expand All @@ -109,6 +114,8 @@ def pow(x, y, z=_SENTINEL):
pow = builtins.pow
round = builtins.round
super = builtins.super
max = builtins.max
min = builtins.min

__all__ = []

Expand Down
48 changes: 48 additions & 0 deletions src/future/builtins/new_min_max.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __builtin__ import max as _builtin_max, min as _builtin_min


def newmin(*args, **kwargs):
return new_min_max(_builtin_min, *args, **kwargs)


def newmax(*args, **kwargs):
return new_min_max(_builtin_max, *args, **kwargs)


def new_min_max(_builtin_func, *args, **kwargs):
"""
To support the argument "default" introduced in python 3.4 for min and max
:param _builtin_func: builtin min or builtin max
:param args:
:param kwargs:
:return: returns the min or max based on the arguments passed
"""

for key, _ in kwargs.items():
if key not in {'key', 'default'}:
raise TypeError('Illegal argument %s', key)

if len(args) == 0:
raise TypeError

if len(args) != 1 and kwargs.get('default') is not None:
raise TypeError

if len(args) == 1:
try:
next(iter(args[0]))
except StopIteration:
if kwargs.get('default') is not None:
return kwargs.get('default')
else:
raise ValueError('iterable is an empty sequence')
if kwargs.get('key') is not None:
return _builtin_func(args[0], key=kwargs.get('key'))
else:
return _builtin_func(args[0])

if len(args) > 1:
if kwargs.get('key') is not None:
return _builtin_func(args, key=kwargs.get('key'))
else:
return _builtin_func(args)
16 changes: 15 additions & 1 deletion tests/test_future/test_builtins.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals
from future.builtins import (bytes, dict, int, range, round, str, super,
ascii, chr, hex, input, next, oct, open, pow,
filter, map, zip)
filter, map, zip, min, max)

from future.utils import PY3, exec_, native_str, implements_iterator
from future.tests.base import (unittest, skip26, expectedFailurePY2,
Expand Down Expand Up @@ -138,6 +138,7 @@ def test_round(self):
self.assertEqual(round(0.125000001, 2), 0.13)
self.assertEqual(round(123.5, 0), 124.0)
self.assertEqual(round(123.5), 124)
self.assertEqual(round(123.5), 124)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.assertEqual(round(123.5), 124)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

self.assertEqual(round(12.35, 2), 12.35)
self.assertEqual(round(12.35, 1), 12.3)
self.assertEqual(round(12.35, 0), 12.0)
Expand Down Expand Up @@ -1099,6 +1100,13 @@ def test_max(self):
self.assertEqual(max(data, key=f),
sorted(reversed(data), key=f)[-1])

self.assertEqual(max([], default=5), 5)
with self.assertRaises(TypeError):
max(None, default=5)
with self.assertRaises(TypeError):
max(1, 2, default=0)
self.assertEqual(max([], default=0), 0)

def test_min(self):
self.assertEqual(min('123123'), '1')
self.assertEqual(min(1, 2, 3), 1)
Expand Down Expand Up @@ -1140,6 +1148,12 @@ def __getitem__(self, index):
f = keys.__getitem__
self.assertEqual(min(data, key=f),
sorted(data, key=f)[0])
self.assertEqual(min([], default=5), 5)
self.assertEqual(min([], default=0), 0)
with self.assertRaises(TypeError):
max(None, default=5)
with self.assertRaises(TypeError):
max(1, 2, default=0)

def test_next(self):
it = iter(range(2))
Expand Down