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

Conversation

AlexVndnblcke
Copy link

See #113 (comment)

Resolves issue when sys.path has a prefix-path for the module path. (e.g. PROJECT_ROOT and PROJECT_ROOT/.venv/lib/python3.11/site-packages)

The default behaviour is to match the shortes path possible (or the outermost module) but this causes a module_name as follows: .venv.lib.python3.11.site-packages.module_name while it most likely should be module_name as this is directly a subdir from the site-packages.

The default behaviour is set to use the outermost to ensure no breaking change with current version.

See roo-oliv#113 (comment)

Resolves issue when `sys.path` has a prefix-path for the module path.
(e.g. `PROJECT_ROOT` and `PROJECT_ROOT/.venv/lib/python3.11/site-packages`)

The default behaviour is to match the shortes path possible (or the outermost module) but this causes a module_name as follows: `.venv.lib.python3.11.site-packages.module_name`  while it most likely should be `module_name` as this is directly a subdir from the `site-packages`.

The default behaviour is set to use the outermost to ensure no breaking change with current version.
Copy link

@roo-oliv
Copy link
Owner

Hi, @AlexVndnblcke!

Thank you very much for taking the time to contribute to the project! ❤️

At first, it seems like a reasonable feature to add, I'll take the time to add tests to it and perform some validations. Also, I'm trying to think how to make it easier for a new dev to understand when to use this feature, ideally before getting into an error like you did.

@AlexVndnblcke
Copy link
Author

Thanks for the follow-up @roo-oliv.

After submitting this patch I retraced the problem a little deeper, into pycollect.

In the module_finder a module is selected if it is in the PYTHONPATH, however, if we would add additional validation to ensure that path is a valid module, this patch wouldn't perhaps be necessary.

Detecting a valid module is however less trivial than this patch, as there are different ways a module can be defined. However, since it is based on the PYTHONPATH (which only contains directories), it can be reduced to detecting a __init__.py file in the given directory to validate it as a module. Correct me if I'm wrong.

Alternatively, the python approach would be to attempt an import of that module and consider it validated if it succeeds.

For reference, the validation could be added here:
https://github.com/roo-oliv/pycollect/blob/c344db70d6864c8edd9f3e8a043b430de997eead/src/pycollect/module_finder.py#L37-L41

As for this patch, perhaps a rename could offer some solace. But keeping the parameter terse is a hard one here.

Let me know what you think, I'm open to provide a patch for pycollect if you think that approach would be better.

@kakaday22
Copy link

Hey all, any update on this PR?

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.

3 participants
0