If your code is designed to work with Python 3.10 or newer, you want to use the PEP 604 syntax, using ... | None union syntax, and not use typing.Optional:
def test(a: dict[Any, Any] | None = None) -> None:
#print(a) ==> {'a': 1234}
#or
#print(a) ==> None
def test(a: list[Any] | None = None) -> None:
#print(a) ==> [1, 2, 3, 4, 'a', 'b']
#or
#print(a) ==> None
Code that still supports older Python versions can still stick to using Optional. Optional[...] is a shorthand notation for Union[..., None], telling the type checker that either an object of the specific type is required, or None is required. ... stands for any valid type hint, including complex compound types or a Union[] of more types. Whenever you have a keyword argument with default value None, you should use Optional.
So for your two examples, you have dict and list container types, but the default value for the a keyword argument shows that None is permitted too so use Optional[...]:
from typing import Optional
def test(a: Optional[dict] = None) -> None:
#print(a) ==> {'a': 1234}
#or
#print(a) ==> None
def test(a: Optional[list] = None) -> None:
#print(a) ==> [1, 2, 3, 4, 'a', 'b']
#or
#print(a) ==> None
There is technically no difference between using Optional[] on a Union[], or just adding None to the Union[]. So Optional[Union[str, int]] and Union[str, int, None] are exactly the same thing.
Personally, I'd stick with always using Optional[] when setting the type for a keyword argument that uses = None to set a default value, this documents the reason why None is allowed better. Moreover, it makes it easier to move the Union[...] part into a separate type alias, or to later remove the Optional[...] part if an argument becomes mandatory.
For example, say you have
from typing import Optional, Union
def api_function(optional_argument: Optional[Union[str, int]] = None) -> None:
"""Frob the fooznar.
If optional_argument is given, it must be an id of the fooznar subwidget
to filter on. The id should be a string, or for backwards compatibility,
an integer is also accepted.
"""
then documentation is improved by pulling out the Union[str, int] into a type alias:
from typing import Optional, Union
# subwidget ids used to be integers, now they are strings. Support both.
SubWidgetId = Union[str, int]
def api_function(optional_argument: Optional[SubWidgetId] = None) -> None:
"""Frob the fooznar.
If optional_argument is given, it must be an id of the fooznar subwidget
to filter on. The id should be a string, or for backwards compatibility,
an integer is also accepted.
"""
The refactor to move the Union[] into an alias was made all the much easier because Optional[...] was used instead of Union[str, int, None]. The None value is not a 'subwidget id' after all, it's not part of the value, None is meant to flag the absence of a value.
Side note: Unless your code only has to support Python 3.9 or newer, you want to avoid using the standard library container types in type hinting, as you can't say anything about what types they must contain. So instead of dict and list, use typing.Dict and typing.List, respectively. And when only reading from a container type, you may just as well accept any immutable abstract container type; lists and tuples are Sequence objects, while dict is a Mapping type:
from typing import Mapping, Optional, Sequence, Union
def test(a: Optional[Mapping[str, int]] = None) -> None:
"""accepts an optional map with string keys and integer values"""
# print(a) ==> {'a': 1234}
# or
# print(a) ==> None
def test(a: Optional[Sequence[Union[int, str]]] = None) -> None:
"""accepts an optional sequence of integers and strings
# print(a) ==> [1, 2, 3, 4, 'a', 'b']
# or
# print(a) ==> None
In Python 3.9 and up, the standard container types have all been updated to support using them in type hints, see PEP 585. But, while you now can use dict[str, int] or list[Union[int, str]], you still may want to use the more expressive Mapping and Sequence annotations to indicate that a function won't be mutating the contents (they are treated as 'read only'), and that the functions would work with any object that works as a mapping or sequence, respectively.
Python 3.10 introduces the | union operator into type hinting, see PEP 604. Instead of Union[str, int] you can write str | int. In line with other type-hinted languages, the preferred (and more concise) way to denote an optional argument in Python 3.10 and up, is now Type | None, e.g. str | None or list | None.
If your code is designed to work with Python 3.10 or newer, you want to use the PEP 604 syntax, using ... | None union syntax, and not use typing.Optional:
def test(a: dict[Any, Any] | None = None) -> None:
#print(a) ==> {'a': 1234}
#or
#print(a) ==> None
def test(a: list[Any] | None = None) -> None:
#print(a) ==> [1, 2, 3, 4, 'a', 'b']
#or
#print(a) ==> None
Code that still supports older Python versions can still stick to using Optional. Optional[...] is a shorthand notation for Union[..., None], telling the type checker that either an object of the specific type is required, or None is required. ... stands for any valid type hint, including complex compound types or a Union[] of more types. Whenever you have a keyword argument with default value None, you should use Optional.
So for your two examples, you have dict and list container types, but the default value for the a keyword argument shows that None is permitted too so use Optional[...]:
from typing import Optional
def test(a: Optional[dict] = None) -> None:
#print(a) ==> {'a': 1234}
#or
#print(a) ==> None
def test(a: Optional[list] = None) -> None:
#print(a) ==> [1, 2, 3, 4, 'a', 'b']
#or
#print(a) ==> None
There is technically no difference between using Optional[] on a Union[], or just adding None to the Union[]. So Optional[Union[str, int]] and Union[str, int, None] are exactly the same thing.
Personally, I'd stick with always using Optional[] when setting the type for a keyword argument that uses = None to set a default value, this documents the reason why None is allowed better. Moreover, it makes it easier to move the Union[...] part into a separate type alias, or to later remove the Optional[...] part if an argument becomes mandatory.
For example, say you have
from typing import Optional, Union
def api_function(optional_argument: Optional[Union[str, int]] = None) -> None:
"""Frob the fooznar.
If optional_argument is given, it must be an id of the fooznar subwidget
to filter on. The id should be a string, or for backwards compatibility,
an integer is also accepted.
"""
then documentation is improved by pulling out the Union[str, int] into a type alias:
from typing import Optional, Union
# subwidget ids used to be integers, now they are strings. Support both.
SubWidgetId = Union[str, int]
def api_function(optional_argument: Optional[SubWidgetId] = None) -> None:
"""Frob the fooznar.
If optional_argument is given, it must be an id of the fooznar subwidget
to filter on. The id should be a string, or for backwards compatibility,
an integer is also accepted.
"""
The refactor to move the Union[] into an alias was made all the much easier because Optional[...] was used instead of Union[str, int, None]. The None value is not a 'subwidget id' after all, it's not part of the value, None is meant to flag the absence of a value.
Side note: Unless your code only has to support Python 3.9 or newer, you want to avoid using the standard library container types in type hinting, as you can't say anything about what types they must contain. So instead of dict and list, use typing.Dict and typing.List, respectively. And when only reading from a container type, you may just as well accept any immutable abstract container type; lists and tuples are Sequence objects, while dict is a Mapping type:
from typing import Mapping, Optional, Sequence, Union
def test(a: Optional[Mapping[str, int]] = None) -> None:
"""accepts an optional map with string keys and integer values"""
# print(a) ==> {'a': 1234}
# or
# print(a) ==> None
def test(a: Optional[Sequence[Union[int, str]]] = None) -> None:
"""accepts an optional sequence of integers and strings
# print(a) ==> [1, 2, 3, 4, 'a', 'b']
# or
# print(a) ==> None
In Python 3.9 and up, the standard container types have all been updated to support using them in type hints, see PEP 585. But, while you now can use dict[str, int] or list[Union[int, str]], you still may want to use the more expressive Mapping and Sequence annotations to indicate that a function won't be mutating the contents (they are treated as 'read only'), and that the functions would work with any object that works as a mapping or sequence, respectively.
Python 3.10 introduces the | union operator into type hinting, see PEP 604. Instead of Union[str, int] you can write str | int. In line with other type-hinted languages, the preferred (and more concise) way to denote an optional argument in Python 3.10 and up, is now Type | None, e.g. str | None or list | None.
Directly from mypy typing module docs.
Optional[str]is just a shorthand or alias forUnion[str, None]. It exists mostly as a convenience to help function signatures look a little cleaner.
Update for Python 3.10+
you can now use the pipe operator as well.
# Python < 3.10
def get_cars(size: Optional[str]=None):
pass
# Python 3.10+
def get_cars(size: str|None=None):
pass
python - How do I define a function with optional arguments? - Stack Overflow
Type hinting with for callables with optional arguments
Adding optional arguments to a function
python - Optional argument type annotation - Stack Overflow
Videos
Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.
Copydef myfunc(a,b, *args, **kwargs):
for ar in args:
print ar
myfunc(a,b,c,d,e,f)
And it will print values of c,d,e,f
Similarly you could use the kwargs argument and then you could name your parameters.
Copydef myfunc(a,b, *args, **kwargs):
c = kwargs.get('c', None)
d = kwargs.get('d', None)
#etc
myfunc(a,b, c='nick', d='dog', ...)
And then kwargs would have a dictionary of all the parameters that are key valued after a,b
Try calling it like: obj.some_function( '1', 2, '3', g="foo", h="bar" ). After the required positional arguments, you can specify specific optional arguments by name.
Hello, everyone.
I am working on a python script that has a specific function that takes as its first argument another callable (called metrics) with signatures like the following:
def metric(mos: Optional["MOS_CS"] = None, Vg: Optional[float] = None, Vd: Optional[float] = None, f: Optional[float] = None, T: Optional[float] = None, ) -> complex:
The way I've annotated those metrics in the function's signature is the following:
Callable[[Optional["MOS_CS"], Optional[float], Optional[float], Optional[float], Optional[float]], complex]
where MOS_CS is a custom class.
The problem I'm having is that different metrics may have some of those parameters that are not optional, such as the following:
def other_metric(mos: Optional["MOS_CS"] = None,
Vg: float,
f: float,
Vd: Optional[float] = None,
T: Optional[float] = None,
) -> complex:
The thing is, every metric should accept those 4 parameters no matter what, but not all metrics will use them, and when not used, they should be equal to None.
I tried passing the following metric to my function:
def S12(mos: "MOS_CS", Vg: float, Vd: float, f: float, T: float, ) -> complex:
This metric needs all 4 parameters. But mypy gives me the following error:
Argument "metric" to "metric_wrt_f" of "MOS_CS" has incompatible type "Callable[[MOS_CS, float, float, float, float], complex]"; expected "Callable[[MOS_CS | None, float | None, float | None, float | None, float | None], complex]"
There are 16 possible combinations of having the parameters optional or not. How can I annotate my function such that it accepts all 16 possibilities without doing a big union between them all?
EDIT: Forgot to write that all parameters are always passed by name.
EDIT 2: Following the suggestion from u/speedy19981, I've looked into protocols and realised I would be better served by a callback protocol instead of the regular type annotation. But my problem kinda persists.
My callback protocol is the following:
class Metric(Protocol): def __call__(self, *, mos: Optional["MOS_CS"] = None, Vg: Optional[float] = None, Vd: Optional[float] = None, f: Optional[float] = None, T: Optional[float] = None, ) -> complex: ...
I've added the asterisk to enforce the keyword-only argument passing I mentioned on my previous edit.
The metric metric shown previously on the text is compliant with the protocol Metric but the metric other_metric is not. I've tried making various callback protocols for each of the 16 possibilities and then create a super protocol that inherits all other protocols, but since each of the 16 protocols has a __call__ method with different signatures, it throws an error.
I could make 16 protocols Metric0 up to Metric15 and then create a type alias using unions like Metric0 | Metric1 ... Metric14 | Metric15, but that sounds un-pythonic to me. Ideally, I would have all that complexity hidden away inside the protocol class. Is it possible?
EDIT 3: I'm going to have all metrics with the same signature and make the check for the parameters I want to be mandatory inside each metric individually.
I am working on a project where I'm supposed to add new features to an existing codebase. As part of this, I need to add an optional argument to one of the functions but just adding the optional argument is causing some of my unit tests to fail.
The function looks like the following initially:
def function(data1: list,
data2: list,
opt1: Optional[list],
) After adding another optional argument it looks like this:
def function(
data1: list,
data2: list,
opt1: Optional[list],
new: Optional[dict],
)The only change I'm making in the codebase is adding this optional argument and it is causing some of my unit tests to fail. I was wondering if someone knows what might be the reason ?