diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2016-03-13 09:29:47 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2016-03-13 09:29:47 -0600 |
commit | 3eff0ae052c954081e542f14be5891e1310e71f7 (patch) | |
tree | 92fdad065f42e49b5bd67f5ae94576382590737d /numpy/core | |
parent | 5b9162853bab2ef0cc8e71576f5f3c754d0a315c (diff) | |
parent | 3af411b270c8ec57ca9ced6fb69029cd86a8e417 (diff) | |
download | numpy-3eff0ae052c954081e542f14be5891e1310e71f7.tar.gz |
Merge pull request #7328 from emilienkofman/master
DEP: Deprecated using a float index in linspace
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/function_base.py | 17 | ||||
-rw-r--r-- | numpy/core/tests/test_deprecations.py | 16 |
2 files changed, 32 insertions, 1 deletions
diff --git a/numpy/core/function_base.py b/numpy/core/function_base.py index 21ca1af01..750fbe838 100644 --- a/numpy/core/function_base.py +++ b/numpy/core/function_base.py @@ -1,10 +1,24 @@ from __future__ import division, absolute_import, print_function +import warnings +import operator + __all__ = ['logspace', 'linspace'] from . import numeric as _nx from .numeric import result_type, NaN, shares_memory, MAY_SHARE_BOUNDS, TooHardError +def _index_deprecate(i, stacklevel=2): + try: + i = operator.index(i) + except TypeError: + msg = ("object of type {} cannot be safely interpreted as " + "an integer.".format(type(i))) + i = int(i) + stacklevel += 1 + warnings.warn(msg, DeprecationWarning, stacklevel=stacklevel) + return i + def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): """ @@ -81,7 +95,8 @@ def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None): >>> plt.show() """ - num = int(num) + # 2016-02-25, 1.12 + num = _index_deprecate(num) if num < 0: raise ValueError("Number of samples, %s, must be non-negative." % num) div = (num - 1) if endpoint else num diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index ee65bc432..3a4c05136 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -578,6 +578,22 @@ class TestArrayDataAttributeAssignmentDeprecation(_DeprecationTestCase): self.assert_deprecated(a.__setattr__, args=('data', b.data)) +class TestLinspaceInvalidNumParameter(_DeprecationTestCase): + """Argument to the num parameter in linspace that cannot be + safely interpreted as an integer is deprecated in 1.12.0. + + Argument to the num parameter in linspace that cannot be + safely interpreted as an integer should not be allowed. + In the interest of not breaking code that passes + an argument that could still be interpreted as an integer, a + DeprecationWarning will be issued for the time being to give + developers time to refactor relevant code. + """ + def test_float_arg(self): + # 2016-02-25, PR#7328 + self.assert_deprecated(np.linspace, args=(0, 10, 2.5)) + + class TestTestDeprecated(object): def test_assert_deprecated(self): test_case_instance = _DeprecationTestCase() |