Skip to content

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

Closed

Description

@Jasha10

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions