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

allow string imports outside of packages #736

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

carloshorn
Copy link

Problem description

Currently, string imports are not possible outside of packages.

Minimal reproducible example

The following code snippet executed from a script or Jupyter notebook cell raises an AttributeError: 'NoneType' object has no attribute '__package__'

from dependency_injector import providers
deque_factory = providers.Factory('collections.deque')

The trace back reveals that the exception is thrown in src/dependency_injector/providers.pyx in dependency_injector.providers._resolve_calling_package_name().

Proposal for solution

In case the module in dependency_injector.providers._resolve_calling_package_name is None, the package should also be None. The subsequent call to importlib.import_module will handle the situation properly.

allow string imports outside of packages
@nightblure
Copy link

IMO this will not be a popular use case, using strings instead of objects is a rather exotic and rare practice..

@@ -5034,7 +5034,7 @@ def _resolve_calling_module():

def _resolve_calling_package_name():
module = _resolve_calling_module()
return module.__package__
return getattr(module, "__package__", None)
Copy link

@nightblure nightblure Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like it would be a good idea to write tests or add test cases for clients using this _resolve_calling_package_name function

nightblure

This comment was marked as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants