Skip to content

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

Closed

Description

@fabiocarrara

🐛 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

Activity

  1. modified the milestone: Hydra 1.1.0 on Jun 11, 2021
  2. omry commented on Jun 11, 2021

    @omry
    Collaborator

    Thanks for reporting, we will take a look.

  3. added this to the Hydra 1.1.1 milestone on Jun 11, 2021
  4. Jasha10 commented on Jun 11, 2021

    @Jasha10
    Contributor

    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.

  5. self-assigned this
    on Jun 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

In progressWork in progressbugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions