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

[Bug] Cannot use target kwarg when instantiating with hydra.utils.instantiate() #1669

Closed
2 tasks done
fabiocarrara opened this issue Jun 11, 2021 · 2 comments 路 Fixed by #1673
Closed
2 tasks done

[Bug] Cannot use target kwarg when instantiating with hydra.utils.instantiate() #1669

fabiocarrara opened this issue Jun 11, 2021 · 2 comments 路 Fixed by #1673
Assignees
Labels
bug Something isn't working In progress Work in progress
Milestone

Comments

@fabiocarrara
Copy link

馃悰 Bug

Description

hydra.utils.instantiate() seems to fail when trying to instantiate a class with a keyword argument called target.
It seems like the two keys _target_ and target are clashing, and this raises a
TypeError: _call_target() got multiple values for argument 'target'.

Checklist

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

To reproduce

Minimal Code/Config snippet to reproduce
Available as a Colab Gists:
https://colab.research.google.com/gist/fabiocarrara/6bf5abaecf3f84c1e83239f8af3b8219/hydra_instantiate_bug.ipynb

Stack trace/error message

TypeError                                 Traceback (most recent call last)
<ipython-input-4-616e52dc8a9b> in <module>()
----> 1 foo = hydra.utils.instantiate(cfg.bar)
      2 foo.target

/usr/local/lib/python3.7/dist-packages/hydra/_internal/instantiate/_instantiate2.py in instantiate(config, *args, **kwargs)
    178         _convert_ = config.pop(_Keys.CONVERT, ConvertMode.NONE)
    179 
--> 180         return instantiate_node(config, *args, recursive=_recursive_, convert=_convert_)
    181     else:
    182         raise InstantiationException(

/usr/local/lib/python3.7/dist-packages/hydra/_internal/instantiate/_instantiate2.py in instantiate_node(node, convert, recursive, *args)
    247                         )
    248                     kwargs[key] = _convert_node(value, convert)
--> 249             return _call_target(target, *args, **kwargs)
    250         else:
    251             # If ALL or PARTIAL non structured, instantiate in dict and resolve interpolations eagerly.

TypeError: _call_target() got multiple values for argument 'target'

Expected Behavior

target should be a permitted key name for callable/constructor's kwargs.

System information

  • Hydra Version : 1.1
  • Python version : 3.7.10 (Colab)
  • Virtual environment type and version : none
  • Operating system : Colab
@fabiocarrara fabiocarrara added the bug Something isn't working label Jun 11, 2021
@omry omry modified the milestone: Hydra 1.1.0 Jun 11, 2021
@omry
Copy link
Collaborator

omry commented Jun 11, 2021

Thanks for reporting, we will take a look.

@omry omry added this to the Hydra 1.1.1 milestone Jun 11, 2021
@Jasha10
Copy link
Collaborator

Jasha10 commented Jun 11, 2021

Here is a minimal repro:

# repro.py
from hydra.utils import instantiate
from omegaconf import OmegaConf
from pytest import raises

class Foo:
    def __init__(self, target):
        self.arget = target

cfg = OmegaConf.create({"_target_": "repro.Foo", "target": "asd"})

with raises(TypeError):
    instantiate(cfg)

No error is raised if the 'target' parameter is renamed.

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

Successfully merging a pull request may close this issue.

4 participants