question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Instantiating objects and calling methods and functions CRASHES

See original GitHub issue

🐛 Bug

https://hydra.cc/docs/next/patterns/objects#instantiating-objects-and-calling-methods-and-functions Following the 1.0rc instructions for object instantiation results in “ValueError: Input config does not have a cls field”, followed by “UnboundLocalError: local variable ‘cls’ referenced before assignment”.

To reproduce

** Minimal Code/Config snippet to reproduce ** pip install hydra-core --upgrade --pre

Basically copy pasted from the docs. All you have to do is make these files in the same dir, then run python main.py.

# config.yaml
myobject:
  target: models.Foo
  params:
    x: 10
    y: 20
    
myclassmethod:
  target: models.Foo.class_method
  params:
    z: 5

mystaticmethod:
  target: models.Foo.static_method
  params:
    z: 15

myfunction:
  target: models.bar
  params:
    z: 15
# models.py
from typing import Any
class Foo:
  def __init__(self, x: int, y: int) -> None:
    self.x = x
    self.y = y

  @classmethod
  def class_method(self, z: int) -> Any:
    return self(z, 10)

  @staticmethod
  def static_method(z: int) -> int:
    return z + 1

def bar(z: int) -> int:
  return z + 2
# main.py
import hydra
from models import Foo

@hydra.main(config_name="config.yaml")
def app(cfg):
  foo1: Foo = hydra.utils.call(cfg.myobject)  # Foo(10, 20)
  print(Foo)
  foo2: Foo = hydra.utils.call(cfg.myclassmethod)  # Foo(5, 10)
  print(Foo)
  ret1: int = hydra.utils.call(cfg.mystaticmethod)  # 16
  print(ret1)
  ret2: int = hydra.utils.call(cfg.myfunction)  # 17
  print(ret2)

if __name__ == "__main__":
    app()

** Stack trace/error message **

Traceback (most recent call last):
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/utils.py", line 29, in call
    cls = _get_cls_name(config)
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/_internal/utils.py", line 489, in _get_cls_name
    raise ValueError("Input config does not have a cls field")
ValueError: Input config does not have a cls field

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 16, in <module>
    app()
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/main.py", line 37, in decorated_main
    strict=strict,
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/_internal/utils.py", line 253, in run_hydra
    lambda: hydra.run(
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/_internal/utils.py", line 185, in run_and_report
    func()
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/_internal/utils.py", line 256, in <lambda>
    overrides=args.overrides,
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/_internal/hydra.py", line 114, in run
    job_subdir_key=None,
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/core/utils.py", line 107, in run_job
    ret.return_value = task_function(task_cfg)
  File "main.py", line 6, in app
    foo1: Foo = hydra.utils.call(cfg.myobject)  # Foo(10, 20)
  File "/home/andrew/.miniconda3/envs/pt/lib/python3.7/site-packages/hydra/utils.py", line 37, in call
    log.error(f"Error instantiating '{cls}' : {e}")
UnboundLocalError: local variable 'cls' referenced before assignment

Expected Behavior

The code in the docs for Hydra’s class instantiation should work.

System information

  • Hydra Version : 1.0rc
  • Python version : 3.7
  • Virtual environment type and version : None
  • Operating system : Manjaro Linux

Additional context

Add any other context about the problem here.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
omrycommented, Jul 9, 2020

Pass it up as in a HydraException.

1reaction
shagunsodhanicommented, Jul 9, 2020

Yup. As misspelled cls will cause the problem. The right thing is to catch the ValueError: Input config does not have a cls field and pass it up. Will fix it with #737

Read more comments on GitHub >

github_iconTop Results From Across the Web

My C program is crashing when I call a method from a class ...
when I run the program it stops working giving error and on removing strcpy(enc,en.EncryptString("It is C's Lounge!! ")); it runs. I want this ......
Read more >
[BUG] node REPL crashes creating ObjectWrap in getter #1000
The node REPL seems to crash if a C++ getter creates an instance of a class that extends ObjectWrap . This works in...
Read more >
JDK-7047697 MethodHandle.invokeExact call for ... - Bug ID
EVALUATION The problem occurs when a compiled method makes a method handle call that then calls into the VM. Possible examples: - Creating...
Read more >
Unreal Engine crashes in packaged build when creating an ...
My current method works just fine, however when I try it in an packaged build it just crashes. Here is my code: for...
Read more >
5 Troubleshoot System Crashes
A crash, or fatal error, causes a process to terminate abnormally. There are various possible reasons for a crash. For example, a crash...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found