In PyTorch, how to make __call__() of nn.Module automatically copy the type hints and docstring of forward()?

It is common to implement the forward steps in forward() and call it through __call__, like this example

from torch import nn, FloatTensor, IntTensor

class MyModule(nn.Module): 
    def __init__(self, ...) -> None: 
        nn.Module.__init__(self)
        ...
    def forward(self, x: FloatTensor, y: FloatTensor) -> tuple[FloatTensor, IntTensor]: 
        """
        Args:
            x (FloatTensor): in shape of BxTxE
            y (FloatTensor): in shape of BxE

        Returns:
            tuple[FloatTensor, IntTensor]: (sth. in shape of BxT, sth. in shape of B)
        """
        ... # implementation of forward steps

model = MyModule(...)
...
a, b = model(x, y) # call it through __call__

However, IDEs like VSCode cannot recognize the type hints or docstring of __call__ because it’s a completely different method without overloading.
Though this is reasonable in principle of Python, it is still unfriendly to circumstances like co-operating that needs convenient coding hints.

A possible but clumsy solution is copy those information to overload of __call__() in each nn.Module:

from torch import nn, FloatTensor, IntTensor

class MyModule(nn.Module): 
    def __init__(self, ...) -> None: 
        nn.Module.__init__(self)
        ...
    def forward(self, x: FloatTensor, y: FloatTensor) -> tuple[FloatTensor, IntTensor]: 
        """
        Args:
            x (FloatTensor): in shape of BxTxE
            y (FloatTensor): in shape of BxE

        Returns:
            tuple[FloatTensor, IntTensor]: (sth. in shape of BxT, sth. in shape of B)
        """
        ... # implementation of forward steps
    def __call__(self, x: FloatTensor, y: FloatTensor) -> tuple[FloatTensor, IntTensor]: 
        """
        Args:
            x (FloatTensor): in shape of BxTxE
            y (FloatTensor): in shape of BxE

        Returns:
            tuple[FloatTensor, IntTensor]: (sth. in shape of BxT, sth. in shape of B)
        """
        return nn.Module.__call__(self, x, y)

model = MyModule(...)
...
a, b = model(x, y) # call through __call__

So, how can I tell python or VSCode that __call__() and forward() share identical input/return types and docstring in any subclass of nn.Module, without write them again in overload of __call__() of each subclass?

(I guess possible solution for docstrings may be decorators? But I have no idea about copying type hints. )

4

Just a temporary solution, which is just a little bit better than copying hints each time, inspired by @InSync and the decorator in related question:

from typing import TypeVar, Callable
from typing_extensions import ParamSpec # for 3.7 <= python < 3.10, import from typing for versions later

T = TypeVar('T')
P = ParamSpec('P')
def take_annotation_from(this: Callable[P, T]) -> Callable[[Callable], Callable[P, T]]: 
    def decorator(real_function: Callable[P, T]) -> Callable[P, T]: 
        # typing real_function with Callable[P, T] can directly tell python the input type (at least for code hinting in VSCode)
        # so wrap the real_function like the related question is unnecessary
        real_function.__doc__ = this.__doc__
        return real_function
    return decorator

And use it as

from torch.nn import Module

class MyModule(Module): 
    def __init__(self, k: float):
        ...
    
    def forward(self, ...) -> ...: # with type hints
        """docstring"""
        ...

    @take_annotation_from(forward)
    def __call__(self, *args, **kwds):
        return Module.__call__(self, *args, **kwds)

And this solution may be proved if last three lines of the code above can be packed as something like macro, because it remains unchanged among different implementations of sub-nn.Modules.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

In PyTorch, how to make __call__() of nn.Module automatically copy the type hints and docstring of forward()?

It is common to implement the forward steps in forward() and call it through __call__, like this example

from torch import nn, FloatTensor, IntTensor

class MyModule(nn.Module): 
    def __init__(self, ...) -> None: 
        nn.Module.__init__(self)
        ...
    def forward(self, x: FloatTensor, y: FloatTensor) -> tuple[FloatTensor, IntTensor]: 
        """
        Args:
            x (FloatTensor): in shape of BxTxE
            y (FloatTensor): in shape of BxE

        Returns:
            tuple[FloatTensor, IntTensor]: (sth. in shape of BxT, sth. in shape of B)
        """
        ... # implementation of forward steps

model = MyModule(...)
...
a, b = model(x, y) # call it through __call__

However, IDEs like VSCode cannot recognize the type hints or docstring of __call__ because it’s a completely different method without overloading.
Though this is reasonable in principle of Python, it is still unfriendly to circumstances like co-operating that needs convenient coding hints.

A possible but clumsy solution is copy those information to overload of __call__() in each nn.Module:

from torch import nn, FloatTensor, IntTensor

class MyModule(nn.Module): 
    def __init__(self, ...) -> None: 
        nn.Module.__init__(self)
        ...
    def forward(self, x: FloatTensor, y: FloatTensor) -> tuple[FloatTensor, IntTensor]: 
        """
        Args:
            x (FloatTensor): in shape of BxTxE
            y (FloatTensor): in shape of BxE

        Returns:
            tuple[FloatTensor, IntTensor]: (sth. in shape of BxT, sth. in shape of B)
        """
        ... # implementation of forward steps
    def __call__(self, x: FloatTensor, y: FloatTensor) -> tuple[FloatTensor, IntTensor]: 
        """
        Args:
            x (FloatTensor): in shape of BxTxE
            y (FloatTensor): in shape of BxE

        Returns:
            tuple[FloatTensor, IntTensor]: (sth. in shape of BxT, sth. in shape of B)
        """
        return nn.Module.__call__(self, x, y)

model = MyModule(...)
...
a, b = model(x, y) # call through __call__

So, how can I tell python or VSCode that __call__() and forward() share identical input/return types and docstring in any subclass of nn.Module, without write them again in overload of __call__() of each subclass?

(I guess possible solution for docstrings may be decorators? But I have no idea about copying type hints. )

4

Just a temporary solution, which is just a little bit better than copying hints each time, inspired by @InSync and the decorator in related question:

from typing import TypeVar, Callable
from typing_extensions import ParamSpec # for 3.7 <= python < 3.10, import from typing for versions later

T = TypeVar('T')
P = ParamSpec('P')
def take_annotation_from(this: Callable[P, T]) -> Callable[[Callable], Callable[P, T]]: 
    def decorator(real_function: Callable[P, T]) -> Callable[P, T]: 
        # typing real_function with Callable[P, T] can directly tell python the input type (at least for code hinting in VSCode)
        # so wrap the real_function like the related question is unnecessary
        real_function.__doc__ = this.__doc__
        return real_function
    return decorator

And use it as

from torch.nn import Module

class MyModule(Module): 
    def __init__(self, k: float):
        ...
    
    def forward(self, ...) -> ...: # with type hints
        """docstring"""
        ...

    @take_annotation_from(forward)
    def __call__(self, *args, **kwds):
        return Module.__call__(self, *args, **kwds)

And this solution may be proved if last three lines of the code above can be packed as something like macro, because it remains unchanged among different implementations of sub-nn.Modules.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật