Skip to content

Latest commit

 

History

History
1006 lines (674 loc) · 24.5 KB

slides.md

File metadata and controls

1006 lines (674 loc) · 24.5 KB
theme class highlighter colorSchema download preload fonts info title
./simple
text-center
prism
light
true
true
sans serif mono italic
IBM Plex Sans
IBM Plex
Fira Code
true
## Simple-slidev-sample Simple Slidev Sample
Type Annotations in Python

Type Annotations in Python

<style> code { font-size: 36px; } </style>

Terribly Intimidating or Tremendously Informative?

 

Gregory M. Kapfhammer

PyOhio 2021

 
def start(t: Talk) -> List[Fun, Learn]:

Okay, what is this about?

<style> h2 { font-size: 36px; @apply text-orange-600 mb-4; } </style>

Key Questions

What are the benefits and challenges associated with using type annotations inside of Python program? Will types make me a better programmer?


Intended Audience

An adventuresome Python programmer who wants to explore how both a new paradigm and software tools can improve their development skills!

Let's explore type annotations in Python programs!

Python Program without Annotations

def extract_urls(df):
    """Extract a list of urls."""
    urls = []
    if "Url" in df.columns:
        urlc = df["Url"]
        if urlc is not None:
            urls = urlc.tolist()
    return urls

What is the type of df ? The terrible docstring does not say!

What is the behavior of return urls in this function?


Python Program without Annotations

<style> </style>
def extract_urls(df):
    """Extract a list of urls."""
    urls = []
    if "Url" in df.columns:
        urlc = df["Url"]
        if urlc is not None:
            urls = urlc.tolist()
    return urls

What happens if the program becomes more complex?

Python Program with Annotations

def extract_urls(df: pandas.DataFrame) -> List[str]:
    """Extract a list of urls."""
    urls = []
    if "Url" in df.columns:
        urlc = df["Url"]
        if urlc is not None:
            urls = urlc.tolist()
    return urls

What is the purpose of df: pandas.DataFrame ?

How does List[str] describe output of extract_urls ?


Wait, isn't this more complicated?
Do type annotations have any benefits?
What are the trade-offs of type annotations?

Challenges

<style> li { font-size: 22px; margin-bottom: 10px; } </style>
  • Readability : function signatures are more difficult to read
  • Productivity : programmers often must add type annotations
  • Complexity : programs use many new classes and types

Benefits

  • Fail-fast : quickly catch errors before running Python programs
  • Tooling : text editors signal problems to programmers
  • Understanding : developers understand the structure of data
Pyright language server in VS Code and Neovim
Mypy static type checker in terminal or editor

Easy command-line interface with Typer
Quickly find a defect that crashes a program
AnalyzeActions/WorkKnow

Command-Line Interface with Typer

import typer
cli = typer.Typer()
@cli.command()
def download(
    repo_urls: List[str],
    repos_csv_file: Path = typer.Option(None),
    results_dir: Path = typer.Option(None),
    env_file: Path = typer.Option(None),
):
See AnalyzeActions/WorkKnow for details!

Command-Line Interface

<style> h2 { font-size: 42px; @apply text-orange-600 mb-4; } li { font-size: 28px; margin-top: 4px; margin-bottom: 9px; } </style>
Usage: workknow download [OPTIONS] REPO_URLS...
  Download the GitHub Action workflow history of repositories.
Arguments:
  REPO_URLS...  [required]
Options:
  --repos-csv-file PATH
  --results-dir PATH
  --env-file PATH
  --peek / --no-peek              [default: False]
  --save / --no-save              [default: False]
  --debug-level [DEBUG|INFO|WARNING|ERROR|CRITICAL]
                                  [default: ERROR]
  --help                          Show this message and exit.
  • Using type annotations, Typer can:
    • automatically generate all menus
    • perform error checking on all arguments
    • convert all arguments to the correct type

Defect Detection with Pyright

def create_results_zip_file(
    results_dir: Path, results_files: List[str]
 ) -> None:
    """Make a .zip file of all results."""
    with zipfile.ZipFile(
        "results/All-WorkKnow-Results.zip",
        "w",
    ) as results_zip_file:
        for results_file in results_files:
            results_zip_file.write(results_files)

Pyright Feedback in VS Code

<style> h2 { font-size: 42px; @apply text-orange-600 mb-4; } li { font-size: 28px; margin-top: 4px; margin-bottom: 9px; } pre { @apply text-3xl } </style>
Argument of type "List[str]" cannot be
assigned to parameter "filename" of
type "StrPath" in function "write"
with zipfile.ZipFile(
    "results/All-WorkKnow-Results.zip",
    "w",
) as results_zip_file:
    for results_file in results_files:
        results_zip_file.write(results_files)

results_file


Type Annotations in Python

<style> h1 { @apply text-6xl -my-2 leading-20 font-bold text-dark-100 text-orange-600; } h2 { @apply text-4xl leading-20 font-bold text-dark-100; } code { font-size: 36px; } </style>

Terribly Intimidating or Tremendously Informative?

Programmers define types
Automatically create command-line
Type checkers automatically find bugs

Type Annotations in Python

<style> h1 { @apply text-6xl -my-2 leading-20 font-bold text-dark-100 text-orange-600; } h2 { @apply text-4xl leading-20 font-bold text-dark-100; } code { font-size: 36px; } </style>

Yes, they are Tremendously Informative! Try them!

AnalyzeActions/WorkKnow
gkapfham/pyohio2021-presentation