Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 4 additions & 0 deletions doc/release/upcoming_changes/26452.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
* The `fix_imports` keyword argument in `numpy.save` is deprecated. Since
NumPy 1.17, `numpy.save` uses a pickle protocol that no longer supports
Python 2, and ignored `fix_imports` keyword. This keyword is kept only
for backward compatibility. It is now deprecated.
25 changes: 24 additions & 1 deletion numpy/_core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import numpy as np
from numpy.testing import (
assert_raises, assert_warns, assert_, assert_array_equal, SkipTest,
KnownFailureException, break_cycles,
KnownFailureException, break_cycles, temppath
)

from numpy._core._multiarray_tests import fromstring_null_term_c_api
Expand Down Expand Up @@ -716,3 +716,26 @@ class TestDeprecatedDTypeParenthesizedRepeatCount(_DeprecationTestCase):
@pytest.mark.parametrize("string", ["(2)i,", "(3)3S,", "f,(2)f"])
def test_parenthesized_repeat_count(self, string):
self.assert_deprecated(np.dtype, args=(string,))


class TestDeprecatedSaveFixImports(_DeprecationTestCase):
# Deprecated in Numpy 2.1, 2024-05
message = "The 'fix_imports' flag is deprecated and has no effect."

def test_deprecated(self):
with temppath(suffix='.npy') as path:
sample_args = (path, np.array(np.zeros((1024, 10))))
self.assert_not_deprecated(np.save, args=sample_args)
self.assert_deprecated(np.save, args=sample_args,
kwargs={'fix_imports': True})
self.assert_deprecated(np.save, args=sample_args,
kwargs={'fix_imports': False})
for allow_pickle in [True, False]:
self.assert_not_deprecated(np.save, args=sample_args,
kwargs={'allow_pickle': allow_pickle})
self.assert_deprecated(np.save, args=sample_args,
kwargs={'allow_pickle': allow_pickle,
'fix_imports': True})
self.assert_deprecated(np.save, args=sample_args,
kwargs={'allow_pickle': allow_pickle,
'fix_imports': False})
17 changes: 12 additions & 5 deletions numpy/lib/_npyio_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ def _save_dispatcher(file, arr, allow_pickle=None, fix_imports=None):


@array_function_dispatch(_save_dispatcher)
def save(file, arr, allow_pickle=True, fix_imports=True):
def save(file, arr, allow_pickle=True, fix_imports=np._NoValue):
"""
Save an array to a binary file in NumPy ``.npy`` format.

Expand All @@ -523,10 +523,11 @@ def save(file, arr, allow_pickle=True, fix_imports=True):
compatible between different versions of Python).
Default: True
fix_imports : bool, optional
Only useful in forcing objects in object arrays on Python 3 to be
pickled in a Python 2 compatible way. If `fix_imports` is True, pickle
will try to map the new Python 3 names to the old module names used in
Python 2, so that the pickle data stream is readable with Python 2.
The `fix_imports` flag is deprecated and has no effect.

.. deprecated:: 2.1
This flag is ignored since NumPy 1.17 and was only needed to
support loading some files in Python 2 written in Python 3.

See Also
--------
Expand Down Expand Up @@ -561,6 +562,12 @@ def save(file, arr, allow_pickle=True, fix_imports=True):
>>> print(a, b)
# [1 2] [1 3]
"""
if fix_imports is not np._NoValue:
# Deprecated 2024-05-16, NumPy 2.1
warnings.warn(
Comment thread
seberg marked this conversation as resolved.
"The 'fix_imports' flag is deprecated and has no effect. "
"(Deprecated in NumPy 2.1)",
DeprecationWarning, stacklevel=2)
if hasattr(file, 'write'):
file_ctx = contextlib.nullcontext(file)
else:
Expand Down