Skip to content

Add explicit typing to nn.Module __init__() #156740

@gabriele-marino

Description

@gabriele-marino

🐛 Describe the bug

nn.Module is missing explicit typing for *args and **kwargs in its constructor.

For this reason, the following code generates an error in static type checkers like pyright, when using strict mode.

class MyModule(nn.Module):
    def __init__(self, ...):
        super().__init__()  # Error: Type of "__init__" is partially unknown

To solve, the constructor of Module should be typed as

class Module:
    def __init__(self, *args: Any, **kwargs: Any) -> None:

Versions

The torch version I am using is 2.7.0.

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @ezyang @malfet @xuzhao9 @gramster

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: nnRelated to torch.nnmodule: typingRelated to mypy type annotationstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions