From 6844b1593d239d560212f9ae5a543fc85bebf787 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sun, 31 Jan 2021 10:59:13 -0500 Subject: [PATCH 1/2] add type-annotated completions in jupyter; closes #136 --- .../Module5_OddsAndEnds/Writing_Good_Code.md | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/Python/Module5_OddsAndEnds/Writing_Good_Code.md b/Python/Module5_OddsAndEnds/Writing_Good_Code.md index ef90af0d..3b035d6e 100644 --- a/Python/Module5_OddsAndEnds/Writing_Good_Code.md +++ b/Python/Module5_OddsAndEnds/Writing_Good_Code.md @@ -5,14 +5,13 @@ jupyter: extension: .md format_name: markdown format_version: '1.2' - jupytext_version: 1.3.0rc1 + jupytext_version: 1.5.0 kernelspec: display_name: Python 3 language: python name: python3 --- - .. meta:: :description: Topic: Writing good code, Difficulty: Easy, Category: Section @@ -446,6 +445,32 @@ These tools are especially useful for large-scale code bases. Companies like Dro If you are using [VSCode as your IDE](https://www.pythonlikeyoumeanit.com/Module1_GettingStartedWithPython/Getting_Started_With_IDEs_and_Notebooks.html), you can install the [PyLance VSCode extension](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) to leverage pyright's type checking within your IDE. + +
+ +**Reading Comprehension: Type-Assisted Code Completion** + +The `jedi` package, which is installed by default alongside Jupyter and IPython, enables type-informed code completion. +This means that we can benefit from type-hints even we are working in a Jupyter notebook! + +In a Jupyter notebook, write the following type-annotated signature for a function: `def f(x: str):`. +Now in the body of the function (i.e. on the next line, and indented inwards), try typing `x.` and then hit ``. + +```python + +def f(x: str): + x.#hit here + +``` + +You should see a list of methods appear for `x`. +What are the first three methods that appear? +Do these make sense based on the annotation associated with `x`? +Now change the annotation for `x` to be `list` and trigger the auto-completion in the editor again; do you see an updated list of methods? + +
+ +
**Takeaway:** @@ -962,8 +987,17 @@ Take some time to review the NumPy and Google docstring specifications, pick one ## Reading Comprehension Solutions + +**Type-Assisted Code Completion: Solution** + +The tab-completion reveals methods like "capitalize", "casefold", and "center" – all of which are string methods. +These are made available despite the fact that `x` did not yet refer to a specific string. +Instead, the type-hint was able to tell the editor that `x` _will_ refer to a string, and this was enough to inform these type completions. + +Revising the type-annotation for `x` to be `list` affects the auto-completion options accordingly: the editor will now suggest list methods like "append", "clear", and "copy". + -**Type Hinting: Solutions** +**Type Hinting: Solution** The following is a well-annotated version of `get_first_and_last`: From 8cd233359741a2f051f3cea78289a957bd7bb005 Mon Sep 17 00:00:00 2001 From: Ryan Soklaski Date: Sun, 31 Jan 2021 11:21:50 -0500 Subject: [PATCH 2/2] Update Python/Module5_OddsAndEnds/Writing_Good_Code.md Co-authored-by: Petar Griggs --- Python/Module5_OddsAndEnds/Writing_Good_Code.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python/Module5_OddsAndEnds/Writing_Good_Code.md b/Python/Module5_OddsAndEnds/Writing_Good_Code.md index 3b035d6e..07a5ce1e 100644 --- a/Python/Module5_OddsAndEnds/Writing_Good_Code.md +++ b/Python/Module5_OddsAndEnds/Writing_Good_Code.md @@ -451,7 +451,7 @@ If you are using [VSCode as your IDE](https://www.pythonlikeyoumeanit.com/Module **Reading Comprehension: Type-Assisted Code Completion** The `jedi` package, which is installed by default alongside Jupyter and IPython, enables type-informed code completion. -This means that we can benefit from type-hints even we are working in a Jupyter notebook! +This means that we can benefit from type-hints even when we are working in a Jupyter notebook! In a Jupyter notebook, write the following type-annotated signature for a function: `def f(x: str):`. Now in the body of the function (i.e. on the next line, and indented inwards), try typing `x.` and then hit ``.