Skip to content

Commit 67257a7

Browse files
authored
Merge pull request #26452 from vxst/DEP_fix_imports
DEP: Deprecate 'fix_imports' flag in numpy.save
2 parents d3a2537 + 1148cd6 commit 67257a7

3 files changed

Lines changed: 40 additions & 6 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
* The `fix_imports` keyword argument in `numpy.save` is deprecated. Since
2+
NumPy 1.17, `numpy.save` uses a pickle protocol that no longer supports
3+
Python 2, and ignored `fix_imports` keyword. This keyword is kept only
4+
for backward compatibility. It is now deprecated.

numpy/_core/tests/test_deprecations.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import numpy as np
1515
from numpy.testing import (
1616
assert_raises, assert_warns, assert_, assert_array_equal, SkipTest,
17-
KnownFailureException, break_cycles,
17+
KnownFailureException, break_cycles, temppath
1818
)
1919

2020
from numpy._core._multiarray_tests import fromstring_null_term_c_api
@@ -716,3 +716,26 @@ class TestDeprecatedDTypeParenthesizedRepeatCount(_DeprecationTestCase):
716716
@pytest.mark.parametrize("string", ["(2)i,", "(3)3S,", "f,(2)f"])
717717
def test_parenthesized_repeat_count(self, string):
718718
self.assert_deprecated(np.dtype, args=(string,))
719+
720+
721+
class TestDeprecatedSaveFixImports(_DeprecationTestCase):
722+
# Deprecated in Numpy 2.1, 2024-05
723+
message = "The 'fix_imports' flag is deprecated and has no effect."
724+
725+
def test_deprecated(self):
726+
with temppath(suffix='.npy') as path:
727+
sample_args = (path, np.array(np.zeros((1024, 10))))
728+
self.assert_not_deprecated(np.save, args=sample_args)
729+
self.assert_deprecated(np.save, args=sample_args,
730+
kwargs={'fix_imports': True})
731+
self.assert_deprecated(np.save, args=sample_args,
732+
kwargs={'fix_imports': False})
733+
for allow_pickle in [True, False]:
734+
self.assert_not_deprecated(np.save, args=sample_args,
735+
kwargs={'allow_pickle': allow_pickle})
736+
self.assert_deprecated(np.save, args=sample_args,
737+
kwargs={'allow_pickle': allow_pickle,
738+
'fix_imports': True})
739+
self.assert_deprecated(np.save, args=sample_args,
740+
kwargs={'allow_pickle': allow_pickle,
741+
'fix_imports': False})

numpy/lib/_npyio_impl.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def _save_dispatcher(file, arr, allow_pickle=None, fix_imports=None):
501501

502502

503503
@array_function_dispatch(_save_dispatcher)
504-
def save(file, arr, allow_pickle=True, fix_imports=True):
504+
def save(file, arr, allow_pickle=True, fix_imports=np._NoValue):
505505
"""
506506
Save an array to a binary file in NumPy ``.npy`` format.
507507
@@ -523,10 +523,11 @@ def save(file, arr, allow_pickle=True, fix_imports=True):
523523
compatible between different versions of Python).
524524
Default: True
525525
fix_imports : bool, optional
526-
Only useful in forcing objects in object arrays on Python 3 to be
527-
pickled in a Python 2 compatible way. If `fix_imports` is True, pickle
528-
will try to map the new Python 3 names to the old module names used in
529-
Python 2, so that the pickle data stream is readable with Python 2.
526+
The `fix_imports` flag is deprecated and has no effect.
527+
528+
.. deprecated:: 2.1
529+
This flag is ignored since NumPy 1.17 and was only needed to
530+
support loading some files in Python 2 written in Python 3.
530531
531532
See Also
532533
--------
@@ -561,6 +562,12 @@ def save(file, arr, allow_pickle=True, fix_imports=True):
561562
>>> print(a, b)
562563
# [1 2] [1 3]
563564
"""
565+
if fix_imports is not np._NoValue:
566+
# Deprecated 2024-05-16, NumPy 2.1
567+
warnings.warn(
568+
"The 'fix_imports' flag is deprecated and has no effect. "
569+
"(Deprecated in NumPy 2.1)",
570+
DeprecationWarning, stacklevel=2)
564571
if hasattr(file, 'write'):
565572
file_ctx = contextlib.nullcontext(file)
566573
else:

0 commit comments

Comments
 (0)