Python Type Hints – *args and **kwargs (2021)
1–10 of 158 posts
Re: Python Type Hints – *args and **kwargs (2021)
#2Re: Python Type Hints – *args and **kwargs (2021)
#3This means that you will have 2^N combinations and doubling every time you accept a new argument.
If that is not good enough, then simply use a `TypedDict` with everything optional instead of `**kwargs`. Your call will then become `foo(SomeTypedDict(p1=p2,...))`.
Re: Python Type Hints – *args and **kwargs (2021)
#4def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int)
Anything after,*, is a kwarg.
Re: Python Type Hints – *args and **kwargs (2021)
#5This does restrict all of your keyword arguments to the same type. If you have keyword arguments of different types, you're right back to no type safety.
To do otherwise would require some form of vararg generics which is uncommon.
Re: Python Type Hints – *args and **kwargs (2021)
#6Why do people not just type everything they want passed? def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int) Anything after,*, is a kwarg.
[0] actually 3 positional-or-keyword which is even more widely divergent
Re: Python Type Hints – *args and **kwargs (2021)
#7Why do people not just type everything they want passed? def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int) Anything after,*, is a kwarg.
def sum(*args: int) -> int:
if len(args) == 0:
return 0
return args[0] + sum(*args[1:])Re: Python Type Hints – *args and **kwargs (2021)
#8https://qiskit.org/ecosystem/aer/stubs/qiskit_aer.primitives...
Re: Python Type Hints – *args and **kwargs (2021)
#9Why do people not just type everything they want passed? def variable(n:str, nn:str, nnn:str, *, a:int, b:int, c:int) Anything after,*, is a kwarg.
Re: Python Type Hints – *args and **kwargs (2021)
#10If your function just wraps another you can use the same type hints as the other function with functools.wraps https://docs.python.org/3/library/functools.html#functools.w...