summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastianb@nvidia.com>2022-11-18 15:11:25 +0100
committerSebastian Berg <sebastianb@nvidia.com>2022-11-24 17:03:42 +0100
commit7c361420b4f81713f593ebbb5c924121c1f2d19d (patch)
tree4fa5c173c3e4df5dfdb9b14b9b0988db0bc69e91 /numpy
parent72af24df557802015820714aaa3d105d53216431 (diff)
downloadnumpy-7c361420b4f81713f593ebbb5c924121c1f2d19d.tar.gz
MAINT: Move set_module to numpy.core to use without C import
Diffstat (limited to 'numpy')
-rw-r--r--numpy/_globals.py16
-rw-r--r--numpy/_typing/__init__.py4
-rw-r--r--numpy/_utils.py24
-rw-r--r--numpy/core/_exceptions.py2
-rw-r--r--numpy/core/_machar.py6
-rw-r--r--numpy/core/_ufunc_config.py2
-rw-r--r--numpy/core/defchararray.py3
-rw-r--r--numpy/core/getlimits.py2
-rw-r--r--numpy/core/memmap.py2
-rw-r--r--numpy/core/numerictypes.py4
-rw-r--r--numpy/core/overrides.py19
-rw-r--r--numpy/core/records.py2
-rw-r--r--numpy/lib/_datasource.py2
-rw-r--r--numpy/lib/function_base.py6
-rw-r--r--numpy/lib/index_tricks.py2
-rw-r--r--numpy/lib/polynomial.py3
-rw-r--r--numpy/lib/type_check.py2
-rw-r--r--numpy/lib/utils.py2
-rw-r--r--numpy/linalg/linalg.py4
-rw-r--r--numpy/matrixlib/defmatrix.py3
20 files changed, 58 insertions, 52 deletions
diff --git a/numpy/_globals.py b/numpy/_globals.py
index 1e4f26cd8..555777167 100644
--- a/numpy/_globals.py
+++ b/numpy/_globals.py
@@ -17,7 +17,9 @@ motivated this module.
"""
import enum
-__ALL__ = [
+from ._utils import set_module as _set_module
+
+__all__ = [
'ModuleDeprecationWarning', 'VisibleDeprecationWarning',
'_NoValue', '_CopyMode'
]
@@ -30,6 +32,7 @@ if '_is_loaded' in globals():
_is_loaded = True
+@_set_module("numpy")
class ModuleDeprecationWarning(DeprecationWarning):
"""Module deprecation warning.
@@ -41,9 +44,7 @@ class ModuleDeprecationWarning(DeprecationWarning):
"""
-ModuleDeprecationWarning.__module__ = 'numpy'
-
-
+@_set_module("numpy")
class VisibleDeprecationWarning(UserWarning):
"""Visible deprecation warning.
@@ -54,9 +55,6 @@ class VisibleDeprecationWarning(UserWarning):
"""
-VisibleDeprecationWarning.__module__ = 'numpy'
-
-
class _NoValueType:
"""Special keyword value.
@@ -90,6 +88,7 @@ class _NoValueType:
_NoValue = _NoValueType()
+@_set_module("numpy")
class _CopyMode(enum.Enum):
"""
An enumeration for the copy modes supported
@@ -120,6 +119,3 @@ class _CopyMode(enum.Enum):
return False
raise ValueError(f"{self} is neither True nor False.")
-
-
-_CopyMode.__module__ = 'numpy'
diff --git a/numpy/_typing/__init__.py b/numpy/_typing/__init__.py
index b800c54b6..a4e763050 100644
--- a/numpy/_typing/__init__.py
+++ b/numpy/_typing/__init__.py
@@ -2,8 +2,8 @@
from __future__ import annotations
-from numpy import ufunc
-from numpy.core.overrides import set_module
+from .. import ufunc
+from .._utils import set_module
from typing import TYPE_CHECKING, final
diff --git a/numpy/_utils.py b/numpy/_utils.py
new file mode 100644
index 000000000..ea73dee07
--- /dev/null
+++ b/numpy/_utils.py
@@ -0,0 +1,24 @@
+"""
+Module for NumPy internal utilities that do not require any other import
+and should be freely available without dependencies (e.g. to avoid circular
+import by depending on the C module).
+
+"""
+
+
+def set_module(module):
+ """Private decorator for overriding __module__ on a function or class.
+
+ Example usage::
+
+ @set_module('numpy')
+ def example():
+ pass
+
+ assert example.__module__ == 'numpy'
+ """
+ def decorator(func):
+ if module is not None:
+ func.__module__ = module
+ return func
+ return decorator
diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py
index be3aa8bee..db5dfea02 100644
--- a/numpy/core/_exceptions.py
+++ b/numpy/core/_exceptions.py
@@ -5,7 +5,7 @@ in python where it's easier.
By putting the formatting in `__str__`, we also avoid paying the cost for
users who silence the exceptions.
"""
-from numpy.core.overrides import set_module
+from .._utils import set_module
def _unpack_tuple(tup):
if len(tup) == 1:
diff --git a/numpy/core/_machar.py b/numpy/core/_machar.py
index 3cc7db278..0a942d98a 100644
--- a/numpy/core/_machar.py
+++ b/numpy/core/_machar.py
@@ -7,9 +7,9 @@ Author: Pearu Peterson, September 2003
"""
__all__ = ['MachAr']
-from numpy.core.fromnumeric import any
-from numpy.core._ufunc_config import errstate
-from numpy.core.overrides import set_module
+from .fromnumeric import any
+from ._ufunc_config import errstate
+from .._utils import set_module
# Need to speed this up...especially for longfloat
diff --git a/numpy/core/_ufunc_config.py b/numpy/core/_ufunc_config.py
index 5aac7ab09..df8213095 100644
--- a/numpy/core/_ufunc_config.py
+++ b/numpy/core/_ufunc_config.py
@@ -7,7 +7,7 @@ import collections.abc
import contextlib
import contextvars
-from .overrides import set_module
+from .._utils import set_module
from .umath import (
UFUNC_BUFSIZE_DEFAULT,
ERR_IGNORE, ERR_WARN, ERR_RAISE, ERR_CALL, ERR_PRINT, ERR_LOG, ERR_DEFAULT,
diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py
index 6750e497a..d312506ff 100644
--- a/numpy/core/defchararray.py
+++ b/numpy/core/defchararray.py
@@ -16,12 +16,13 @@ The preferred alias for `defchararray` is `numpy.char`.
"""
import functools
+
+from .._utils import set_module
from .numerictypes import (
string_, unicode_, integer, int_, object_, bool_, character)
from .numeric import ndarray, compare_chararrays
from .numeric import array as narray
from numpy.core.multiarray import _vec_string
-from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.compat import asbytes
import numpy
diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py
index 2c0f462cc..c884e3d70 100644
--- a/numpy/core/getlimits.py
+++ b/numpy/core/getlimits.py
@@ -5,8 +5,8 @@ __all__ = ['finfo', 'iinfo']
import warnings
+from .._utils import set_module
from ._machar import MachAr
-from .overrides import set_module
from . import numeric
from . import numerictypes as ntypes
from .numeric import array, inf, NaN
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py
index b0d9cb3af..5071c909a 100644
--- a/numpy/core/memmap.py
+++ b/numpy/core/memmap.py
@@ -1,9 +1,9 @@
from contextlib import nullcontext
import numpy as np
+from .._utils import set_module
from .numeric import uint8, ndarray, dtype
from numpy.compat import os_fspath, is_pathlib_path
-from numpy.core.overrides import set_module
__all__ = ['memmap']
diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py
index 3d1cb6fd1..37fb8ee62 100644
--- a/numpy/core/numerictypes.py
+++ b/numpy/core/numerictypes.py
@@ -81,11 +81,11 @@ Exported symbols include:
"""
import numbers
-from numpy.core.multiarray import (
+from .multiarray import (
ndarray, array, dtype, datetime_data, datetime_as_string,
busday_offset, busday_count, is_busday, busdaycalendar
)
-from numpy.core.overrides import set_module
+from .._utils import set_module
# we add more at the bottom
__all__ = ['sctypeDict', 'sctypes',
diff --git a/numpy/core/overrides.py b/numpy/core/overrides.py
index 450464f89..6bd72063a 100644
--- a/numpy/core/overrides.py
+++ b/numpy/core/overrides.py
@@ -3,6 +3,7 @@ import collections
import functools
import os
+from .._utils import set_module
from numpy.core._multiarray_umath import (
add_docstring, implement_array_function, _get_implementing_args)
from numpy.compat._inspect import getargspec
@@ -107,24 +108,6 @@ def verify_matching_signatures(implementation, dispatcher):
'default argument values')
-def set_module(module):
- """Decorator for overriding __module__ on a function or class.
-
- Example usage::
-
- @set_module('numpy')
- def example():
- pass
-
- assert example.__module__ == 'numpy'
- """
- def decorator(func):
- if module is not None:
- func.__module__ = module
- return func
- return decorator
-
-
def array_function_dispatch(dispatcher, module=None, verify=True,
docs_from_dispatcher=False):
"""Decorator for adding dispatch with the __array_function__ protocol.
diff --git a/numpy/core/records.py b/numpy/core/records.py
index c014bc97c..0fb49e8f7 100644
--- a/numpy/core/records.py
+++ b/numpy/core/records.py
@@ -37,10 +37,10 @@ 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 numpy.core.overrides import set_module
from .arrayprint import _get_legacy_print_mode
# All of the functions allow formats to be a dtype
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py
index b7778234e..613733fa5 100644
--- a/numpy/lib/_datasource.py
+++ b/numpy/lib/_datasource.py
@@ -37,7 +37,7 @@ Example::
import os
import io
-from numpy.core.overrides import set_module
+from .._utils import set_module
_open = open
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py
index 5f59254b6..0ab49fa11 100644
--- a/numpy/lib/function_base.py
+++ b/numpy/lib/function_base.py
@@ -4,6 +4,7 @@ import re
import sys
import warnings
+from .._utils import set_module
import numpy as np
import numpy.core.numeric as _nx
from numpy.core import transpose
@@ -19,7 +20,6 @@ from numpy.core.fromnumeric import (
ravel, nonzero, partition, mean, any, sum
)
from numpy.core.numerictypes import typecodes
-from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.core.function_base import add_newdoc
from numpy.lib.twodim_base import diag
@@ -4017,7 +4017,7 @@ def percentile(a,
since Python uses 0-based indexing, the code subtracts another 1 from the
index internally.
- The following formula determines the virtual index ``i + g``, the location
+ The following formula determines the virtual index ``i + g``, the location
of the percentile in the sorted sample:
.. math::
@@ -4306,7 +4306,7 @@ def quantile(a,
since Python uses 0-based indexing, the code subtracts another 1 from the
index internally.
- The following formula determines the virtual index ``i + g``, the location
+ The following formula determines the virtual index ``i + g``, the location
of the quantile in the sorted sample:
.. math::
diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py
index 95d5e3ede..58dd394e1 100644
--- a/numpy/lib/index_tricks.py
+++ b/numpy/lib/index_tricks.py
@@ -3,6 +3,7 @@ import sys
import math
import warnings
+from .._utils import set_module
import numpy.core.numeric as _nx
from numpy.core.numeric import (
asarray, ScalarType, array, alltrue, cumprod, arange, ndim
@@ -12,7 +13,6 @@ from numpy.core.numerictypes import find_common_type, issubdtype
import numpy.matrixlib as matrixlib
from .function_base import diff
from numpy.core.multiarray import ravel_multi_index, unravel_index
-from numpy.core.overrides import set_module
from numpy.core import overrides, linspace
from numpy.lib.stride_tricks import as_strided
diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py
index 6aa708861..0f7ab0334 100644
--- a/numpy/lib/polynomial.py
+++ b/numpy/lib/polynomial.py
@@ -9,12 +9,13 @@ __all__ = ['poly', 'roots', 'polyint', 'polyder', 'polyadd',
import functools
import re
import warnings
+
+from .._utils import set_module
import numpy.core.numeric as NX
from numpy.core import (isscalar, abs, finfo, atleast_1d, hstack, dot, array,
ones)
from numpy.core import overrides
-from numpy.core.overrides import set_module
from numpy.lib.twodim_base import diag, vander
from numpy.lib.function_base import trim_zeros
from numpy.lib.type_check import iscomplex, real, imag, mintypecode
diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py
index 94d525f51..0dc014d76 100644
--- a/numpy/lib/type_check.py
+++ b/numpy/lib/type_check.py
@@ -9,9 +9,9 @@ __all__ = ['iscomplexobj', 'isrealobj', 'imag', 'iscomplex',
'typename', 'asfarray', 'mintypecode',
'common_type']
+from .._utils import set_module
import numpy.core.numeric as _nx
from numpy.core.numeric import asarray, asanyarray, isnan, zeros
-from numpy.core.overrides import set_module
from numpy.core import overrides
from .ufunclike import isneginf, isposinf
diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py
index afde8cc60..2a9d30b16 100644
--- a/numpy/lib/utils.py
+++ b/numpy/lib/utils.py
@@ -6,8 +6,8 @@ import re
import warnings
import functools
+from .._utils import set_module
from numpy.core.numerictypes import issubclass_, issubsctype, issubdtype
-from numpy.core.overrides import set_module
from numpy.core import ndarray, ufunc, asarray
import numpy as np
diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py
index 697193321..ee0fe6166 100644
--- a/numpy/linalg/linalg.py
+++ b/numpy/linalg/linalg.py
@@ -18,6 +18,7 @@ import functools
import operator
import warnings
+from .._utils import set_module
from numpy.core import (
array, asarray, zeros, empty, empty_like, intc, single, double,
csingle, cdouble, inexact, complexfloating, newaxis, all, Inf, dot,
@@ -28,7 +29,6 @@ from numpy.core import (
reciprocal
)
from numpy.core.multiarray import normalize_axis_index
-from numpy.core.overrides import set_module
from numpy.core import overrides
from numpy.lib.twodim_base import triu, eye
from numpy.linalg import _umath_linalg
@@ -943,7 +943,7 @@ def qr(a, mode='reduced'):
return wrap(a)
# mc is the number of columns in the resulting q
- # matrix. If the mode is complete then it is
+ # matrix. If the mode is complete then it is
# same as number of rows, and if the mode is reduced,
# then it is the minimum of number of rows and columns.
if mode == 'complete' and m > n:
diff --git a/numpy/matrixlib/defmatrix.py b/numpy/matrixlib/defmatrix.py
index a414ee9bb..d029b13fb 100644
--- a/numpy/matrixlib/defmatrix.py
+++ b/numpy/matrixlib/defmatrix.py
@@ -3,9 +3,10 @@ __all__ = ['matrix', 'bmat', 'mat', 'asmatrix']
import sys
import warnings
import ast
+
+from .._utils import set_module
import numpy.core.numeric as N
from numpy.core.numeric import concatenate, isscalar
-from numpy.core.overrides import set_module
# While not in __all__, matrix_power used to be defined here, so we import
# it for backward compatibility.
from numpy.linalg import matrix_power