Skip to content

Implement nn.Module.__dir__ #987

@wcbeard

Description

@wcbeard

If I create a module class and add a layer to it, I can't see this new layer when I try to use IPython's autocomplete. For example, with the the following class:

class Example(nn.Module):
    def __init__(self):
        super().__init__()
        self.fully_connected = nn.Linear(20, 1)

ex = Example()

When I type ex.f<tab>, the only options are 'forward' and 'float'. And when I type ex.fully_connected.b, I don't get the completion option for the bias attribute.

From my limited understanding, I think that this has to do with the strings that are returned with the __dir__ method:

[attr for attr in ex.__dir__() if attr.startswith('f')]
=> ['forward', 'float']

So for the first issue, if I modify the class to have a method

def __dir__(self):
    return super().__dir__() + list(self._modules)

then I get the fully_connected as a possible completion.

I'm not sure if this is because of an intentional design decision, but it would be nice if all of the relevant possible attributes were available with the auto complete, as I'm still becoming familiar with this library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions