Let’s Think about Type Hints!
Yusuke Miyazaki @ymyzk 

PyCon JP 2016
2016/9/22 Room 203
Introduction
Introduction
• / Yusuke Miyazaki / @ymyzk
• Master’s course student of Kyoto University
• Programming Language / Type System
• Co-founder Unimap, Inc. / KyodaiMap
• Member of Student Community CAMPHOR-
• Visit ymyzk.com for more details!
Python and Me
• Using for about 5 years
• Web applications (Django, Flask, Bottle…), tools, etc…
• Contributing to OSS
• Libraries (django-channels, python-gyazo, etc…)
• Translation (Python, Django)
• Local staff of Python Boot Camp in Kyoto
• Attendee of PyCon APAC 2013, PyCon JP 2014-2015
Types and Me
Python
Swift C
Java
Objective-C
OCaml
Scheme
JavaScript
Ruby
PHP
Types and Me
Python
Swift
C
Java
Objective-C
Statically

Typed
Dynamically
Typed
OCaml
Scheme
JavaScript
Ruby
PHP
What is Type Hints?
Standardized

Type Annotations

for Python
Example without Type Hints
def add(x, y):

return x + y
def greeting(name):
return "Hello, {}!".format(name)
def make_pair(e):

return e, e
Example with Type Hints
def add(x: int, y: int) -> int:

return x + y
def greeting(name: str) -> str:
return "Hello, {}!".format(name)
from typing import Tuple, TypeVar
T = TypeVar("T")

def make_pair(e: T) -> Tuple[T, T]:

return e, e
Goal of the Session
Get to know type hints
Goal of the Session
Get to know type hints
Use type hints in your projects!
Agenda
• Background
• Introduction to type hints
• Tools
• Comparison with other language
• Summary
Background
Type Checking
Type Checking
• Python is a dynamically typed language
• Fast development / prototyping
• We want to avoid runtime type errors
• Big projects
• Scientific computing / machine learning
Existing Projects
• mypy ― Optional static type checker
• PyCharm ― IDE: Use type information for
completion and error detection
• Reticulated Python ― Static type checker &
runtime type checking
Function Annotations
Function Annotations
• Introduced in Python 3.0 (PEP 3107)
• Annotate function’s parameters and return
values with expressions
• Runtime access via __annotations__
• Expected use cases in PEP:

Type information, documentation, etc…
Examples
# Documentation

def compile(source: "source code",

file: "filename") -> "result":

pass
# Type information

def compile(source: str, file: str) -> str:

pass
Function annotation (parameter)
Function annotation (return value)
Documentation
Documentation
• Type information as a part of documentation
• Good practice for libraries or big projects
• Write parameter types or return type in
docstrings or comments
• Existing projects: Sphinx, Doxygen, etc…
Sphinx
def greeting(name):
"""Generate a greeting
:param name: name of someone
:type name: str
:return: generated message
:rtype: str
"""
return "Hello, {}!".format(name)
Sphinx
Gradual Typing
Gradual Typing
• Type system proposed by Siek and Taha (2006)
• Statically typed parts & dynamically typed
parts in a single program
• References:
• PEP 483 ― The Theory of Type Hints
• What is Gradual Typing ( )
Type Hints
Type Hints
Standardized type annotation for Python
• Introduced in Python 3.5 (2015)
• Specification: PEP 484 ― Type Hints ( )
• Use function annotation to annotate types
• Use typing module for expressing types
How to Write Type Hints?
def greeting(name: str) -> str:

return "Hello, {}!".format(name)
How to Write Type Hints?
def greeting(name: str) -> str:

return "Hello, {}!".format(name)
Use function annotation
How to Write Type Hints?
def greeting(name: str) -> str:

return "Hello, {}!".format(name)






from typing import List

Use function annotation
Import "List" to express type hints
How to Write Type Hints?
def greeting(name: str) -> str:

return "Hello, {}!".format(name)






from typing import List

def count_zero(elements: List[int]) -> int:

return sum(1 for e in elements if e == 0)
Use function annotation
Import "List" to express type hints
How to Write Type Hints?
# Before Python 3.6

YEAR = 2016 # type: int





How to Write Type Hints?
# Before Python 3.6

YEAR = 2016 # type: int





Type comments in PEP 484
How to Write Type Hints?
# Before Python 3.6

YEAR = 2016 # type: int





# NEW in Python 3.6

YEAR: int = 2016
Type comments in PEP 484
Variable annotations in PEP 526
Another Way: Stub Files
# greet.py
def greeting(name):
return "Hello, {}!".format(name)
# greet.pyi
# A stub file for greet.py
def greeting(name: str) -> str: ...
Types Defined in PEP 484
• Callable

(e.g., Callable[[int, int], float])
• TypeVar (e.g., T = TypeVar("T"))
• Generics (e.g., Sequence[T])
• Any, Optional, Union, Iterable, etc…
Features of Type Hints
• Use the same type hints in various tools
• Type checker, editors, documentation, etc…
• Not required (use of type hints is optional)
• CPython ignores type hints at runtime
• Not performance boosting (at least in CPython)
• Backwards compatible
Backwards Compatibility
• typing module is just a single Python file!
• A backport is available for Python 2.7, 3.x:

$ pip install typing
• Syntax for Python 2.7 is suggested in PEP:
def greeting(name):
# type: (str) -> str
return "Hello, {}!".format(name)
Tools
mypy
mypy
• Optional static type checker for Python
• PEP 484 is strongly inspired by mypy
• Still in development
• You can use it as a lint tool like pylint / flake8
Example
def greeting(name: str) -> str:
return "Hello, {}!".format(name)
greeting("OK")
greeting(1234)
Example
def greeting(name: str) -> str:
return "Hello, {}!".format(name)
greeting("OK")
greeting(1234)
error: Argument 1 to "greeting" has
incompatible type "int"; expected "str"
typeshed
typeshed
• Repository of stub files for:
• Python builtins / standard libraries
• Third-party libraries
• A few third-party libraries are supported
• Let’s make PRs: github.com/python/typeshed
PyCharm
PyCharm
• IDE for Python
• Type checking / Code completion
• References:
• Python 3.5 type hinting in PyCharm 5
• Type Hinting in PyCharm
Example 1
Type checking
Example 2
Code completion
Sphinx
Sphinx
• Documentation tool written in Python
• Supports type annotations in docstrings
• Supports type hints too!
Example
from typing import Iterable



def greeting_all(names: Iterable[str]) -> str:

return "Hello, {}!".format(", ".join(names))
Example
from typing import Iterable



def greeting_all(names: Iterable[str]) -> str:

return "Hello, {}!".format(", ".join(names))
Comparison

with Other Languages
JavaScript
JavaScript + Types
There are a lot of projects…
• TypeScript
• Flow
• Closure Compiler
• JSDoc
TypeScript
• Superset of JavaScript by Microsoft
• Static type checker + compiler to JavaScript
• DefinitelyTyped (corresponds to typeshed)
• Type definitions for more than 1,000 libraries
• Structural type system
Example
// greet.ts
const greeting = (name: string) => "Hi, " + name;
greeting("Taro");
greeting(10);
Example
// greet.ts
const greeting = (name: string) => "Hi, " + name;
greeting("Taro");
greeting(10);
// greet.js
var greeting =
function (name) { return "Hi, " + name; };
greeting("Taro");
greeting(10);
Example
// greet.ts
const greeting = (name: string) => "Hi, " + name;
greeting("Taro");
greeting(10);
// greet.js
var greeting =
function (name) { return "Hi, " + name; };
greeting("Taro");
greeting(10);
error TS2345: Argument of type
'number' is not assignable to
parameter of type 'string'.
Flow
• Static type checker for JavaScript
• Can be used without type annotations by
using type inference
• You can write type annotations
• Strip type annotations to run programs
Example
// @flow
function times10(n) {
return n * 10;
}
times10(100);
times10("Hello");
Example
// @flow
function times10(n) {
return n * 10;
}
times10(100);
times10("Hello");
7: times10("Hello");
^^^^^^^^^^^^^^^^ function call
3: return n * 10;
^ string. This type is incompatible with
3: return n * 10;
^^^^^^ number
PHP
Hack
• PHP + various features (types, collections,
lambdas…)
• Static typechecker
• Runs on HHVM
• Use type information at runtime & speed
up!
Example
<?php
function greeting($name) {
return "Hello, " . $name;
}
function main() {
greeting("Taro");
greeting(12345);
}
main();
Example
<?hh
function greeting(string $name): string {
return "Hello, " . $name;
}
function main(): void {
greeting("Taro");
greeting(12345);
}
main();
Example
<?hh
function greeting(string $name): string {
return "Hello, " . $name;
}
function main(): void {
greeting("Taro");
greeting(12345);
}
main();greet.hh:7:14,18: Invalid argument (Typing[4110])
greet.hh:2:19,24: This is a string
greet.hh:7:14,18: It is incompatible with an int
Summary of Other Languages
• There are lots of projects related to type annotations
• TypeScript is widely used & has a lot of users
• No standardized type annotations
• Rewrite type annotations to use different tools
• Not backwards compatible
• Requires new compiler / interpreter
Summary
Summary of Type Hints
• Standardized type annotation in Python
• Introduced in Python 3.5
• Use function annotation to write types
• Use typing module for expressing types
Pros of Type Hints
• Type annotation is standardized!
• Use the same annotation with various
tools: mypy, PyCharm, Sphinx, etc …
• Backwards compatible (Python 2 & 3)
• No compilation or translation is required
Cons of Type Hints
• Not written by many developers
• Few stub files for third party libraries
• Not performance boosting
• Not advanced type system
Let’s Use It Now!
If you are interested in type hints:
• Create tools which use type hints
• Write type hints in your project
• Write stub files for existing projects
If you are not interested in type hints:
• Tools like PyCharm will help you!
Thank You for Listening!!
• Feel free to ask me questions
• This slide will be uploaded later (see Twitter)
• Development sprint about type hints

on Saturday (9/24)

型ヒントについて考えよう!

  • 1.
    
 Let’s Think aboutType Hints! Yusuke Miyazaki @ymyzk 
 PyCon JP 2016 2016/9/22 Room 203
  • 2.
  • 3.
    Introduction • / YusukeMiyazaki / @ymyzk • Master’s course student of Kyoto University • Programming Language / Type System • Co-founder Unimap, Inc. / KyodaiMap • Member of Student Community CAMPHOR- • Visit ymyzk.com for more details!
  • 4.
    Python and Me •Using for about 5 years • Web applications (Django, Flask, Bottle…), tools, etc… • Contributing to OSS • Libraries (django-channels, python-gyazo, etc…) • Translation (Python, Django) • Local staff of Python Boot Camp in Kyoto • Attendee of PyCon APAC 2013, PyCon JP 2014-2015
  • 5.
    Types and Me Python SwiftC Java Objective-C OCaml Scheme JavaScript Ruby PHP
  • 6.
  • 7.
  • 8.
  • 9.
    Example without TypeHints def add(x, y):
 return x + y def greeting(name): return "Hello, {}!".format(name) def make_pair(e):
 return e, e
  • 10.
    Example with TypeHints def add(x: int, y: int) -> int:
 return x + y def greeting(name: str) -> str: return "Hello, {}!".format(name) from typing import Tuple, TypeVar T = TypeVar("T")
 def make_pair(e: T) -> Tuple[T, T]:
 return e, e
  • 11.
    Goal of theSession Get to know type hints
  • 12.
    Goal of theSession Get to know type hints Use type hints in your projects!
  • 13.
    Agenda • Background • Introductionto type hints • Tools • Comparison with other language • Summary
  • 14.
  • 15.
  • 16.
    Type Checking • Pythonis a dynamically typed language • Fast development / prototyping • We want to avoid runtime type errors • Big projects • Scientific computing / machine learning
  • 17.
    Existing Projects • mypy― Optional static type checker • PyCharm ― IDE: Use type information for completion and error detection • Reticulated Python ― Static type checker & runtime type checking
  • 18.
  • 19.
    Function Annotations • Introducedin Python 3.0 (PEP 3107) • Annotate function’s parameters and return values with expressions • Runtime access via __annotations__ • Expected use cases in PEP:
 Type information, documentation, etc…
  • 20.
    Examples # Documentation
 def compile(source:"source code",
 file: "filename") -> "result":
 pass # Type information
 def compile(source: str, file: str) -> str:
 pass Function annotation (parameter) Function annotation (return value)
  • 21.
  • 22.
    Documentation • Type informationas a part of documentation • Good practice for libraries or big projects • Write parameter types or return type in docstrings or comments • Existing projects: Sphinx, Doxygen, etc…
  • 23.
    Sphinx def greeting(name): """Generate agreeting :param name: name of someone :type name: str :return: generated message :rtype: str """ return "Hello, {}!".format(name)
  • 24.
  • 25.
  • 26.
    Gradual Typing • Typesystem proposed by Siek and Taha (2006) • Statically typed parts & dynamically typed parts in a single program • References: • PEP 483 ― The Theory of Type Hints • What is Gradual Typing ( )
  • 27.
  • 28.
    Type Hints Standardized typeannotation for Python • Introduced in Python 3.5 (2015) • Specification: PEP 484 ― Type Hints ( ) • Use function annotation to annotate types • Use typing module for expressing types
  • 29.
    How to WriteType Hints? def greeting(name: str) -> str:
 return "Hello, {}!".format(name)
  • 30.
    How to WriteType Hints? def greeting(name: str) -> str:
 return "Hello, {}!".format(name) Use function annotation
  • 31.
    How to WriteType Hints? def greeting(name: str) -> str:
 return "Hello, {}!".format(name) 
 
 
 from typing import List
 Use function annotation Import "List" to express type hints
  • 32.
    How to WriteType Hints? def greeting(name: str) -> str:
 return "Hello, {}!".format(name) 
 
 
 from typing import List
 def count_zero(elements: List[int]) -> int:
 return sum(1 for e in elements if e == 0) Use function annotation Import "List" to express type hints
  • 33.
    How to WriteType Hints? # Before Python 3.6
 YEAR = 2016 # type: int
 
 

  • 34.
    How to WriteType Hints? # Before Python 3.6
 YEAR = 2016 # type: int
 
 
 Type comments in PEP 484
  • 35.
    How to WriteType Hints? # Before Python 3.6
 YEAR = 2016 # type: int
 
 
 # NEW in Python 3.6
 YEAR: int = 2016 Type comments in PEP 484 Variable annotations in PEP 526
  • 36.
    Another Way: StubFiles # greet.py def greeting(name): return "Hello, {}!".format(name) # greet.pyi # A stub file for greet.py def greeting(name: str) -> str: ...
  • 37.
    Types Defined inPEP 484 • Callable
 (e.g., Callable[[int, int], float]) • TypeVar (e.g., T = TypeVar("T")) • Generics (e.g., Sequence[T]) • Any, Optional, Union, Iterable, etc…
  • 38.
    Features of TypeHints • Use the same type hints in various tools • Type checker, editors, documentation, etc… • Not required (use of type hints is optional) • CPython ignores type hints at runtime • Not performance boosting (at least in CPython) • Backwards compatible
  • 39.
    Backwards Compatibility • typingmodule is just a single Python file! • A backport is available for Python 2.7, 3.x:
 $ pip install typing • Syntax for Python 2.7 is suggested in PEP: def greeting(name): # type: (str) -> str return "Hello, {}!".format(name)
  • 40.
  • 41.
  • 42.
    mypy • Optional statictype checker for Python • PEP 484 is strongly inspired by mypy • Still in development • You can use it as a lint tool like pylint / flake8
  • 43.
    Example def greeting(name: str)-> str: return "Hello, {}!".format(name) greeting("OK") greeting(1234)
  • 44.
    Example def greeting(name: str)-> str: return "Hello, {}!".format(name) greeting("OK") greeting(1234) error: Argument 1 to "greeting" has incompatible type "int"; expected "str"
  • 45.
  • 46.
    typeshed • Repository ofstub files for: • Python builtins / standard libraries • Third-party libraries • A few third-party libraries are supported • Let’s make PRs: github.com/python/typeshed
  • 47.
  • 48.
    PyCharm • IDE forPython • Type checking / Code completion • References: • Python 3.5 type hinting in PyCharm 5 • Type Hinting in PyCharm
  • 49.
  • 50.
  • 51.
  • 52.
    Sphinx • Documentation toolwritten in Python • Supports type annotations in docstrings • Supports type hints too!
  • 53.
    Example from typing importIterable
 
 def greeting_all(names: Iterable[str]) -> str:
 return "Hello, {}!".format(", ".join(names))
  • 54.
    Example from typing importIterable
 
 def greeting_all(names: Iterable[str]) -> str:
 return "Hello, {}!".format(", ".join(names))
  • 55.
  • 56.
  • 57.
    JavaScript + Types Thereare a lot of projects… • TypeScript • Flow • Closure Compiler • JSDoc
  • 58.
    TypeScript • Superset ofJavaScript by Microsoft • Static type checker + compiler to JavaScript • DefinitelyTyped (corresponds to typeshed) • Type definitions for more than 1,000 libraries • Structural type system
  • 59.
    Example // greet.ts const greeting= (name: string) => "Hi, " + name; greeting("Taro"); greeting(10);
  • 60.
    Example // greet.ts const greeting= (name: string) => "Hi, " + name; greeting("Taro"); greeting(10); // greet.js var greeting = function (name) { return "Hi, " + name; }; greeting("Taro"); greeting(10);
  • 61.
    Example // greet.ts const greeting= (name: string) => "Hi, " + name; greeting("Taro"); greeting(10); // greet.js var greeting = function (name) { return "Hi, " + name; }; greeting("Taro"); greeting(10); error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
  • 62.
    Flow • Static typechecker for JavaScript • Can be used without type annotations by using type inference • You can write type annotations • Strip type annotations to run programs
  • 63.
    Example // @flow function times10(n){ return n * 10; } times10(100); times10("Hello");
  • 64.
    Example // @flow function times10(n){ return n * 10; } times10(100); times10("Hello"); 7: times10("Hello"); ^^^^^^^^^^^^^^^^ function call 3: return n * 10; ^ string. This type is incompatible with 3: return n * 10; ^^^^^^ number
  • 65.
  • 66.
    Hack • PHP +various features (types, collections, lambdas…) • Static typechecker • Runs on HHVM • Use type information at runtime & speed up!
  • 67.
    Example <?php function greeting($name) { return"Hello, " . $name; } function main() { greeting("Taro"); greeting(12345); } main();
  • 68.
    Example <?hh function greeting(string $name):string { return "Hello, " . $name; } function main(): void { greeting("Taro"); greeting(12345); } main();
  • 69.
    Example <?hh function greeting(string $name):string { return "Hello, " . $name; } function main(): void { greeting("Taro"); greeting(12345); } main();greet.hh:7:14,18: Invalid argument (Typing[4110]) greet.hh:2:19,24: This is a string greet.hh:7:14,18: It is incompatible with an int
  • 70.
    Summary of OtherLanguages • There are lots of projects related to type annotations • TypeScript is widely used & has a lot of users • No standardized type annotations • Rewrite type annotations to use different tools • Not backwards compatible • Requires new compiler / interpreter
  • 71.
  • 72.
    Summary of TypeHints • Standardized type annotation in Python • Introduced in Python 3.5 • Use function annotation to write types • Use typing module for expressing types
  • 73.
    Pros of TypeHints • Type annotation is standardized! • Use the same annotation with various tools: mypy, PyCharm, Sphinx, etc … • Backwards compatible (Python 2 & 3) • No compilation or translation is required
  • 74.
    Cons of TypeHints • Not written by many developers • Few stub files for third party libraries • Not performance boosting • Not advanced type system
  • 75.
    Let’s Use ItNow! If you are interested in type hints: • Create tools which use type hints • Write type hints in your project • Write stub files for existing projects If you are not interested in type hints: • Tools like PyCharm will help you!
  • 76.
    Thank You forListening!! • Feel free to ask me questions • This slide will be uploaded later (see Twitter) • Development sprint about type hints
 on Saturday (9/24)