Annotating a function with passthrough kwargs #1501
Unanswered
BrandonNav
asked this question in
Q&A
Replies: 1 comment 4 replies
-
Your approach looks OK to me. If you really want to avoid any potential breaking changes, you'd need to support both The reason you're not seeing a return type in mypy is that it never infers return types for functions. If you add an explicit return type annotation to Here are a couple of other approaches you might consider:
from functools import wraps
@wraps(third_party_func)
def run(**kwargs: Any):
return third_party_func(**kwargs)
run = third_party_func
def run(*, a: int, b: int) -> float:
return third_party_func(a=a, b=b) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I have a function which has no parameters other than kwargs which are to be passed onto a third party library call.
Here is a simplified example
I'd like to type
kwargs
so I can get both completion suggestions and better safety. I could create a TypedDict with all the parameters of the function call but that potentially breaks whenever the 3rd party is updated.What I'd like to do is have a type checker consider the function signature of one function as the same as another.
Given I'm just passing the parameters through from one function to another, I considered pretending my function is akin to a decorator, so I could somehow use Paramspec to capture the kwargs of the function and pass them along
Something like the following:
This "works" in so far as I can get completions (in vscode) and if I purely rely on my
wrapped_run
I should get warnings. Butkwargs
is still untyped (it be a dict[str, Any]) and I feel this isn't the intention of Paramspec and I'm risking this breaking in the future. Also, the fact mypy can't infer the return type (even with--new-type-inference
) makes me think I'm on the wrong track.I also thought over if I could use
Unpack
but you can't unpackP.kwargs
nor can I find a way to statically create a TypedDict from a function signature.Is there another way to accomplish this?
Beta Was this translation helpful? Give feedback.
All reactions