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

Opaque error message when attempting to instantiate object from a module that has errors #1863

Closed
Jasha10 opened this issue Oct 22, 2021 · 0 comments · Fixed by #1864
Closed
Assignees
Labels
enhancement Enhanvement request In progress Work in progress

Comments

@Jasha10
Copy link
Collaborator

Jasha10 commented Oct 22, 2021

The hydra.utils.instantiate function looks up it's target by using Python's importlib machinery.

# my_app.py
import hydra
hydra.utils.instantiate({"_target_": "my_module.Foo"})
# my_module.py
class Foo:
    print("Hi")
$ python3 my_app.py
Hi

Suppose the user makes a syntax error in my_module.py, forgetting a colon in the class statement:

# my_module.py
class Foo
    print("Hi")
python3 my_app.py
Traceback (most recent call last):
...
ValueError: Empty module name

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
...
  File "/home/jasha10/hydra.git/hydra/_internal/utils.py", line 573, in _locate
    raise ImportError(f"Error loading module '{path}'") from e
ImportError: Error loading module 'my_module.Foo'

The first part of this traceback, which says "ValueError: Empty module name", is not very helpful.
The traceback should instead look like this:

$ python3 my_app.py
Traceback (most recent call last):
...
    class Foo
             ^
SyntaxError: invalid syntax

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
...
  File "/home/jasha10/hydra.git/hydra/_internal/utils.py", line 573, in _locate
    raise ImportError(f"Error loading module '{path}'") from e
ImportError: Error loading module 'my_module.Foo'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhanvement request In progress Work in progress
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant