util.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html
  2. # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
  3. # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. import pathlib
  6. import sys
  7. from functools import lru_cache
  8. from importlib._bootstrap_external import _NamespacePath
  9. from importlib.util import _find_spec_from_path # type: ignore[attr-defined]
  10. from astroid.const import IS_PYPY
  11. @lru_cache(maxsize=4096)
  12. def is_namespace(modname: str) -> bool:
  13. from astroid.modutils import ( # pylint: disable=import-outside-toplevel
  14. EXT_LIB_DIRS,
  15. STD_LIB_DIRS,
  16. )
  17. STD_AND_EXT_LIB_DIRS = STD_LIB_DIRS.union(EXT_LIB_DIRS)
  18. if modname in sys.builtin_module_names:
  19. return False
  20. found_spec = None
  21. # find_spec() attempts to import parent packages when given dotted paths.
  22. # That's unacceptable here, so we fallback to _find_spec_from_path(), which does
  23. # not, but requires instead that each single parent ('astroid', 'nodes', etc.)
  24. # be specced from left to right.
  25. processed_components = []
  26. last_submodule_search_locations: _NamespacePath | None = None
  27. for component in modname.split("."):
  28. processed_components.append(component)
  29. working_modname = ".".join(processed_components)
  30. try:
  31. # Both the modname and the path are built iteratively, with the
  32. # path (e.g. ['a', 'a/b', 'a/b/c']) lagging the modname by one
  33. found_spec = _find_spec_from_path(
  34. working_modname, path=last_submodule_search_locations
  35. )
  36. except AttributeError:
  37. return False
  38. except ValueError:
  39. if modname == "__main__":
  40. return False
  41. try:
  42. # .pth files will be on sys.modules
  43. # __spec__ is set inconsistently on PyPy so we can't really on the heuristic here
  44. # See: https://foss.heptapod.net/pypy/pypy/-/issues/3736
  45. # Check first fragment of modname, e.g. "astroid", not "astroid.interpreter"
  46. # because of cffi's behavior
  47. # See: https://github.com/PyCQA/astroid/issues/1776
  48. mod = sys.modules[processed_components[0]]
  49. return (
  50. mod.__spec__ is None
  51. and getattr(mod, "__file__", None) is None
  52. and hasattr(mod, "__path__")
  53. and not IS_PYPY
  54. )
  55. except KeyError:
  56. return False
  57. except AttributeError:
  58. # Workaround for "py" module
  59. # https://github.com/pytest-dev/apipkg/issues/13
  60. return False
  61. except KeyError:
  62. # Intermediate steps might raise KeyErrors
  63. # https://github.com/python/cpython/issues/93334
  64. # TODO: update if fixed in importlib
  65. # For tree a > b > c.py
  66. # >>> from importlib.machinery import PathFinder
  67. # >>> PathFinder.find_spec('a.b', ['a'])
  68. # KeyError: 'a'
  69. # Repair last_submodule_search_locations
  70. if last_submodule_search_locations:
  71. # TODO: py38: remove except
  72. try:
  73. # pylint: disable=unsubscriptable-object
  74. last_item = last_submodule_search_locations[-1]
  75. except TypeError:
  76. last_item = last_submodule_search_locations._recalculate()[-1]
  77. # e.g. for failure example above, add 'a/b' and keep going
  78. # so that find_spec('a.b.c', path=['a', 'a/b']) succeeds
  79. assumed_location = pathlib.Path(last_item) / component
  80. last_submodule_search_locations.append(str(assumed_location))
  81. continue
  82. # Update last_submodule_search_locations for next iteration
  83. if found_spec and found_spec.submodule_search_locations:
  84. # But immediately return False if we can detect we are in stdlib
  85. # or external lib (e.g site-packages)
  86. if any(
  87. any(location.startswith(lib_dir) for lib_dir in STD_AND_EXT_LIB_DIRS)
  88. for location in found_spec.submodule_search_locations
  89. ):
  90. return False
  91. last_submodule_search_locations = found_spec.submodule_search_locations
  92. return (
  93. found_spec is not None
  94. and found_spec.submodule_search_locations is not None
  95. and found_spec.origin is None
  96. )