Type Annotations and Docstrings for Decorator

I’m trying to add type annotations for my python decorator, but its proving to be really difficult to get right.

Here is my decorator:

RT = TypeVar('RT')
P = ParamSpec('P')

def meraki_dashboard_setup(function: Callable[P, RT]):
    """
    Adds both a "meraki_key" and a "dashboard" argument to a decorated function. If the
    "meraki_key" argument is provided, the function will setup the dashboard object using the
    key. If the "dashboard" argument is provided, the function will use it directly.
    """
    @wraps(function)
    def wrapper(
        dashboard: Optional[DashboardAPI] = None,
        meraki_key: Optional[MerakiKey] = None,
        *args: P.args, **kwargs: P.kwargs
    ):
        if not dashboard and meraki_key:
            dashboard = setup_meraki_dashboard(meraki_key)
        if dashboard and meraki_key:
            raise ValueError(
                "Both a dashboard object and a Meraki key were provided. Only one should be used."
            )
        if not dashboard and not meraki_key:
            raise ValueError(
                "Neither a dashboard object nor a Meraki key were provided."
            )
        if not dashboard:
            raise ValueError("Unable to setup the Meraki dashboard object")

        kwargs["dashboard"] = dashboard
        return function(*args, **kwargs)

    return wrapper

And here is my decorated function

@meraki_dashboard_setup
def get_organization_networks(
    organization_id: str,
    **kwargs: Any
):
    """
    Fetch all networks from a specific organization.
    """
    dashboard = cast(DashboardAPI, kwargs.get("dashboard"))
    return cast(
        List[MerakiOrgNetwork],
        dashboard.organizations.getOrganizationNetworks(organization_id)
    )

My problem is that it “almost” works. When I use the get_organization_networks function, the function will make sure that the parameters “dashboard”, “meraki_key” and “organization_id” have the correct type, and it shows those parameters when you hover over the function.

However, since the decorator is inferring the output type of the decorator as the inferred output type of the “wrapper” function after being decorated with “wrapped”, the only hint I get through the linter is this

(function) get_organization_networks: _Wrapped[(organization_id: str, **kwargs: Any), List[MerakiOrgNetwork], (dashboard: DashboardAPI | None = None, meraki_key: MerakiKey | None = None, organization_id: str, **kwargs: Any), List[MerakiOrgNetwork]]

This hint basically consists of _Wrapped[P, RT, Mix of wrapper arguments + P, RT]. This is almost perfect, but I would love to get just this (like in a regular function):

(function) get_organization_networks(dashboard: DashboardAPI | None = None, meraki_key: MerakiKey | None = None, organization_id: str, **kwargs: Any) -> List[MerakiOrgNetwork]
Fetch all networks from a specific organization.

Seems like functools’s wraps decorator is not able to finish copying the metadata for the wrapper into the function, causing no docstring from the decorated function to be copied, as well as making the function signature appear all wonky. One way I found of forcing the wrapper to by typed properly is by adding an output type to the meraki_dashboard_setup function

def meraki_dashboard_setup(function: Callable[P, RT]) -> Callable[P, RT]:
   ...

This returns the following hint

(function) def get_organizations(**kwargs: Any) -> List[MerakiOrg]
Fetch all organizations from the Meraki Dashboard API

I’ve investigated how to get this working, but I cannot figure it out for the life of me. Maybe something to do with ParamSpec? I could technically leave it with my initial approach and it will work, but I really would like to have docstrings and a proper function signature. Its going to drive me insane if it doesn’t. What could I do?

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