The special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list. The syntax is to use the symbol * to take in a variable number of arguments; by convention, it is often used with the word args.
*args passes variable number of non-keyworded arguments and on which operation of the tuple can be performed. **kwargs passes variable number of keyword arguments dictionary to function on which operation of a dictionary can be performed.
Type annotations — also known as type signatures — are used to indicate the datatypes of variables and input/outputs of functions and methods. In many languages, datatypes are explicitly stated. In these languages, if you don't declare your datatype — the code will not run.
List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
For variable positional arguments (*args
) and variable keyword arguments (**kw
) you only need to specify the expected value for one such argument.
From the Arbitrary argument lists and default argument values section of the Type Hints PEP:
Arbitrary argument lists can as well be type annotated, so that the definition:
def foo(*args: str, **kwds: int): ...
is acceptable and it means that, e.g., all of the following represent function calls with valid types of arguments:
foo('a', 'b', 'c') foo(x=1, y=2) foo('', z=0)
So you'd want to specify your method like this:
def foo(*args: int):
However, if your function can only accept either one or two integer values, you should not use *args
at all, use one explicit positional argument and a second keyword argument:
def foo(first: int, second: Optional[int] = None):
Now your function is actually limited to one or two arguments, and both must be integers if specified. *args
always means 0 or more, and can't be limited by type hints to a more specific range.
The proper way to do this is using @overload
from typing import overload
@overload
def foo(arg1: int, arg2: int) -> int:
...
@overload
def foo(arg: int) -> int:
...
def foo(*args):
try:
i, j = args
return i + j
except ValueError:
assert len(args) == 1
i = args[0]
return i
print(foo(1))
print(foo(1, 2))
Note that you do not add @overload
or type annotations to the actual implementation, which must come last.
You'll need a newish version of both typing
and mypy to get support for @overload outside of stub files.
You can also use this to vary the returned result in a way that makes explicit which argument types correspond with which return type. e.g.:
from typing import Tuple, overload
@overload
def foo(arg1: int, arg2: int) -> Tuple[int, int]:
...
@overload
def foo(arg: int) -> int:
...
def foo(*args):
try:
i, j = args
return j, i
except ValueError:
assert len(args) == 1
i = args[0]
return i
print(foo(1))
print(foo(1, 2))
As a short addition to the previous answer, if you're trying to use mypy on Python 2 files and need to use comments to add types instead of annotations, you need to prefix the types for args
and kwargs
with *
and **
respectively:
def foo(param, *args, **kwargs):
# type: (bool, *str, **int) -> None
pass
This is treated by mypy as being the same as the below, Python 3.5 version of foo
:
def foo(param: bool, *args: str, **kwargs: int) -> None:
pass
While you can annotate variadic arguments with a type, I don't find it very useful because it assumes that all arguments are of the same type.
The proper type annotation of *args
and **kwargs
that allows specifying each variadic argument separately is not supported by mypy yet. There is a proposal for adding an Expand
helper on mypy_extensions
module, it would work like this:
class Options(TypedDict):
timeout: int
alternative: str
on_error: Callable[[int], None]
on_timeout: Callable[[], None]
...
def fun(x: int, *, **options: Expand[Options]) -> None:
...
The GitHub issue was opened on January 2018 but it's still not closed. Note that while the issue is about **kwargs
, the Expand
syntax will likely be used for *args
as well.
In some cases the content of **kwargs can be a variety of types.
This seems to work for me:
from typing import Any
def testfunc(**kwargs: Any) -> None:
print(kwargs)
or
from typing import Any, Optional
def testfunc(**kwargs: Optional[Any]) -> None:
print(kwargs)
In the case where you feel the need to constrain the types in **kwargs
I suggest creating a struct-like object and add the typing there. This can be done with dataclasses, or pydantic.
from dataclasses import dataclass
@dataclass
class MyTypedKwargs:
expected_variable: str
other_expected_variable: int
def testfunc(expectedargs: MyTypedKwargs) -> None:
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With