8000 Add option to pass `select_innermost_module` during resolution by AlexVndnblcke · Pull Request #114 · roo-oliv/injectable · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add option to pass select_innermost_module during resolution #114

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 10 additions & 2 deletions injectable/container/injection_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ def _link_dependencies(cls, search_path: str):

@classmethod
def load_dependencies_from(
cls, absolute_search_path: str, default_namespace: str, encoding: str = "utf-8"
cls,
absolute_search_path: str,
default_namespace: str,
encoding: str = "utf-8",
select_innermost_module: bool = False,
):
files = cls._collect_python_files(absolute_search_path)
cls.LOADING_DEFAULT_NAMESPACE = default_namespace
Expand All @@ -169,7 +173,11 @@ def load_dependencies_from(
continue
cls.LOADING_FILEPATH = file.path
try:
run_module(module_finder.find_module_name(file.path))
run_module(
module_finder.find_module_name(
file.path, innermost=select_innermost_module
)
)
except AttributeError:
# This is needed for some corner cases involving pytest
# See more at https://github.com/pytest-dev/pytest/issues/9007
Expand Down
9 changes: 8 additions & 1 deletion injectable/container/load_injection_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def load_injection_container(
*,
default_namespace: str = DEFAULT_NAMESPACE,
encoding: str = "utf-8",
select_innermost_module: bool = False,
):
"""
Loads injectables under the search path to a shared injection container under the
Expand All @@ -24,6 +25,10 @@ def load_injection_container(
:const:`injectable.constants.DEFAULT_NAMESPACE`.
:param encoding: (optional) defines which encoding to use when reading project files
to discover and register injectables. Defaults to ``utf-8``.
:param select_innermost_module: (optional) defines whether or not the most detailed
sys.path should be used to resolve the module name.
Enabling this option helps with prefix-paths which could resolve to incorrect modules.
Defaults to ``false``.

Usage::

Expand All @@ -44,4 +49,6 @@ def load_injection_container(
elif not os.path.isabs(search_path):
caller_path = os.path.dirname(get_caller_filepath())
search_path = os.path.abspath(os.path.join(caller_path, search_path))
InjectionContainer.load_dependencies_from(search_path, default_namespace, encoding)
InjectionContainer.load_dependencies_from(
search_path, default_namespace, encoding, select_innermost_module
)
0