-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update submodule_path after finding an editable install (#2033)
- Loading branch information
1 parent
27352c2
commit fe57762
Showing
5 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
tests/testdata/python3/data/import_setuptools_pep660/__editable___example_0_1_0_finder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""This file contains Finders automatically generated by setuptools for a package installed | ||
in editable mode via custom import hooks. It's generated here: | ||
https://github.com/pypa/setuptools/blob/c34b82735c1a9c8707bea00705ae2f621bf4c24d/setuptools/command/editable_wheel.py#L732-L801 | ||
""" | ||
import sys | ||
from importlib.machinery import ModuleSpec | ||
from importlib.machinery import all_suffixes as module_suffixes | ||
from importlib.util import spec_from_file_location | ||
from itertools import chain | ||
from pathlib import Path | ||
|
||
MAPPING = {"example": Path(__file__).parent.resolve() / "example"} | ||
NAMESPACES = {} | ||
PATH_PLACEHOLDER = "__editable__.example-0.1.0.finder" + ".__path_hook__" | ||
|
||
|
||
class _EditableFinder: # MetaPathFinder | ||
@classmethod | ||
def find_spec(cls, fullname, path=None, target=None): | ||
for pkg, pkg_path in reversed(list(MAPPING.items())): | ||
if fullname == pkg or fullname.startswith(f"{pkg}."): | ||
rest = fullname.replace(pkg, "", 1).strip(".").split(".") | ||
return cls._find_spec(fullname, Path(pkg_path, *rest)) | ||
|
||
return None | ||
|
||
@classmethod | ||
def _find_spec(cls, fullname, candidate_path): | ||
init = candidate_path / "__init__.py" | ||
candidates = (candidate_path.with_suffix(x) for x in module_suffixes()) | ||
for candidate in chain([init], candidates): | ||
if candidate.exists(): | ||
return spec_from_file_location(fullname, candidate) | ||
|
||
|
||
class _EditableNamespaceFinder: # PathEntryFinder | ||
@classmethod | ||
def _path_hook(cls, path): | ||
if path == PATH_PLACEHOLDER: | ||
return cls | ||
raise ImportError | ||
|
||
@classmethod | ||
def _paths(cls, fullname): | ||
# Ensure __path__ is not empty for the spec to be considered a namespace. | ||
return NAMESPACES[fullname] or MAPPING.get(fullname) or [PATH_PLACEHOLDER] | ||
|
||
@classmethod | ||
def find_spec(cls, fullname, target=None): | ||
if fullname in NAMESPACES: | ||
spec = ModuleSpec(fullname, None, is_package=True) | ||
spec.submodule_search_locations = cls._paths(fullname) | ||
return spec | ||
return None | ||
|
||
@classmethod | ||
def find_module(cls, fullname): | ||
return None | ||
|
||
|
||
def install(): | ||
if not any(finder == _EditableFinder for finder in sys.meta_path): | ||
sys.meta_path.append(_EditableFinder) | ||
|
||
if not NAMESPACES: | ||
return | ||
|
||
if not any(hook == _EditableNamespaceFinder._path_hook for hook in sys.path_hooks): | ||
# PathEntryFinder is needed to create NamespaceSpec without private APIS | ||
sys.path_hooks.append(_EditableNamespaceFinder._path_hook) | ||
if PATH_PLACEHOLDER not in sys.path: | ||
sys.path.append(PATH_PLACEHOLDER) # Used just to trigger the path hook |
1 change: 1 addition & 0 deletions
1
tests/testdata/python3/data/import_setuptools_pep660/example/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from subpackage import hello |
1 change: 1 addition & 0 deletions
1
tests/testdata/python3/data/import_setuptools_pep660/example/subpackage/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello = 1 |