Skip to content

Commit

Permalink
Improve cheat sheet
Browse files Browse the repository at this point in the history
- Mention PEP 604 unions
- Separate out the 3.8 stuff to keep things readable

Linking python#13681
  • Loading branch information
hauntsaninja committed Oct 12, 2022
1 parent dbe9a88 commit d0c380d
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions docs/source/cheat_sheet_py3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,27 @@ Useful built-in types

.. code-block:: python
from typing import List, Set, Dict, Tuple, Optional
# For most types, just use the name of the type
x: int = 1
x: float = 1.0
x: bool = True
x: str = "test"
x: bytes = b"test"
# For collections, the type of the collection item is in brackets
# (Python 3.9+)
# For collections on Python 3.9+, the type of the collection item is in brackets
x: list[int] = [1]
x: set[int] = {6, 7}
# In Python 3.8 and earlier, the name of the collection type is
# capitalized, and the type is imported from the 'typing' module
x: List[int] = [1]
x: Set[int] = {6, 7}
# For mappings, we need the types of both keys and values
x: dict[str, float] = {"field": 2.0} # Python 3.9+
x: Dict[str, float] = {"field": 2.0}
# For tuples of fixed size, we specify the types of all the elements
x: tuple[int, str, float] = (3, "yes", 7.5) # Python 3.9+
x: Tuple[int, str, float] = (3, "yes", 7.5)
# For tuples of variable size, we use one type and ellipsis
x: tuple[int, ...] = (1, 2, 3) # Python 3.9+
x: Tuple[int, ...] = (1, 2, 3)
from typing import Optional
# Use Optional[] for values that could be None
x: Optional[str] = some_function()
Expand All @@ -74,6 +65,16 @@ Useful built-in types
assert x is not None
print(x.upper())
from typing import List, Set, Dict, Tuple
# In Python 3.8 and earlier, the name of the collection type is
# capitalized, and the type is imported from the 'typing' module
x: List[int] = [1]
x: Set[int] = {6, 7}
x: Dict[str, float] = {"field": 2.0}
x: Tuple[int, str, float] = (3, "yes", 7.5)
x: Tuple[int, ...] = (1, 2, 3)
Functions
*********

Expand Down Expand Up @@ -143,7 +144,9 @@ When you're puzzled or when things are complicated
# message with the type; remove it again before running the code.
reveal_type(1) # Revealed type is "builtins.int"
# Use Union when something could be one of a few types
# On Python 3.10, use the | operator when something could be one of a few types
x: list[int | str] = [3, 5, "test", "fun"]
# On earlier versions, use Union
x: list[Union[int, str]] = [3, 5, "test", "fun"]
# If you initialize a variable with an empty container or "None"
Expand Down

0 comments on commit d0c380d

Please sign in to comment.