Hey everyone! I'm trying to add type annotations for my python decorator, but its proving to be really difficult to get right
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 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
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 when I use the get_organization_networks function, the function hints will show as the following:
(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]]