@@ -43,18 +43,18 @@ Useful built-in types
4343 x: str = " test"
4444 x: bytes = b " test"
4545
46- # For collections on Python 3.9+ , the type of the collection item is in brackets
46+ # For collections, the type of the collection item is in brackets
4747 x: list[int ] = [1 ]
4848 x: set[int ] = {6 , 7 }
4949
5050 # For mappings, we need the types of both keys and values
51- x: dict[str , float ] = {" field" : 2.0 } # Python 3.9+
51+ x: dict[str , float ] = {" field" : 2.0 }
5252
5353 # For tuples of fixed size, we specify the types of all the elements
54- x: tuple[int , str , float ] = (3 , " yes" , 7.5 ) # Python 3.9+
54+ x: tuple[int , str , float ] = (3 , " yes" , 7.5 )
5555
5656 # For tuples of variable size, we use one type and ellipsis
57- x: tuple[int , ... ] = (1 , 2 , 3 ) # Python 3.9+
57+ x: tuple[int , ... ] = (1 , 2 , 3 )
5858
5959 # On Python 3.8 and earlier, the name of the collection type is
6060 # capitalized, and the type is imported from the 'typing' module
@@ -67,13 +67,12 @@ Useful built-in types
6767
6868 from typing import Union, Optional
6969
70- # On Python 3.10+, use the | operator when something could be one of a few types
71- x: list[int | str ] = [3 , 5 , " test" , " fun" ] # Python 3.10+
72- # On earlier versions, use Union
70+ # Use the | operator when something could be one of a few types
71+ x: list[int | str ] = [3 , 5 , " test" , " fun" ]
72+ # Union is equivalent
7373 x: list[Union[int , str ]] = [3 , 5 , " test" , " fun" ]
7474
75- # Use X | None for a value that could be None on Python 3.10+
76- # Use Optional[X] on 3.9 and earlier; Optional[X] is the same as 'X | None'
75+ # Use X | None for a value that could be None; Optional[X] is the same as X | None
7776 x: str | None = " something" if some_condition() else None
7877 if x is not None :
7978 # Mypy understands x won't be None here because of the if-statement
0 commit comments