317 words
2 minutes
[Python] Annotations to typescript developers
[Basic] Python Annotations
Quick guild for trpescript developers to check python annotations syntax.
Common types
Basic
# Numbers
int_num: int = 68
float_num: float = 32.2
double_num: double = 320.233
# String
name: str = "Peter"
by: bytes = b"test"
is_ok: bool = False
Special
# List / Array
num_list: list = []
num_list_specific: list[int] = [1,2,3] ## Python 3.9
# Special
mytuple: tuple = ("apple", "banana", "cherry")
mytuple_specific: tuple[int, str, str] = (1, "banana", "cherry") # Python 3.9+
mySet: set[int] = {6, 7} ## Python 3.9
myDict: dict[str, int] = { "age" : 30 }
# Any
whatever: Any = False
TIPFor Python 3.8 and earlier, you may use the
typing
collectionfrom typing import List, Set, Dict, Tuple
Functions
def add(a: int, b: int) -> int:
return a + b
def addVoid(a: int, b: int) -> None:
print( a + b )
# Python < 3.10+ in case
from typing import Iterator
def gen(n: int) -> Iterator[int]:
i = 0
while i < n:
yield i
i += 1
Special type method
Literal
types
The Literal
is used to define your own output types like enum
def isBigger(a: int, b: int) -> Literal["yes", "no"]:
return "yes" if a > b else "no"
TIPFor Python 3.8 and earlier, you may use
typing
collectionfrom typing import Literal
type
types
The type
is used to define your own type for later used.
# Define a type
type Point = tuple[float, float]
myPoint: Point = (1.1, 4.7)
def makePoint(a: float, b: float) -> Point:
return (a, b)
Union
types
The Union
is used to define mixed type.
from typing import Union # Python < 3.10+ in case
# Python 3.10+
mix_list: list[str | bool] = ["omg", True, "ok"]
mix_list_union: list[ Union[str | bool] ] = ["gg", True, "ok"]
Optional
types
The Optional
is used to define optional params in functions.
from typing import Optional # Python < 3.10+ in case
def isBigger(a: Optional[str] = None) -> None:
print(a)
# Other usage beside Optional
def isBigger(a: str | None = None) -> None:
print(a)