Skip to content

Passing callable as instantiate _target_ fails for nested classes #1914

Closed

Description

@Jasha10

Hydra's instantiate function allows users to pass a callable (rather than a
string) via the _target_ keyword argument. For example:

def fn():
    return "fn return"


class A:
    def __init__(self):
        print("in A.__init__")

    @classmethod
    def method(self):
        return "A.method return"

# passing a callable (rather than a string) as _target_:
fn_return = instantiate({}, _target_=fn)
A_instance = instantiate({}, _target_=A)
A_method_return = instantiate({}, _target_=A.method)

However, this fails when a nested class is passed as the _target_:

class A:
    class B:
        def __init__(self):
            print("in A.B.__init__")

A_B_instance = instantiate({}, _target_=A.B)  # ImportError: Encountered AttributeError while loading '__main__.B': module '__main__' has no attribute 'B'

The error message indicates that __main__.B could not be found. This indicates a mistake in handling the keyword argument _target_=A.B: it is __main__.A.B that should have been looked up, not __main__.B.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions