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

get_class_from_modulename mishandles ImportErrors unrelated to its scope #11685

Open
whatisgalen opened this issue Dec 11, 2024 · 2 comments
Open

Comments

@whatisgalen
Copy link
Member

whatisgalen commented Dec 11, 2024

this method:

def get_class_from_modulename(modulename, classname, extension_type: ExtensionType):
    mod_path = modulename.replace(".py", "")
    module = None
    import_success = False
    import_error = None
    for directory in get_directories(extension_type):
        try:
            module = importlib.import_module(directory + ".%s" % mod_path)
        except ImportError as e:
            import_error = e
            continue

captures an ImportError that would come up if the module wasn't actually present in the directory. However, it inadvertently also handles for, say, an ImportError that would arise from the module's own logic.

For example, in my_module.py:

from arches.app.models.nonexistent import NonexistentClass

class thing(NonexistentClass):
    ...

this logic on its own would raise an ImportError but the trace gets effectively overridden when the module is imported somewhere else by what the logic assumes is a totally unrelated error of the module not being there in the first place.

@chiatt chiatt added this to pipeline Dec 11, 2024
@whatisgalen
Copy link
Member Author

@jacobtylerwalls idk why but this might be of interest to you

@jacobtylerwalls
Copy link
Member

Good catch @whatisgalen and good spidy sense -- in other projects I've used from importlib.util import find_spec for exactly this kind of thing, just to sniff whether the module exists without importing. Here we could do that to finish the search loop, then with the right module in hand do the import.

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

No branches or pull requests

2 participants