Pros,
I need you right now! I tried to make my function-description look beautiful but it didnt work. The problem is that I have two types of returns: a list and a string. Then you hover on functions vscode shows the returns of the functions like this.
I found out that you can define one type of return using this example (-> str):
def function(x, y) -> str:
string = "test"
return string
But let’s say I have tow diffrent return in my code:
def function(x, y):
if x == 1:
string = "test"
return string
else:
list = [1, 2]
return list
How to assign two different types of returns to that function? here is another example
>Solution :
Depending on your Python version, there are multiple ways this can be epxressed:
-
Prior to 3.9:
from typing import Union, List def foo() -> Union[str, List]: pass -
Since Python v. 3.9 you can also write:
from typing import Union def foo() -> Union[str, list]: pass -
Since Python v. 3.10 you can also write:
def foo() -> str | list: pass