|
1 | 1 | """
|
2 |
| -**Note:** almost all functions in the ``numpy.lib`` namespace |
| 2 | +**Note:** almost all functions in the ``numpy.lib.submodule`` namespaces |
3 | 3 | are also present in the main ``numpy`` namespace. Please use the
|
4 | 4 | functions as ``np.<funcname>`` where possible.
|
5 | 5 |
|
|
11 | 11 | useful to have in the main name-space.
|
12 | 12 |
|
13 | 13 | """
|
14 |
| -import math |
15 |
| - |
16 |
| -from numpy.version import version as __version__ |
17 |
| - |
18 |
| -# Public submodules |
19 |
| -# Note: recfunctions and (maybe) format are public too, but not imported |
20 |
| -from . import mixins |
21 |
| -from . import scimath as emath |
22 |
| - |
23 |
| -# Private submodules |
24 |
| -from .type_check import * |
25 |
| -from .index_tricks import * |
26 |
| -from .function_base import * |
27 |
| -from .nanfunctions import * |
28 |
| -from .shape_base import * |
29 |
| -from .stride_tricks import * |
30 |
| -from .twodim_base import * |
31 |
| -from .ufunclike import * |
32 |
| -from .histograms import * |
33 |
| - |
34 |
| -from .polynomial import * |
35 |
| -from .utils import * |
36 |
| -from .arraysetops import * |
37 |
| -from .npyio import * |
| 14 | +# NOTE: This file is the _public_ interface for additional submodules |
| 15 | +# housed in `numpy.lib`. The private import mechanism lives in the |
| 16 | +# `_lib_importer.py` file, which defines `__all__` to include all names to |
| 17 | +# be added to the main namespace! |
| 18 | + |
| 19 | +from . import scimath as emath # import explicitly to rename |
| 20 | + |
38 | 21 | from .arrayterator import Arrayterator
|
39 |
| -from .arraypad import * |
40 |
| -from ._version import * |
41 | 22 | from numpy.core._multiarray_umath import tracemalloc_domain
|
42 |
| - |
43 |
| -__all__ = ['emath', 'math', 'tracemalloc_domain', 'Arrayterator'] |
44 |
| -__all__ += type_check.__all__ |
45 |
| -__all__ += index_tricks.__all__ |
46 |
| -__all__ += function_base.__all__ |
47 |
| -__all__ += shape_base.__all__ |
48 |
| -__all__ += stride_tricks.__all__ |
49 |
| -__all__ += twodim_base.__all__ |
50 |
| -__all__ += ufunclike.__all__ |
51 |
| -__all__ += arraypad.__all__ |
52 |
| -__all__ += polynomial.__all__ |
53 |
| -__all__ += utils.__all__ |
54 |
| -__all__ += arraysetops.__all__ |
55 |
| -__all__ += npyio.__all__ |
56 |
| -__all__ += nanfunctions.__all__ |
57 |
| -__all__ += histograms.__all__ |
| 23 | +from ._version import NumpyVersion |
58 | 24 |
|
59 | 25 | from numpy._pytesttester import PytestTester
|
60 | 26 | test = PytestTester(__name__)
|
61 | 27 | del PytestTester
|
| 28 | + |
| 29 | +from . import _lib_importer as _hidden_lib_namespace |
| 30 | + |
| 31 | + |
| 32 | +# Ensure that __all__ only contains those things that we want it to contain. |
| 33 | +# Because this is an __init__, we would otherwise include all submodules. |
| 34 | +# |
| 35 | +# NOTE: The symbols not included in `__all__` will be hidden, but do not go |
| 36 | +# through `__getattr__` (see `__dir__`). |
| 37 | +__all__ = {'emath', 'Arrayterator', 'tracemalloc_domain', 'NumpyVersion'} |
| 38 | + |
| 39 | +__lib_submodules_ = { |
| 40 | + 'type_check', |
| 41 | + 'emath', |
| 42 | + 'arraysetops', |
| 43 | + 'utils', |
| 44 | + 'format', |
| 45 | + 'npyio', |
| 46 | + 'arrayterator', |
| 47 | + 'mixins', |
| 48 | + 'stride_tricks', |
| 49 | + 'twodim_base', |
| 50 | + 'histograms', |
| 51 | + 'index_tricks', |
| 52 | + 'nanfunctions', |
| 53 | + 'shape_base', |
| 54 | +} |
| 55 | +# Add submodules to all (and finalize all) |
| 56 | +__all__.update(__lib_submodules_) |
| 57 | +__all__ = sorted(__all__) |
| 58 | + |
| 59 | +# Add hidden submodules: |
| 60 | +__lib_submodules_.update({ |
| 61 | + 'arraypad', |
| 62 | + 'function_base', |
| 63 | + 'polynomial', |
| 64 | + 'scimath', |
| 65 | + "ufunclike", |
| 66 | + }) |
| 67 | + |
| 68 | + |
| 69 | +def __getattr__(name): |
| 70 | + if name in __lib_submodules_: |
| 71 | + import importlib |
| 72 | + # Allow lazy import (and avoid requiring explicit import), at the |
| 73 | + # time of writing `mixins` may be the only namespace using this. |
| 74 | + return importlib.import_module("." + name, __name__) |
| 75 | + |
| 76 | + try: |
| 77 | + attribute = getattr(_hidden_lib_namespace, name) |
| 78 | + except AttributeError: |
| 79 | + pass |
| 80 | + else: |
| 81 | + # Adding a warning in this branch would allow us to properly deprecate |
| 82 | + # all hidden attributes. Raise an error here to test NumPy itself. |
| 83 | + return attribute |
| 84 | + |
| 85 | + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") |
| 86 | + |
| 87 | + |
| 88 | +def __dir__(): |
| 89 | + # Overriding __dir__ hides symbols from tab completion for example. |
| 90 | + # Note that it does _not_ hide symbols from `np.lib.__dict__()`, to do |
| 91 | + # that, we would have to delete them from the locals here and hide them |
| 92 | + # into `__getattr__`. But this seems sufficient, e.g. for tab completion. |
| 93 | + return __all__ |
0 commit comments