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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instantiation exception also raises an exception sometimes #1911

Closed
1 of 2 tasks
bryant1410 opened this issue Dec 3, 2021 · 2 comments 路 Fixed by #1916
Closed
1 of 2 tasks

Instantiation exception also raises an exception sometimes #1911

bryant1410 opened this issue Dec 3, 2021 · 2 comments 路 Fixed by #1916
Labels
bug Something isn't working
Milestone

Comments

@bryant1410
Copy link
Contributor

馃悰 Bug

Description

When there's an instantiation exception, raising a chained exception sometimes also fails because it doesn't take an argument:

except Exception as e:
raise type(e)(
f"Error instantiating '{_convert_target_to_string(_target_)}' : {e}"
).with_traceback(sys.exc_info()[2])

Checklist

  • I checked on the latest version of Hydra
  • I created a minimal repro (See this for tips).

To reproduce

Try to instantiate an object such that it raises an exception that doesn't take any argument.

Expected Behavior

Maybe it should try to show another type of exception instead? Or chaining (from e).

System information

  • Hydra Version : 1.1
  • Python version : 3.8
  • Virtual environment type and version : conda 4.11
  • Operating system : Ubuntu 18.04

Additional context

Add any other context about the problem here.

@bryant1410 bryant1410 added the bug Something isn't working label Dec 3, 2021
@Jasha10
Copy link
Collaborator

Jasha10 commented Dec 3, 2021

Hi @bryant1410, thanks for reporting this.

Here is a minimal repro:

# module.py
class MyException(Exception):
    def __init__(self):
        """Init method taking no argument"""
        pass

def foo():
    raise MyException()
# run.py
from hydra.utils import instantiate

instantiate({"_target_": "module.foo"})
$ python run.py
Traceback (most recent call last):
  File "/Users/jasha10/hydra.git/hydra/_internal/instantiate/_instantiate2.py", line 62, in _call_target
    return _target_(*args, **kwargs)
  File "/Users/jasha10/hydra_tmp/1911/module.py", line 8, in foo
    raise MyException()
module.MyException

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jasha10/hydra_tmp/1911/run.py", line 4, in <module>
    instantiate({"_target_": "module.foo"})
  File "/Users/jasha10/hydra.git/hydra/_internal/instantiate/_instantiate2.py", line 180, in instantiate
    return instantiate_node(config, *args, recursive=_recursive_, convert=_convert_)
  File "/Users/jasha10/hydra.git/hydra/_internal/instantiate/_instantiate2.py", line 249, in instantiate_node
    return _call_target(_target_, *args, **kwargs)
  File "/Users/jasha10/hydra.git/hydra/_internal/instantiate/_instantiate2.py", line 64, in _call_target
    raise type(e)(
TypeError: __init__() takes 1 positional argument but 2 were given

@Jasha10
Copy link
Collaborator

Jasha10 commented Dec 3, 2021

I like your proposed idea of exception chaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants