Skip to content

Commit 8ae71c7

Browse files
committed
Complement cheatsheet for Optionals
Add a paragrah to explain there are 2 ways to make the typechecker understand that an optional is not `None`: use a if condition or an `assert`.
1 parent 970d8a9 commit 8ae71c7

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

docs/source/cheat_sheet.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ Built-in types
4949
if x is not None:
5050
print x
5151
52+
# There are 2 ways you can make mypy understand that
53+
# an Optional value is not None
54+
x = some_function() # type: Optional[str]
55+
# First way: with an if condition
56+
if x is not None:
57+
print(x.upper())
58+
# Second way: with an assert statement
59+
assert x is not None
60+
print(x.upper())
5261
5362
Functions
5463
*********

docs/source/cheat_sheet_py3.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ Built-in types
7575
if x is not None:
7676
print(x)
7777
78+
# There are 2 ways you can make mypy understand that
79+
# an Optional value is not None
80+
x: Optional[str] = some_function()
81+
# First way: with an if condition
82+
if x is not None:
83+
print(x.upper())
84+
# Second way: with an assert statement
85+
assert x is not None
86+
print(x.upper())
7887
7988
Functions
8089
*********

0 commit comments

Comments
 (0)