test_public_api.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. import functools
  2. import importlib
  3. import inspect
  4. import pkgutil
  5. import subprocess
  6. import sys
  7. import sysconfig
  8. import types
  9. import warnings
  10. import pytest
  11. import numpy
  12. import numpy as np
  13. from numpy.testing import IS_WASM
  14. try:
  15. import ctypes
  16. except ImportError:
  17. ctypes = None
  18. def check_dir(module, module_name=None):
  19. """Returns a mapping of all objects with the wrong __module__ attribute."""
  20. if module_name is None:
  21. module_name = module.__name__
  22. results = {}
  23. for name in dir(module):
  24. if name == "core":
  25. continue
  26. item = getattr(module, name)
  27. if (hasattr(item, '__module__') and hasattr(item, '__name__')
  28. and item.__module__ != module_name):
  29. results[name] = item.__module__ + '.' + item.__name__
  30. return results
  31. def test_numpy_namespace():
  32. # We override dir to not show these members
  33. allowlist = {
  34. 'recarray': 'numpy.rec.recarray',
  35. }
  36. bad_results = check_dir(np)
  37. # pytest gives better error messages with the builtin assert than with
  38. # assert_equal
  39. assert bad_results == allowlist
  40. @pytest.mark.skipif(IS_WASM, reason="can't start subprocess")
  41. @pytest.mark.parametrize('name', ['testing'])
  42. def test_import_lazy_import(name):
  43. """Make sure we can actually use the modules we lazy load.
  44. While not exported as part of the public API, it was accessible. With the
  45. use of __getattr__ and __dir__, this isn't always true It can happen that
  46. an infinite recursion may happen.
  47. This is the only way I found that would force the failure to appear on the
  48. badly implemented code.
  49. We also test for the presence of the lazily imported modules in dir
  50. """
  51. exe = (sys.executable, '-c', "import numpy; numpy." + name)
  52. result = subprocess.check_output(exe)
  53. assert not result
  54. # Make sure they are still in the __dir__
  55. assert name in dir(np)
  56. def test_dir_testing():
  57. """Assert that output of dir has only one "testing/tester"
  58. attribute without duplicate"""
  59. assert len(dir(np)) == len(set(dir(np)))
  60. def test_numpy_linalg():
  61. bad_results = check_dir(np.linalg)
  62. assert bad_results == {}
  63. def test_numpy_fft():
  64. bad_results = check_dir(np.fft)
  65. assert bad_results == {}
  66. @pytest.mark.skipif(ctypes is None,
  67. reason="ctypes not available in this python")
  68. def test_NPY_NO_EXPORT():
  69. cdll = ctypes.CDLL(np._core._multiarray_tests.__file__)
  70. # Make sure an arbitrary NPY_NO_EXPORT function is actually hidden
  71. f = getattr(cdll, 'test_not_exported', None)
  72. assert f is None, ("'test_not_exported' is mistakenly exported, "
  73. "NPY_NO_EXPORT does not work")
  74. # Historically NumPy has not used leading underscores for private submodules
  75. # much. This has resulted in lots of things that look like public modules
  76. # (i.e. things that can be imported as `import numpy.somesubmodule.somefile`),
  77. # but were never intended to be public. The PUBLIC_MODULES list contains
  78. # modules that are either public because they were meant to be, or because they
  79. # contain public functions/objects that aren't present in any other namespace
  80. # for whatever reason and therefore should be treated as public.
  81. #
  82. # The PRIVATE_BUT_PRESENT_MODULES list contains modules that look public (lack
  83. # of underscores) but should not be used. For many of those modules the
  84. # current status is fine. For others it may make sense to work on making them
  85. # private, to clean up our public API and avoid confusion.
  86. PUBLIC_MODULES = ['numpy.' + s for s in [
  87. "ctypeslib",
  88. "dtypes",
  89. "exceptions",
  90. "f2py",
  91. "fft",
  92. "lib",
  93. "lib.array_utils",
  94. "lib.format",
  95. "lib.introspect",
  96. "lib.mixins",
  97. "lib.npyio",
  98. "lib.recfunctions", # note: still needs cleaning, was forgotten for 2.0
  99. "lib.scimath",
  100. "lib.stride_tricks",
  101. "linalg",
  102. "ma",
  103. "ma.extras",
  104. "ma.mrecords",
  105. "polynomial",
  106. "polynomial.chebyshev",
  107. "polynomial.hermite",
  108. "polynomial.hermite_e",
  109. "polynomial.laguerre",
  110. "polynomial.legendre",
  111. "polynomial.polynomial",
  112. "random",
  113. "strings",
  114. "testing",
  115. "testing.overrides",
  116. "typing",
  117. "typing.mypy_plugin",
  118. "version",
  119. ]]
  120. if sys.version_info < (3, 12):
  121. PUBLIC_MODULES += [
  122. 'numpy.' + s for s in [
  123. "distutils",
  124. "distutils.cpuinfo",
  125. "distutils.exec_command",
  126. "distutils.misc_util",
  127. "distutils.log",
  128. "distutils.system_info",
  129. ]
  130. ]
  131. PUBLIC_ALIASED_MODULES = [
  132. "numpy.char",
  133. "numpy.emath",
  134. "numpy.rec",
  135. ]
  136. PRIVATE_BUT_PRESENT_MODULES = ['numpy.' + s for s in [
  137. "conftest",
  138. "core",
  139. "core.multiarray",
  140. "core.numeric",
  141. "core.umath",
  142. "core.arrayprint",
  143. "core.defchararray",
  144. "core.einsumfunc",
  145. "core.fromnumeric",
  146. "core.function_base",
  147. "core.getlimits",
  148. "core.numerictypes",
  149. "core.overrides",
  150. "core.records",
  151. "core.shape_base",
  152. "f2py.auxfuncs",
  153. "f2py.capi_maps",
  154. "f2py.cb_rules",
  155. "f2py.cfuncs",
  156. "f2py.common_rules",
  157. "f2py.crackfortran",
  158. "f2py.diagnose",
  159. "f2py.f2py2e",
  160. "f2py.f90mod_rules",
  161. "f2py.func2subr",
  162. "f2py.rules",
  163. "f2py.symbolic",
  164. "f2py.use_rules",
  165. "lib.user_array", # note: not in np.lib, but probably should just be deleted
  166. "linalg.lapack_lite",
  167. "ma.core",
  168. "ma.testutils",
  169. "matlib",
  170. "matrixlib",
  171. "matrixlib.defmatrix",
  172. "polynomial.polyutils",
  173. "random.mtrand",
  174. "random.bit_generator",
  175. "testing.print_coercion_tables",
  176. ]]
  177. if sys.version_info < (3, 12):
  178. PRIVATE_BUT_PRESENT_MODULES += [
  179. 'numpy.' + s for s in [
  180. "distutils.armccompiler",
  181. "distutils.fujitsuccompiler",
  182. "distutils.ccompiler",
  183. 'distutils.ccompiler_opt',
  184. "distutils.command",
  185. "distutils.command.autodist",
  186. "distutils.command.bdist_rpm",
  187. "distutils.command.build",
  188. "distutils.command.build_clib",
  189. "distutils.command.build_ext",
  190. "distutils.command.build_py",
  191. "distutils.command.build_scripts",
  192. "distutils.command.build_src",
  193. "distutils.command.config",
  194. "distutils.command.config_compiler",
  195. "distutils.command.develop",
  196. "distutils.command.egg_info",
  197. "distutils.command.install",
  198. "distutils.command.install_clib",
  199. "distutils.command.install_data",
  200. "distutils.command.install_headers",
  201. "distutils.command.sdist",
  202. "distutils.conv_template",
  203. "distutils.core",
  204. "distutils.extension",
  205. "distutils.fcompiler",
  206. "distutils.fcompiler.absoft",
  207. "distutils.fcompiler.arm",
  208. "distutils.fcompiler.compaq",
  209. "distutils.fcompiler.environment",
  210. "distutils.fcompiler.g95",
  211. "distutils.fcompiler.gnu",
  212. "distutils.fcompiler.hpux",
  213. "distutils.fcompiler.ibm",
  214. "distutils.fcompiler.intel",
  215. "distutils.fcompiler.lahey",
  216. "distutils.fcompiler.mips",
  217. "distutils.fcompiler.nag",
  218. "distutils.fcompiler.none",
  219. "distutils.fcompiler.pathf95",
  220. "distutils.fcompiler.pg",
  221. "distutils.fcompiler.nv",
  222. "distutils.fcompiler.sun",
  223. "distutils.fcompiler.vast",
  224. "distutils.fcompiler.fujitsu",
  225. "distutils.from_template",
  226. "distutils.intelccompiler",
  227. "distutils.lib2def",
  228. "distutils.line_endings",
  229. "distutils.mingw32ccompiler",
  230. "distutils.msvccompiler",
  231. "distutils.npy_pkg_config",
  232. "distutils.numpy_distribution",
  233. "distutils.pathccompiler",
  234. "distutils.unixccompiler",
  235. ]
  236. ]
  237. def is_unexpected(name):
  238. """Check if this needs to be considered."""
  239. return (
  240. '._' not in name and '.tests' not in name and '.setup' not in name
  241. and name not in PUBLIC_MODULES
  242. and name not in PUBLIC_ALIASED_MODULES
  243. and name not in PRIVATE_BUT_PRESENT_MODULES
  244. )
  245. if sys.version_info >= (3, 12):
  246. SKIP_LIST = []
  247. else:
  248. SKIP_LIST = ["numpy.distutils.msvc9compiler"]
  249. def test_all_modules_are_expected():
  250. """
  251. Test that we don't add anything that looks like a new public module by
  252. accident. Check is based on filenames.
  253. """
  254. modnames = []
  255. for _, modname, ispkg in pkgutil.walk_packages(path=np.__path__,
  256. prefix=np.__name__ + '.',
  257. onerror=None):
  258. if is_unexpected(modname) and modname not in SKIP_LIST:
  259. # We have a name that is new. If that's on purpose, add it to
  260. # PUBLIC_MODULES. We don't expect to have to add anything to
  261. # PRIVATE_BUT_PRESENT_MODULES. Use an underscore in the name!
  262. modnames.append(modname)
  263. if modnames:
  264. raise AssertionError(f'Found unexpected modules: {modnames}')
  265. # Stuff that clearly shouldn't be in the API and is detected by the next test
  266. # below
  267. SKIP_LIST_2 = [
  268. 'numpy.lib.math',
  269. 'numpy.matlib.char',
  270. 'numpy.matlib.rec',
  271. 'numpy.matlib.emath',
  272. 'numpy.matlib.exceptions',
  273. 'numpy.matlib.math',
  274. 'numpy.matlib.linalg',
  275. 'numpy.matlib.fft',
  276. 'numpy.matlib.random',
  277. 'numpy.matlib.ctypeslib',
  278. 'numpy.matlib.ma',
  279. ]
  280. if sys.version_info < (3, 12):
  281. SKIP_LIST_2 += [
  282. 'numpy.distutils.log.sys',
  283. 'numpy.distutils.log.logging',
  284. 'numpy.distutils.log.warnings',
  285. ]
  286. def test_all_modules_are_expected_2():
  287. """
  288. Method checking all objects. The pkgutil-based method in
  289. `test_all_modules_are_expected` does not catch imports into a namespace,
  290. only filenames. So this test is more thorough, and checks this like:
  291. import .lib.scimath as emath
  292. To check if something in a module is (effectively) public, one can check if
  293. there's anything in that namespace that's a public function/object but is
  294. not exposed in a higher-level namespace. For example for a `numpy.lib`
  295. submodule::
  296. mod = np.lib.mixins
  297. for obj in mod.__all__:
  298. if obj in np.__all__:
  299. continue
  300. elif obj in np.lib.__all__:
  301. continue
  302. else:
  303. print(obj)
  304. """
  305. def find_unexpected_members(mod_name):
  306. members = []
  307. module = importlib.import_module(mod_name)
  308. if hasattr(module, '__all__'):
  309. objnames = module.__all__
  310. else:
  311. objnames = dir(module)
  312. for objname in objnames:
  313. if not objname.startswith('_'):
  314. fullobjname = mod_name + '.' + objname
  315. if isinstance(getattr(module, objname), types.ModuleType):
  316. if is_unexpected(fullobjname):
  317. if fullobjname not in SKIP_LIST_2:
  318. members.append(fullobjname)
  319. return members
  320. unexpected_members = find_unexpected_members("numpy")
  321. for modname in PUBLIC_MODULES:
  322. unexpected_members.extend(find_unexpected_members(modname))
  323. if unexpected_members:
  324. raise AssertionError("Found unexpected object(s) that look like "
  325. f"modules: {unexpected_members}")
  326. def test_api_importable():
  327. """
  328. Check that all submodules listed higher up in this file can be imported
  329. Note that if a PRIVATE_BUT_PRESENT_MODULES entry goes missing, it may
  330. simply need to be removed from the list (deprecation may or may not be
  331. needed - apply common sense).
  332. """
  333. def check_importable(module_name):
  334. try:
  335. importlib.import_module(module_name)
  336. except (ImportError, AttributeError):
  337. return False
  338. return True
  339. module_names = []
  340. for module_name in PUBLIC_MODULES:
  341. if not check_importable(module_name):
  342. module_names.append(module_name)
  343. if module_names:
  344. raise AssertionError("Modules in the public API that cannot be "
  345. f"imported: {module_names}")
  346. for module_name in PUBLIC_ALIASED_MODULES:
  347. try:
  348. eval(module_name)
  349. except AttributeError:
  350. module_names.append(module_name)
  351. if module_names:
  352. raise AssertionError("Modules in the public API that were not "
  353. f"found: {module_names}")
  354. with warnings.catch_warnings(record=True) as w:
  355. warnings.filterwarnings('always', category=DeprecationWarning)
  356. warnings.filterwarnings('always', category=ImportWarning)
  357. for module_name in PRIVATE_BUT_PRESENT_MODULES:
  358. if not check_importable(module_name):
  359. # Nasty hack to avoid new FreeBSD failures. This
  360. # is only needed for NumPy 2.4.x, so go with it
  361. if not module_name == 'numpy.distutils.msvccompiler':
  362. module_names.append(module_name)
  363. if module_names:
  364. raise AssertionError("Modules that are not really public but looked "
  365. "public and can not be imported: "
  366. f"{module_names}")
  367. @pytest.mark.xfail(
  368. sysconfig.get_config_var("Py_DEBUG") not in (None, 0, "0"),
  369. reason=(
  370. "NumPy possibly built with `USE_DEBUG=True ./tools/travis-test.sh`, "
  371. "which does not expose the `array_api` entry point. "
  372. "See https://github.com/numpy/numpy/pull/19800"
  373. ),
  374. )
  375. def test_array_api_entry_point():
  376. """
  377. Entry point for Array API implementation can be found with importlib and
  378. returns the main numpy namespace.
  379. """
  380. # For a development install that did not go through meson-python,
  381. # the entrypoint will not have been installed. So ensure this test fails
  382. # only if numpy is inside site-packages.
  383. numpy_in_sitepackages = sysconfig.get_path('platlib') in np.__file__
  384. eps = importlib.metadata.entry_points()
  385. xp_eps = eps.select(group="array_api")
  386. if len(xp_eps) == 0:
  387. if numpy_in_sitepackages:
  388. msg = "No entry points for 'array_api' found"
  389. raise AssertionError(msg) from None
  390. return
  391. try:
  392. ep = next(ep for ep in xp_eps if ep.name == "numpy")
  393. except StopIteration:
  394. if numpy_in_sitepackages:
  395. msg = "'numpy' not in array_api entry points"
  396. raise AssertionError(msg) from None
  397. return
  398. if ep.value == 'numpy.array_api':
  399. # Looks like the entrypoint for the current numpy build isn't
  400. # installed, but an older numpy is also installed and hence the
  401. # entrypoint is pointing to the old (no longer existing) location.
  402. # This isn't a problem except for when running tests with `spin` or an
  403. # in-place build.
  404. return
  405. xp = ep.load()
  406. msg = (
  407. f"numpy entry point value '{ep.value}' "
  408. "does not point to our Array API implementation"
  409. )
  410. assert xp is numpy, msg
  411. def test_main_namespace_all_dir_coherence():
  412. """
  413. Checks if `dir(np)` and `np.__all__` are consistent and return
  414. the same content, excluding exceptions and private members.
  415. """
  416. def _remove_private_members(member_set):
  417. return {m for m in member_set if not m.startswith('_')}
  418. def _remove_exceptions(member_set):
  419. return member_set.difference({
  420. "bool" # included only in __dir__
  421. })
  422. all_members = _remove_private_members(np.__all__)
  423. all_members = _remove_exceptions(all_members)
  424. dir_members = _remove_private_members(np.__dir__())
  425. dir_members = _remove_exceptions(dir_members)
  426. assert all_members == dir_members, (
  427. "Members that break symmetry: "
  428. f"{all_members.symmetric_difference(dir_members)}"
  429. )
  430. @pytest.mark.filterwarnings(
  431. r"ignore:numpy.core(\.\w+)? is deprecated:DeprecationWarning"
  432. )
  433. def test_core_shims_coherence():
  434. """
  435. Check that all "semi-public" members of `numpy._core` are also accessible
  436. from `numpy.core` shims.
  437. """
  438. import numpy.core as core
  439. for member_name in dir(np._core):
  440. # Skip private and test members. Also if a module is aliased,
  441. # no need to add it to np.core
  442. if (
  443. member_name.startswith("_")
  444. or member_name in ["tests", "strings"]
  445. or f"numpy.{member_name}" in PUBLIC_ALIASED_MODULES
  446. ):
  447. continue
  448. member = getattr(np._core, member_name)
  449. # np.core is a shim and all submodules of np.core are shims
  450. # but we should be able to import everything in those shims
  451. # that are available in the "real" modules in np._core, with
  452. # the exception of the namespace packages (__spec__.origin is None),
  453. # like numpy._core.include, or numpy._core.lib.pkgconfig.
  454. if (
  455. inspect.ismodule(member)
  456. and member.__spec__ and member.__spec__.origin is not None
  457. ):
  458. submodule = member
  459. submodule_name = member_name
  460. for submodule_member_name in dir(submodule):
  461. # ignore dunder names
  462. if submodule_member_name.startswith("__"):
  463. continue
  464. submodule_member = getattr(submodule, submodule_member_name)
  465. core_submodule = __import__(
  466. f"numpy.core.{submodule_name}",
  467. fromlist=[submodule_member_name]
  468. )
  469. assert submodule_member is getattr(
  470. core_submodule, submodule_member_name
  471. )
  472. else:
  473. assert member is getattr(core, member_name)
  474. def test_functions_single_location():
  475. """
  476. Check that each public function is available from one location only.
  477. Test performs BFS search traversing NumPy's public API. It flags
  478. any function-like object that is accessible from more that one place.
  479. """
  480. from collections.abc import Callable
  481. from typing import Any
  482. from numpy._core._multiarray_umath import (
  483. _ArrayFunctionDispatcher as dispatched_function,
  484. )
  485. visited_modules: set[types.ModuleType] = {np}
  486. visited_functions: set[Callable[..., Any]] = set()
  487. # Functions often have `__name__` overridden, therefore we need
  488. # to keep track of locations where functions have been found.
  489. functions_original_paths: dict[Callable[..., Any], str] = {}
  490. # Here we aggregate functions with more than one location.
  491. # It must be empty for the test to pass.
  492. duplicated_functions: list[tuple] = []
  493. modules_queue = [np]
  494. while len(modules_queue) > 0:
  495. module = modules_queue.pop()
  496. for member_name in dir(module):
  497. member = getattr(module, member_name)
  498. # first check if we got a module
  499. if (
  500. inspect.ismodule(member) and # it's a module
  501. "numpy" in member.__name__ and # inside NumPy
  502. not member_name.startswith("_") and # not private
  503. "numpy._core" not in member.__name__ and # outside _core
  504. # not a legacy or testing module
  505. member_name not in ["f2py", "ma", "testing", "tests"] and
  506. member not in visited_modules # not visited yet
  507. ):
  508. modules_queue.append(member)
  509. visited_modules.add(member)
  510. # else check if we got a function-like object
  511. elif (
  512. inspect.isfunction(member) or
  513. isinstance(member, (dispatched_function, np.ufunc))
  514. ):
  515. if member in visited_functions:
  516. # skip main namespace functions with aliases
  517. if (
  518. member.__name__ in [
  519. "absolute", # np.abs
  520. "arccos", # np.acos
  521. "arccosh", # np.acosh
  522. "arcsin", # np.asin
  523. "arcsinh", # np.asinh
  524. "arctan", # np.atan
  525. "arctan2", # np.atan2
  526. "arctanh", # np.atanh
  527. "left_shift", # np.bitwise_left_shift
  528. "right_shift", # np.bitwise_right_shift
  529. "conjugate", # np.conj
  530. "invert", # np.bitwise_not & np.bitwise_invert
  531. "remainder", # np.mod
  532. "divide", # np.true_divide
  533. "concatenate", # np.concat
  534. "power", # np.pow
  535. "transpose", # np.permute_dims
  536. ] and
  537. module.__name__ == "numpy"
  538. ):
  539. continue
  540. # skip trimcoef from numpy.polynomial as it is
  541. # duplicated by design.
  542. if (
  543. member.__name__ == "trimcoef" and
  544. module.__name__.startswith("numpy.polynomial")
  545. ):
  546. continue
  547. # skip ufuncs that are exported in np.strings as well
  548. if member.__name__ in (
  549. "add",
  550. "equal",
  551. "not_equal",
  552. "greater",
  553. "greater_equal",
  554. "less",
  555. "less_equal",
  556. ) and module.__name__ == "numpy.strings":
  557. continue
  558. # numpy.char reexports all numpy.strings functions for
  559. # backwards-compatibility
  560. if module.__name__ == "numpy.char":
  561. continue
  562. # function is present in more than one location!
  563. duplicated_functions.append(
  564. (member.__name__,
  565. module.__name__,
  566. functions_original_paths[member])
  567. )
  568. else:
  569. visited_functions.add(member)
  570. functions_original_paths[member] = module.__name__
  571. del visited_functions, visited_modules, functions_original_paths
  572. assert len(duplicated_functions) == 0, duplicated_functions
  573. def test___module___attribute():
  574. modules_queue = [np]
  575. visited_modules = {np}
  576. visited_functions = set()
  577. incorrect_entries = []
  578. while len(modules_queue) > 0:
  579. module = modules_queue.pop()
  580. for member_name in dir(module):
  581. member = getattr(module, member_name)
  582. # first check if we got a module
  583. if (
  584. inspect.ismodule(member) and # it's a module
  585. "numpy" in member.__name__ and # inside NumPy
  586. not member_name.startswith("_") and # not private
  587. "numpy._core" not in member.__name__ and # outside _core
  588. # not in a skip module list
  589. member_name not in [
  590. "char", "core", "f2py", "ma", "lapack_lite", "mrecords",
  591. "testing", "tests", "polynomial", "typing", "mtrand",
  592. "bit_generator",
  593. ] and
  594. member not in visited_modules # not visited yet
  595. ):
  596. modules_queue.append(member)
  597. visited_modules.add(member)
  598. elif (
  599. not inspect.ismodule(member) and
  600. hasattr(member, "__name__") and
  601. not member.__name__.startswith("_") and
  602. member.__module__ != module.__name__ and
  603. member not in visited_functions
  604. ):
  605. # skip ufuncs that are exported in np.strings as well
  606. if member.__name__ in (
  607. "add", "equal", "not_equal", "greater", "greater_equal",
  608. "less", "less_equal",
  609. ) and module.__name__ == "numpy.strings":
  610. continue
  611. # recarray and record are exported in np and np.rec
  612. if (
  613. (member.__name__ == "recarray" and module.__name__ == "numpy") or
  614. (member.__name__ == "record" and module.__name__ == "numpy.rec")
  615. ):
  616. continue
  617. # ctypeslib exports ctypes c_long/c_longlong
  618. if (
  619. member.__name__ in ("c_long", "c_longlong") and
  620. module.__name__ == "numpy.ctypeslib"
  621. ):
  622. continue
  623. # skip cdef classes
  624. if member.__name__ in (
  625. "BitGenerator", "Generator", "MT19937", "PCG64", "PCG64DXSM",
  626. "Philox", "RandomState", "SFC64", "SeedSequence",
  627. ):
  628. continue
  629. incorrect_entries.append(
  630. {
  631. "Func": member.__name__,
  632. "actual": member.__module__,
  633. "expected": module.__name__,
  634. }
  635. )
  636. visited_functions.add(member)
  637. if incorrect_entries:
  638. assert len(incorrect_entries) == 0, incorrect_entries
  639. def _check_correct_qualname_and_module(obj) -> bool:
  640. qualname = obj.__qualname__
  641. name = obj.__name__
  642. module_name = obj.__module__
  643. assert name == qualname.split(".")[-1]
  644. module = sys.modules[module_name]
  645. actual_obj = functools.reduce(getattr, qualname.split("."), module)
  646. return (
  647. actual_obj is obj or
  648. # `obj` may be a bound method/property of `actual_obj`:
  649. (
  650. hasattr(actual_obj, "__get__") and hasattr(obj, "__self__") and
  651. actual_obj.__module__ == obj.__module__ and
  652. actual_obj.__qualname__ == qualname
  653. )
  654. )
  655. def test___qualname___and___module___attribute():
  656. # NumPy messes with module and name/qualname attributes, but any object
  657. # should be discoverable based on its module and qualname, so test that.
  658. # We do this for anything with a name (ensuring qualname is also set).
  659. modules_queue = [np]
  660. visited_modules = {np}
  661. visited_functions = set()
  662. incorrect_entries = []
  663. while len(modules_queue) > 0:
  664. module = modules_queue.pop()
  665. for member_name in dir(module):
  666. member = getattr(module, member_name)
  667. # first check if we got a module
  668. if (
  669. inspect.ismodule(member) and # it's a module
  670. "numpy" in member.__name__ and # inside NumPy
  671. not member_name.startswith("_") and # not private
  672. member_name not in {"tests", "typing"} and # type names don't match
  673. "numpy._core" not in member.__name__ and # outside _core
  674. member not in visited_modules # not visited yet
  675. ):
  676. modules_queue.append(member)
  677. visited_modules.add(member)
  678. elif (
  679. not inspect.ismodule(member) and
  680. hasattr(member, "__name__") and
  681. not member.__name__.startswith("_") and
  682. not member_name.startswith("_") and
  683. not _check_correct_qualname_and_module(member) and
  684. member not in visited_functions
  685. ):
  686. incorrect_entries.append(
  687. {
  688. "found_at": f"{module.__name__}:{member_name}",
  689. "advertises": f"{member.__module__}:{member.__qualname__}",
  690. }
  691. )
  692. visited_functions.add(member)
  693. if incorrect_entries:
  694. assert len(incorrect_entries) == 0, incorrect_entries