Skip to content

Commit

Permalink
move union docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Oct 12, 2022
1 parent a43f1c8 commit b2538e3
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions docs/source/cheat_sheet_py3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ Useful built-in types
# For tuples of variable size, we use one type and ellipsis
x: tuple[int, ...] = (1, 2, 3) # Python 3.9+
from typing import Optional
from typing import Union, Optional
# Use Optional[] for values that could be None
x: Optional[str] = some_function()
# 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"]
# Use Optional[X] for a value that could be None
# (Optional[X] is the same as X | None or Union[X, None])
x: Optional[str] = "something" if some_condition() else None
# Mypy understands a value can't be None in an if-statement
if x is not None:
print(x.upper())
Expand Down Expand Up @@ -193,11 +199,6 @@ 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"
# 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"
# you may have to help mypy a bit by providing an explicit type annotation
x: list[str] = []
Expand Down

0 comments on commit b2538e3

Please sign in to comment.