From ca95897e142148ed88542b80d84dbda1688b0ffa Mon Sep 17 00:00:00 2001 From: Vincent Perez Date: Sat, 15 Dec 2018 12:13:27 +0100 Subject: [PATCH 1/3] Complement cheatsheet for Optionals Add a paragrah to explain how to indicate to the type checker that an Optional value is not None: with an if statement, or an assert (depending upon the situation) --- docs/source/cheat_sheet.rst | 8 ++++++-- docs/source/cheat_sheet_py3.rst | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/source/cheat_sheet.rst b/docs/source/cheat_sheet.rst index 76a0922d3671..f18a4cb29849 100644 --- a/docs/source/cheat_sheet.rst +++ b/docs/source/cheat_sheet.rst @@ -46,9 +46,13 @@ Built-in types # Use Optional[] for values that could be None x = some_function() # type: Optional[str] + # To indicate to mypy that the value is not None, 2 cases: + # If it still may be None sometimes, use an if-statement if x is not None: - print x - + print x.upper() + # If it can never be None (e.g. due to some invariants), use an assert + assert x is not None + print x.upper() Functions ********* diff --git a/docs/source/cheat_sheet_py3.rst b/docs/source/cheat_sheet_py3.rst index 3c86186398c1..9e2c1badb28a 100644 --- a/docs/source/cheat_sheet_py3.rst +++ b/docs/source/cheat_sheet_py3.rst @@ -72,9 +72,13 @@ Built-in types # Use Optional[] for values that could be None x: Optional[str] = some_function() + # To indicate to mypy that the value is not None, 2 cases: + # If it still may be None sometimes, use an if-statement if x is not None: - print(x) - + print(x.upper()) + # If it can never be None (e.g. due to some invariants), use an assert + assert x is not None + print(x.upper()) Functions ********* From 6e4a7a5c46152cbf9735b104940618310ff6f3d6 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 22 Dec 2018 13:16:23 +0000 Subject: [PATCH 2/3] Tweak comments --- docs/source/cheat_sheet.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/source/cheat_sheet.rst b/docs/source/cheat_sheet.rst index f18a4cb29849..80ff62e9ee70 100644 --- a/docs/source/cheat_sheet.rst +++ b/docs/source/cheat_sheet.rst @@ -46,11 +46,10 @@ Built-in types # Use Optional[] for values that could be None x = some_function() # type: Optional[str] - # To indicate to mypy that the value is not None, 2 cases: - # If it still may be None sometimes, use an if-statement + # Mypy understands a value can't be None in an if-statement if x is not None: print x.upper() - # If it can never be None (e.g. due to some invariants), use an assert + # If a value can never be None due to some invariants, use an assert assert x is not None print x.upper() From 79377488f70ad7cee79cabf4b0b05b8dbd198fd5 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sat, 22 Dec 2018 13:18:04 +0000 Subject: [PATCH 3/3] Tweak comments (Py3) --- docs/source/cheat_sheet_py3.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docs/source/cheat_sheet_py3.rst b/docs/source/cheat_sheet_py3.rst index 9e2c1badb28a..2d8ac74c1786 100644 --- a/docs/source/cheat_sheet_py3.rst +++ b/docs/source/cheat_sheet_py3.rst @@ -72,11 +72,10 @@ Built-in types # Use Optional[] for values that could be None x: Optional[str] = some_function() - # To indicate to mypy that the value is not None, 2 cases: - # If it still may be None sometimes, use an if-statement + # Mypy understands a value can't be None in an if-statement if x is not None: print(x.upper()) - # If it can never be None (e.g. due to some invariants), use an assert + # If a value can never be None due to some invariants, use an assert assert x is not None print(x.upper())