theme | class | highlighter | colorSchema | download | preload | fonts | info | title | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
./simple |
text-center |
prism |
light |
true |
true |
|
## Simple-slidev-sample
Simple Slidev Sample
|
Type Annotations in Python |
def start(t: Talk) -> List[Fun, Learn]:
<style> h2 { font-size: 36px; @apply text-orange-600 mb-4; } </style>
What are the benefits and challenges associated with using type annotations inside of Python program? Will types make me a better programmer?
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!
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?
<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?
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?
<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
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
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!
<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
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)
<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
<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>
Programmers define types
Automatically create command-line
Type checkers automatically find bugs
<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>
AnalyzeActions/WorkKnow
gkapfham/pyohio2021-presentation