Skip to content

Commit

Permalink
DEP: deprecate compat and selected lib utils (#23830)
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
mtsokol committed Jun 14, 2023
1 parent 6cd60bd commit 12efa8e
Show file tree
Hide file tree
Showing 41 changed files with 154 additions and 137 deletions.
18 changes: 11 additions & 7 deletions numpy/__init__.py
Expand Up @@ -135,10 +135,10 @@

# mapping of {name: (value, deprecation_msg)}
__deprecated_attrs__ = {}
__expired_functions__ = {}

from . import core
from .core import *
from . import compat
from . import exceptions
from . import dtypes
from . import lib
Expand Down Expand Up @@ -259,12 +259,13 @@
# a warning, and calling the function will raise an exception.
_financial_names = ['fv', 'ipmt', 'irr', 'mirr', 'nper', 'npv', 'pmt',
'ppmt', 'pv', 'rate']
__expired_functions__ = {
name: (f'In accordance with NEP 32, the function {name} was removed '
'from NumPy version 1.20. A replacement for this function '
'is available in the numpy_financial library: '
'https://pypi.org/project/numpy-financial')
for name in _financial_names}
for name in _financial_names:
__expired_functions__[name] = (
f'In accordance with NEP 32, the function {name} was removed '
'from NumPy version 1.20. A replacement for this function '
'is available in the numpy_financial library: '
'https://pypi.org/project/numpy-financial'
)

# Filter out Cython harmless warnings
warnings.filterwarnings("ignore", message="numpy.dtype size changed")
Expand Down Expand Up @@ -318,6 +319,9 @@ def _expired(*args, **kwds):
elif attr == 'Tester':
"Removed in NumPy 1.25.0"
raise RuntimeError("Tester was removed in NumPy 1.25.")
elif attr == "compat":
import numpy.compat as compat
return compat

raise AttributeError("module {!r} has no attribute "
"{!r}".format(__name__, attr))
Expand Down
18 changes: 18 additions & 0 deletions numpy/_utils/_convertions.py
@@ -0,0 +1,18 @@
"""
A set of methods retained from np.compat module that
are still used across codebase.
"""

__all__ = ["asunicode", "asbytes"]


def asunicode(s):
if isinstance(s, bytes):
return s.decode('latin1')
return str(s)


def asbytes(s):
if isinstance(s, bytes):
return s
return str(s).encode('latin1')
10 changes: 10 additions & 0 deletions numpy/compat/__init__.py
Expand Up @@ -7,13 +7,23 @@
* compatibility
* we may only need a small subset of the copied library/module
This module is deprecated since 1.26.0 and will be removed in future versions.
"""

import warnings

from .._utils import _inspect
from .._utils._inspect import getargspec, formatargspec
from . import py3k
from .py3k import *

warnings.warn(
"`np.compat`, which was used during the Python 2 to 3 transition,"
" is deprecated since 1.26.0, and will be removed",
DeprecationWarning, stacklevel=2
)

__all__ = []
__all__.extend(_inspect.__all__)
__all__.extend(py3k.__all__)
22 changes: 0 additions & 22 deletions numpy/compat/tests/test_compat.py

This file was deleted.

5 changes: 3 additions & 2 deletions numpy/core/_methods.py
Expand Up @@ -3,6 +3,8 @@
and the Python code for the NumPy-namespace function
"""
import os
import pickle
import warnings
from contextlib import nullcontext

Expand All @@ -13,7 +15,6 @@
from numpy.core import _exceptions
from numpy.core._ufunc_config import _no_nep50_warning
from numpy._globals import _NoValue
from numpy.compat import pickle, os_fspath

# save those O(100) nanoseconds!
umr_maximum = um.maximum.reduce
Expand Down Expand Up @@ -226,7 +227,7 @@ def _dump(self, file, protocol=2):
if hasattr(file, 'write'):
ctx = nullcontext(file)
else:
ctx = open(os_fspath(file), "wb")
ctx = open(os.fspath(file), "wb")
with ctx as f:
pickle.dump(self, f, protocol=protocol)

Expand Down
10 changes: 5 additions & 5 deletions numpy/core/_type_aliases.py
Expand Up @@ -17,7 +17,6 @@
"""

from numpy.compat import unicode
from numpy.core._string_helpers import english_lower
from numpy.core.multiarray import typeinfo, dtype
from numpy.core._dtype import _kind_name
Expand Down Expand Up @@ -195,10 +194,11 @@ def _set_up_aliases():


sctypes = {'int': [],
'uint':[],
'float':[],
'complex':[],
'others':[bool, object, bytes, unicode, void]}
'uint': [],
'float': [],
'complex': [],
'others': [bool, object, bytes, str, void]}


def _add_array_type(typename, bits):
try:
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/defchararray.py
Expand Up @@ -24,7 +24,7 @@
from .numeric import array as narray
from numpy.core.multiarray import _vec_string
from numpy.core import overrides
from numpy.compat import asbytes
from numpy._utils._convertions import asbytes
import numpy

__all__ = [
Expand Down Expand Up @@ -86,6 +86,7 @@ def _clean_args(*args):
newargs.append(chk)
return newargs


def _get_num_chars(a):
"""
Helper function that returns the number of characters per field in
Expand Down
8 changes: 5 additions & 3 deletions numpy/core/memmap.py
Expand Up @@ -3,7 +3,6 @@
import numpy as np
from .._utils import set_module
from .numeric import uint8, ndarray, dtype
from numpy.compat import os_fspath, is_pathlib_path

__all__ = ['memmap']

Expand Down Expand Up @@ -231,7 +230,10 @@ def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
if hasattr(filename, 'read'):
f_ctx = nullcontext(filename)
else:
f_ctx = open(os_fspath(filename), ('r' if mode == 'c' else mode)+'b')
f_ctx = open(
os.fspath(filename),
('r' if mode == 'c' else mode)+'b'
)

with f_ctx as fid:
fid.seek(0, 2)
Expand Down Expand Up @@ -282,7 +284,7 @@ def __new__(subtype, filename, dtype=uint8, mode='r+', offset=0,
self.offset = offset
self.mode = mode

if is_pathlib_path(filename):
if isinstance(filename, os.PathLike):
# special case - if we were constructed with a pathlib.path,
# then filename is a path object, not a string
self.filename = filename.resolve()
Expand Down
1 change: 0 additions & 1 deletion numpy/core/numerictypes.py
Expand Up @@ -116,7 +116,6 @@
# we don't export these for import *, but we do want them accessible
# as numerictypes.bool, etc.
from builtins import bool, int, float, complex, object, str, bytes
from numpy.compat import long, unicode


# We use this later
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/records.py
Expand Up @@ -33,14 +33,14 @@
array([2., 2.])
"""
import os
import warnings
from collections import Counter
from contextlib import nullcontext

from .._utils import set_module
from . import numeric as sb
from . import numerictypes as nt
from numpy.compat import os_fspath
from .arrayprint import _get_legacy_print_mode

# All of the functions allow formats to be a dtype
Expand Down Expand Up @@ -913,7 +913,7 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None,
ctx = nullcontext(fd)
else:
# open file
ctx = open(os_fspath(fd), 'rb')
ctx = open(os.fspath(fd), 'rb')

with ctx as fd:
if offset > 0:
Expand Down
1 change: 0 additions & 1 deletion numpy/core/setup.py
Expand Up @@ -12,7 +12,6 @@
from numpy.distutils.msvccompiler import lib_opts_if_msvc
from distutils.dep_util import newer
from sysconfig import get_config_var
from numpy.compat import npy_load_module
from setup_common import * # noqa: F403

# Set to True to enable relaxed strides checking. This (mostly) means
Expand Down
4 changes: 2 additions & 2 deletions numpy/core/src/multiarray/methods.h
Expand Up @@ -15,11 +15,11 @@ NpyPath_PathlikeToFspath(PyObject *file)
{
static PyObject *os_PathLike = NULL;
static PyObject *os_fspath = NULL;
npy_cache_import("numpy.compat", "os_PathLike", &os_PathLike);
npy_cache_import("os", "PathLike", &os_PathLike);
if (os_PathLike == NULL) {
return NULL;
}
npy_cache_import("numpy.compat", "os_fspath", &os_fspath);
npy_cache_import("os", "fspath", &os_fspath);
if (os_fspath == NULL) {
return NULL;
}
Expand Down
7 changes: 4 additions & 3 deletions numpy/core/tests/test_datetime.py
@@ -1,14 +1,15 @@
import datetime
import pickle

import pytest

import numpy
import numpy as np
import datetime
import pytest
from numpy.testing import (
IS_WASM,
assert_, assert_equal, assert_raises, assert_warns, suppress_warnings,
assert_raises_regex, assert_array_equal,
)
from numpy.compat import pickle

# Use pytz to test out various time zones if available
try:
Expand Down
9 changes: 9 additions & 0 deletions numpy/core/tests/test_deprecations.py
Expand Up @@ -784,3 +784,12 @@ def test_deprecated_np_math(self):

def test_deprecated_np_lib_math(self):
self.assert_deprecated(lambda: np.lib.math)


class TestLibImports(_DeprecationTestCase):
# Deprecated in Numpy 1.26.0, 2023-09
def test_lib_functions_deprecation_call(self):
from numpy.lib import byte_bounds, safe_eval, who
self.assert_deprecated(lambda: byte_bounds(np.array([1])))
self.assert_deprecated(lambda: safe_eval("None"))
self.assert_deprecated(lambda: who())
7 changes: 3 additions & 4 deletions numpy/core/tests/test_dtype.py
Expand Up @@ -5,6 +5,7 @@
import gc
import types
from typing import Any
import pickle

import numpy as np
import numpy.dtypes
Expand All @@ -13,7 +14,6 @@
from numpy.testing import (
assert_, assert_equal, assert_array_equal, assert_raises, HAS_REFCOUNT,
IS_PYSTON, _OLD_PROMOTION)
from numpy.compat import pickle
from itertools import permutations
import random

Expand All @@ -33,8 +33,7 @@ def assert_dtype_not_equal(a, b):
"two different types hash to the same value !")

class TestBuiltin:
@pytest.mark.parametrize('t', [int, float, complex, np.int32, str, object,
np.compat.unicode])
@pytest.mark.parametrize('t', [int, float, complex, np.int32, str, object])
def test_run(self, t):
"""Only test hash runs at all."""
dt = np.dtype(t)
Expand Down Expand Up @@ -1299,7 +1298,7 @@ def check_pickling(self, dtype):
assert_equal(x[0], y[0])

@pytest.mark.parametrize('t', [int, float, complex, np.int32, str, object,
np.compat.unicode, bool])
bool])
def test_builtin(self, t):
self.check_pickling(np.dtype(t))

Expand Down
4 changes: 1 addition & 3 deletions numpy/core/tests/test_multiarray.py
Expand Up @@ -15,9 +15,7 @@
import weakref
import pytest
from contextlib import contextmanager

from numpy.compat import pickle

import pickle
import pathlib
import builtins
from decimal import Decimal
Expand Down
5 changes: 3 additions & 2 deletions numpy/core/tests/test_overrides.py
Expand Up @@ -4,15 +4,16 @@
import tempfile
from io import StringIO
from unittest import mock
import pickle

import pytest

import numpy as np
from numpy.testing import (
assert_, assert_equal, assert_raises, assert_raises_regex)
from numpy.core.overrides import (
_get_implementing_args, array_function_dispatch,
verify_matching_signatures)
from numpy.compat import pickle
import pytest


def _return_not_implemented(self, *args, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_records.py
Expand Up @@ -3,14 +3,15 @@
from io import BytesIO
from os import path
from pathlib import Path
import pickle

import pytest

import numpy as np
from numpy.testing import (
assert_, assert_equal, assert_array_equal, assert_array_almost_equal,
assert_raises, temppath,
)
from numpy.compat import pickle


class TestFromrecords:
Expand Down
3 changes: 2 additions & 1 deletion numpy/core/tests/test_regression.py
Expand Up @@ -6,6 +6,7 @@
from os import path
from io import BytesIO
from itertools import chain
import pickle

import numpy as np
from numpy.testing import (
Expand All @@ -15,7 +16,7 @@
_assert_valid_refcount, HAS_REFCOUNT, IS_PYSTON, IS_WASM
)
from numpy.testing._private.utils import _no_tracing, requires_memory
from numpy.compat import asbytes, asunicode, pickle
from numpy._utils._convertions import asbytes, asunicode


class TestRegression:
Expand Down

0 comments on commit 12efa8e

Please sign in to comment.