Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on pkg_resources from setuptools #1536

Merged
merged 22 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def contribute_to_path(self, spec, processed):
# Builtin.
return None

if _is_setuptools_namespace(spec.location):
if util._is_setuptools_namespace(spec.location):
# extend_path is called, search sys.path for module/packages
# of this name see pkgutil.extend_path documentation
path = [
Expand Down Expand Up @@ -253,20 +253,6 @@ def contribute_to_path(self, spec, processed):
)


def _is_setuptools_namespace(location):
try:
with open(os.path.join(location, "__init__.py"), "rb") as stream:
data = stream.read(4096)
except OSError:
return None
else:
extend_path = b"pkgutil" in data and b"extend_path" in data
declare_namespace = (
b"pkg_resources" in data and b"declare_namespace(__name__)" in data
)
return extend_path or declare_namespace


@lru_cache()
def _cached_set_diff(left, right):
result = set(left)
Expand Down
25 changes: 25 additions & 0 deletions astroid/interpreter/_import/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt

import os
import pathlib
from importlib.util import _find_spec_from_path


def _is_setuptools_namespace(location):
try:
with open(os.path.join(location, "__init__.py"), "rb") as stream:
data = stream.read(4096)
except OSError:
return None
else:
extend_path = b"pkgutil" in data and b"extend_path" in data
declare_namespace = (
b"pkg_resources" in data and b"declare_namespace(__name__)" in data
)
return extend_path or declare_namespace


def is_namespace(modname: str) -> bool:
found_spec = None

Expand All @@ -30,4 +46,13 @@ def is_namespace(modname: str) -> bool:
if found_spec is None:
return False

if found_spec.submodule_search_locations and any(
_is_setuptools_namespace(directory)
for directory in pathlib.Path(
found_spec.submodule_search_locations._path[0]
).iterdir()
if directory.is_dir()
):
return True

return found_spec.origin == "namespace"
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions tests/unittest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from astroid import manager, test_utils
from astroid.const import IS_JYTHON
from astroid.exceptions import AstroidBuildingError, AstroidImportError
from astroid.interpreter._import import util
from astroid.nodes import Const

from . import resources
Expand Down Expand Up @@ -107,6 +108,11 @@ def test_ast_from_namespace_pkgutil(self) -> None:
def test_ast_from_namespace_pkg_resources(self) -> None:
self._test_ast_from_old_namespace_package_protocol("pkg_resources")

def test_identify_old_namespace_package_protocol(self) -> None:
self.assertTrue(
util.is_namespace("tests.testdata.python3.data.path_pkg_resources_1")
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
)
DanielNoord marked this conversation as resolved.
Show resolved Hide resolved

def test_implicit_namespace_package(self) -> None:
data_dir = os.path.dirname(resources.find("data/namespace_pep_420"))
contribute = os.path.join(data_dir, "contribute_to_namespace")
Expand Down