diff options
Diffstat (limited to 'numpy')
27 files changed, 69 insertions, 58 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/__init__.py b/numpy/_utils/__init__.py new file mode 100644 index 000000000..60703f145 --- /dev/null +++ b/numpy/_utils/__init__.py @@ -0,0 +1,27 @@ +""" +This is a module for defining private helpers which do not depend on the +rest of NumPy. + +Everything in here must be self-contained so that it can be +imported anywhere else without creating circular imports. +If a utility requires the import of NumPy, it probably belongs +in ``numpy.core``. +""" + + +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/compat/_inspect.py b/numpy/_utils/_inspect.py index 9a874a71d..9a874a71d 100644 --- a/numpy/compat/_inspect.py +++ b/numpy/_utils/_inspect.py diff --git a/numpy/compat/_pep440.py b/numpy/_utils/_pep440.py index 73d0afb5e..73d0afb5e 100644 --- a/numpy/compat/_pep440.py +++ b/numpy/_utils/_pep440.py diff --git a/numpy/compat/__init__.py b/numpy/compat/__init__.py index afee621b8..504f8b003 100644 --- a/numpy/compat/__init__.py +++ b/numpy/compat/__init__.py @@ -8,9 +8,10 @@ extensions, which may be included for the following reasons: * we may only need a small subset of the copied library/module """ -from . import _inspect + +from .._utils import _inspect +from .._utils._inspect import getargspec, formatargspec from . import py3k -from ._inspect import getargspec, formatargspec from .py3k import * __all__ = [] 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 3f1bec493..59d71014f 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 45818b326..5baeb97fe 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 21341b2e3..79c695455 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 21a00ac6f..7a5948025 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 6d3680915..46e1fbe2c 100644 --- a/numpy/core/overrides.py +++ b/numpy/core/overrides.py @@ -3,9 +3,10 @@ import collections import functools import os +from .._utils import set_module +from .._utils._inspect import getargspec from numpy.core._multiarray_umath import ( add_docstring, implement_array_function, _get_implementing_args) -from numpy.compat._inspect import getargspec ARRAY_FUNCTIONS = set() @@ -109,24 +110,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/core/tests/test_cython.py b/numpy/core/tests/test_cython.py index f4aac4a36..e916adceb 100644 --- a/numpy/core/tests/test_cython.py +++ b/numpy/core/tests/test_cython.py @@ -14,7 +14,7 @@ try: except ImportError: cython = None else: - from numpy.compat import _pep440 + from numpy._utils import _pep440 # Cython 0.29.30 is required for Python 3.11 and there are # other fixes in the 0.29 series that are needed even for earlier diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index e3dc52c48..8de821340 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -4,7 +4,7 @@ import warnings import itertools import operator import platform -from numpy.compat import _pep440 +from numpy._utils import _pep440 import pytest from hypothesis import given, settings from hypothesis.strategies import sampled_from 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 diff --git a/numpy/random/tests/test_extending.py b/numpy/random/tests/test_extending.py index d5898d25b..aca61bf80 100644 --- a/numpy/random/tests/test_extending.py +++ b/numpy/random/tests/test_extending.py @@ -32,7 +32,7 @@ try: except ImportError: cython = None else: - from numpy.compat import _pep440 + from numpy._utils import _pep440 # Cython 0.29.30 is required for Python 3.11 and there are # other fixes in the 0.29 series that are needed even for earlier # Python versions. diff --git a/numpy/setup.py b/numpy/setup.py index 28c28d1ac..b6f879bcc 100644 --- a/numpy/setup.py +++ b/numpy/setup.py @@ -20,6 +20,7 @@ def configuration(parent_package='',top_path=None): config.add_subpackage('testing') config.add_subpackage('typing') config.add_subpackage('_typing') + config.add_subpackage('_utils') config.add_data_dir('doc') config.add_data_files('py.typed') config.add_data_files('*.pyi') |