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

smiles extra and decorator for rdkit functions #200

Closed
wants to merge 2 commits into from

Conversation

GeorgWa
Copy link
Collaborator

@GeorgWa GeorgWa commented Jul 17, 2024

This allows to install rdkit only when the smiles extra is defined.
All RDKit dependent functions will give a meaningfull error message if called:

RDKit is required for the function '{func.__name__}' 
Please install it with 'pip install alphabase[smiles]'

return func(*args, **kwargs)
return wrapper

@requires_rdkit
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not code that is actually used, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, just as a reference for @boopthesnoot

@functools.wraps(func)
def wrapper(*args, **kwargs):
try:
from rdkit import Chem
Copy link
Contributor

Choose a reason for hiding this comment

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

is there another option to find out that the package is there, without actually importing it? this would avoid the side effect of this decorator

Copy link
Contributor

Choose a reason for hiding this comment

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

also, this will only work if the imports are done within the functions not for top-level imports, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You could use importlib.util.find_spec("rdkit") is not None which still invokes the import machinery to some degree.

On the second point, I think there is no way around this.
If you have a module level import you will always fail during initialization.
Maybe it's possible with some heavy import magic but I'm not aware.

Copy link
Contributor

Choose a reason for hiding this comment

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

okay, but can we then not just catch the import error in the smiles module, and change it to the user-friendly error message?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I prefer to catch ImportError once this module is imported

@@ -0,0 +1 @@
rdkit>=2024.3.3
Copy link
Contributor

Choose a reason for hiding this comment

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

fix version? :-)

@jalew188
Copy link
Collaborator

What is the connection between this PR and #199

@jalew188
Copy link
Collaborator

If smiles is imported in many other modules, i think it is better to place rdkit in the main requirement

@GeorgWa GeorgWa closed this Jul 17, 2024
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