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)

More Related Content

KEY
Mypy pycon-fi-2012
PPTX
Type hints in python & mypy
PDF
Writing Fast Code (JP) - PyCon JP 2015
ODP
Introduction to programming with python
PDF
C++0x :: Introduction to some amazing features
PPTX
Phython Programming Language
PPTX
Mixed-language Python/C++ debugging with Python Tools for Visual Studio- Pave...
PDF
The Benefits of Type Hints
Mypy pycon-fi-2012
Type hints in python & mypy
Writing Fast Code (JP) - PyCon JP 2015
Introduction to programming with python
C++0x :: Introduction to some amazing features
Phython Programming Language
Mixed-language Python/C++ debugging with Python Tools for Visual Studio- Pave...
The Benefits of Type Hints

What's hot (20)

PPT
Introduction to phython programming
PDF
Beginning Python
PDF
Python - the basics
PPTX
Introduction to python for Beginners
PPTX
Python - An Introduction
PPTX
Python Tutorial Part 2
PDF
Python indroduction
 
PDF
An Introduction to Python Programming
PDF
Python for All
PPTX
Python Programming
PPTX
GDG Helwan Introduction to python
KEY
Programming with Python: Week 1
PDF
Python Foundation – A programmer's introduction to Python concepts & style
PDF
Overview of python 2019
PPT
Introduction to Python
PPTX
Python Programming Language
PPTX
Python Tutorial for Beginner
PDF
introduction of python in data science
PPTX
Introduction to Python Basics Programming
PDF
Python final ppt
Introduction to phython programming
Beginning Python
Python - the basics
Introduction to python for Beginners
Python - An Introduction
Python Tutorial Part 2
Python indroduction
 
An Introduction to Python Programming
Python for All
Python Programming
GDG Helwan Introduction to python
Programming with Python: Week 1
Python Foundation – A programmer's introduction to Python concepts & style
Overview of python 2019
Introduction to Python
Python Programming Language
Python Tutorial for Beginner
introduction of python in data science
Introduction to Python Basics Programming
Python final ppt
Ad

Similar to 型ヒントについて考えよう! (20)

PDF
Python (3).pdf
PDF
Python Programming
PPTX
modul-python-part1.pptx
PPT
Python ppt
PPTX
bhaskars.pptx
PPTX
Basic concept of Python.pptx includes design tool, identifier, variables.
PPTX
Strong typing : adoption, adaptation and organisation
PDF
web programming UNIT VIII python by Bhavsingh Maloth
PPTX
python_class.pptx
PDF
Python basics_ part1
PPTX
INTRODUCTION TO PYTHON.pptx
PPTX
Python 01.pptx
PDF
prakash ppt (2).pdf
PPTX
Type script
PPTX
Fundamentals of Python Programming
PPTX
bhaskars.pptx
PDF
Strong typing @ php leeds
PPTX
Python unit 2 is added. Has python related programming content
PPTX
Learn Python The Hard Way Presentation
PPTX
Introduction to python
Python (3).pdf
Python Programming
modul-python-part1.pptx
Python ppt
bhaskars.pptx
Basic concept of Python.pptx includes design tool, identifier, variables.
Strong typing : adoption, adaptation and organisation
web programming UNIT VIII python by Bhavsingh Maloth
python_class.pptx
Python basics_ part1
INTRODUCTION TO PYTHON.pptx
Python 01.pptx
prakash ppt (2).pdf
Type script
Fundamentals of Python Programming
bhaskars.pptx
Strong typing @ php leeds
Python unit 2 is added. Has python related programming content
Learn Python The Hard Way Presentation
Introduction to python
Ad

More from Yusuke Miyazaki (12)

PDF
Dynamic Type Inference for Gradual Hindley–Milner Typing
PDF
Python と Docker で mypy Playground を開発した話
PDF
Introducing wsgi_lineprof / PyCon JP 2017 LT
PDF
オープンソースソフトウェア入門
PDF
iot.ymyzk.com の紹介
PDF
Django から各種チャットツールに通知するライブラリを作った話
PDF
iOS 開発のいま (ADF2015 LT会)
PDF
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
PDF
Swift の問題点
PDF
最新の iOS に対応したアプリの開発
PDF
コンピューターネットワーク入門
PDF
HTML初心者講座
Dynamic Type Inference for Gradual Hindley–Milner Typing
Python と Docker で mypy Playground を開発した話
Introducing wsgi_lineprof / PyCon JP 2017 LT
オープンソースソフトウェア入門
iot.ymyzk.com の紹介
Django から各種チャットツールに通知するライブラリを作った話
iOS 開発のいま (ADF2015 LT会)
iOS 開発のいま (CAMPHOR- x KMC 合同LT会)
Swift の問題点
最新の iOS に対応したアプリの開発
コンピューターネットワーク入門
HTML初心者講座

Recently uploaded (20)

PPTX
Foundations of Marketo Engage: Nurturing
PDF
Engineering Document Management System (EDMS)
PPTX
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
PDF
Lumion Pro Crack New latest version Download 2025
PPTX
Human-Computer Interaction for Lecture 2
PPTX
Human-Computer Interaction for Lecture 1
PPTX
Why 2025 Is the Best Year to Hire Software Developers in India
PDF
IT Consulting Services to Secure Future Growth
PPTX
ERP Manufacturing Modules & Consulting Solutions : Contetra Pvt Ltd
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PPTX
ROI Analysis for Newspaper Industry with Odoo ERP
PDF
Crypto Loss And Recovery Guide By Expert Recovery Agency.
PPTX
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
PPTX
Chapter_05_System Modeling for software engineering
PDF
MAGIX Sound Forge Pro CrackSerial Key Keygen
DOCX
Industrial Bio-Lynx: Advanced Biometric Solution for Workforce Management
PPT
3.Software Design for software engineering
PDF
Cloud Native Aachen Meetup - Aug 21, 2025
PPTX
Chapter 1 - Transaction Processing and Mgt.pptx
PPTX
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...
Foundations of Marketo Engage: Nurturing
Engineering Document Management System (EDMS)
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
Lumion Pro Crack New latest version Download 2025
Human-Computer Interaction for Lecture 2
Human-Computer Interaction for Lecture 1
Why 2025 Is the Best Year to Hire Software Developers in India
IT Consulting Services to Secure Future Growth
ERP Manufacturing Modules & Consulting Solutions : Contetra Pvt Ltd
Understanding the Need for Systemic Change in Open Source Through Intersectio...
ROI Analysis for Newspaper Industry with Odoo ERP
Crypto Loss And Recovery Guide By Expert Recovery Agency.
Plex Media Server 1.28.2.6151 With Crac5 2022 Free .
Chapter_05_System Modeling for software engineering
MAGIX Sound Forge Pro CrackSerial Key Keygen
Industrial Bio-Lynx: Advanced Biometric Solution for Workforce Management
3.Software Design for software engineering
Cloud Native Aachen Meetup - Aug 21, 2025
Chapter 1 - Transaction Processing and Mgt.pptx
string python Python Strings: Literals, Slicing, Methods, Formatting, and Pra...

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

  • 1. 
 Let’s Think about Type Hints! Yusuke Miyazaki @ymyzk 
 PyCon JP 2016 2016/9/22 Room 203
  • 3. 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!
  • 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 Swift C Java Objective-C OCaml Scheme JavaScript Ruby PHP
  • 7. What is Type Hints?
  • 9. 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
  • 10. 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
  • 11. Goal of the Session Get to know type hints
  • 12. Goal of the Session Get to know type hints Use type hints in your projects!
  • 13. Agenda • Background • Introduction to type hints • Tools • Comparison with other language • Summary
  • 16. Type Checking • Python is 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
  • 19. 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…
  • 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)
  • 22. 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…
  • 23. Sphinx def greeting(name): """Generate a greeting :param name: name of someone :type name: str :return: generated message :rtype: str """ return "Hello, {}!".format(name)
  • 26. 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 ( )
  • 28. 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
  • 29. How to Write Type Hints? def greeting(name: str) -> str:
 return "Hello, {}!".format(name)
  • 30. How to Write Type Hints? def greeting(name: str) -> str:
 return "Hello, {}!".format(name) Use function annotation
  • 31. 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
  • 32. 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
  • 33. How to Write Type Hints? # Before Python 3.6
 YEAR = 2016 # type: int
 
 

  • 34. How to Write Type Hints? # Before Python 3.6
 YEAR = 2016 # type: int
 
 
 Type comments in PEP 484
  • 35. 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
  • 36. 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: ...
  • 37. 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…
  • 38. 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
  • 39. 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)
  • 40. Tools
  • 41. mypy
  • 42. 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
  • 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"
  • 46. 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
  • 48. PyCharm • IDE for Python • Type checking / Code completion • References: • Python 3.5 type hinting in PyCharm 5 • Type Hinting in PyCharm
  • 52. Sphinx • Documentation tool written in Python • Supports type annotations in docstrings • Supports type hints too!
  • 53. Example from typing import Iterable
 
 def greeting_all(names: Iterable[str]) -> str:
 return "Hello, {}!".format(", ".join(names))
  • 54. Example from typing import Iterable
 
 def greeting_all(names: Iterable[str]) -> str:
 return "Hello, {}!".format(", ".join(names))
  • 57. JavaScript + Types There are a lot of projects… • TypeScript • Flow • Closure Compiler • JSDoc
  • 58. 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
  • 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 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
  • 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. PHP
  • 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 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
  • 72. 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
  • 73. 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
  • 74. Cons of Type Hints • Not written by many developers • Few stub files for third party libraries • Not performance boosting • Not advanced type system
  • 75. 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!
  • 76. 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)