Python type hints allow annotating function and variable types in Python code. They were introduced in PEP 484 for Python 3.5 and provide documentation of types without runtime enforcement. Using type hints and tools like mypy enables type checking of code without execution, improving code quality and readability. They help with code completion, refactoring Python 2 code to Python 3, and catching type errors during development.
3. Who are you( )?
• (masahito)
• Typetalk Software Developer
• use Scala and JavaScript and Python
• Python ❤ and
☕
❤
Python - PyCon Kyushu in Okinawa 2019 May 18th 3
4. Masato Nakamura(masahito)
• Speaker
• Python Conference Hong Kong 2018
• Python Conference Kyushu 2018 in Japan
• Python Conference Taiwan 2017
Python - PyCon Kyushu in Okinawa 2019 May 18th 4
8. Today's Outline
Ⅰ. Introduction of Type Hints
Ⅱ. Usage of Type Hints
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 8
9. Today's Outline
!
Ⅰ. Introduction of Type Hints
Ⅱ. Usage of Type Hints
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 9
10. Introduction of Type Hints
• A. What is Type Hints function
• B. History of Type Hints
• C. typing module
Python - PyCon Kyushu in Okinawa 2019 May 18th 10
11. What is Type Hints function
We can write more Readable Codes.
We can annotate our codes only.
Python - PyCon Kyushu in Okinawa 2019 May 18th 11
12. What is Type Hints function
• Traditional Python code
def twice(num):
return num * 2
• using Type Hints
def twice(num: int) -> int:
return num * 2
Python - PyCon Kyushu in Okinawa 2019 May 18th 12
13. What is Type Hints function
also we can use Class!
class Car:
def __init__(self) -> None:
self.seats = 4
c: Car = Car()
Python - PyCon Kyushu in Okinawa 2019 May 18th 13
14. What is Type Hints function
• using Type Hints
def twice(num: int) -> int:
return num * 2
• C-language Code
int twice(int num) {
return num * 2;
}
Python - PyCon Kyushu in Okinawa 2019 May 18th 14
15. Difference of Type Hints and C code
• If you set wrong Type,We cannot show runtime error
def twice(num: int) -> int:
return num * 2.0
# expected `int` but use `str`
!
print(twice("hello"))
$ python set_wrong_type.py
$ hellohello ##
!
Python - PyCon Kyushu in Okinawa 2019 May 18th 15
16. hmm, What is TypeHints ???
Python - PyCon Kyushu in Okinawa 2019 May 18th 16
17. We can annotate our codes only.
Python - PyCon Kyushu in Okinawa 2019 May 18th 17
18. But You use library, e.g. mypy
$ mypy --python-version 3.7 example.py
example.py:5: error: Argument 1 to "twice" has incompatible type
"str"; expected "int"
Python - PyCon Kyushu in Okinawa 2019 May 18th 18
19. What is good point for checking type ???
Python - PyCon Kyushu in Okinawa 2019 May 18th 19
20. We can check our code without run codes.
• so we can re-write our Python2 code to Python3 more easily
• We can do it !!
Python - PyCon Kyushu in Okinawa 2019 May 18th 20
21. History of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 21
22. History of Type Hints
implement PEP
Python 3.0 3107
Python 3.5 484
Ptyhon 3.6 526
Python 3.7 560, 561, 562,563
Python - PyCon Kyushu in Okinawa 2019 May 18th 22
23. Traditional Python code(Python2)
def greeting(name):
return 'Hello ' + name
greet = greeting("Guido")
• What is name
!
?
• What does greeting return
!
?
Python - PyCon Kyushu in Okinawa 2019 May 18th 23
24. PEP 3107(Python 3.0)
def compile(source: "something compilable",
filename: "where the compilable thing comes from",
mode: "is this a single statement or a suite?"):
Python - PyCon Kyushu in Okinawa 2019 May 18th 24
25. PEP 484(Python 3.5)
def greeting(name: str) -> str:
return 'Hello ' + name
• PEP-484 Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 25
26. Points in PEP484
Python dynamically typed language
•
•
•
Python - PyCon Kyushu in Okinawa 2019 May 18th 26
27. inconvinient of PEP484
def greeting(name: str) -> str:
return 'Hello ' + name
greet = greeting("Masato") # type: str # make greeting message!
Hmm ,
Python - PyCon Kyushu in Okinawa 2019 May 18th 27
28. PEP 526(Python 3.6)
# PEP 484
def greeting(name: str) -> str:
return 'Hello ' + name
greet = greeting("Masato") # type: str # make greeting message!
# pep 526
def greeting(name: str) -> str:
return 'Hello ' + name
greet:str = greeting("Masato") # make greeting message!
Python - PyCon Kyushu in Okinawa 2019 May 18th 28
30. What is the typing module
• PEP 484 3.5
• List,Dict,Union Python
• namedtuple
from typing import List
a: List[int] = [1,2,3]
path: Optional[str] = None # Path to module sourde
Python - PyCon Kyushu in Okinawa 2019 May 18th 30
31. • Union
i = 1 # Infer type "int" for i
l = [1, 2] # Infer type "List[int]" for l
from typing import Union
x: Union[int, str] = 1
x: Union[int, str] = 1.1 # Error
Python - PyCon Kyushu in Okinawa 2019 May 18th 31
32. • NamedTuple
In Python 3.5(PEP 484)
Point = namedtuple('Point', ['x', 'y'])
p = Point(x=1, y=2)
In Python 3.6(PEP 526)
from typing import NamedTuple
class Point(NamedTuple):
x: int
y: int
p = Point(x=1, y=2)
Python - PyCon Kyushu in Okinawa 2019 May 18th 32
33. Today's Outline
Ⅰ. Introduction of Type Hints
!
Ⅱ. Usage of Type Hints
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 33
34. Usage Of Type Hints
• A. How to write
• B. How to get Type Hints Info
Python - PyCon Kyushu in Okinawa 2019 May 18th 34
35. How to write
PEP526 style
def greeting(name: str) -> str:
return 'Hello ' + name
greet: str = greeting("Masato")
Python - PyCon Kyushu in Okinawa 2019 May 18th 35
36. difference of PEP484 and PEP526
# pep 484
child # type: bool # cannot allow
if age < 18:
child = True # type: bool # It's OK
else:
child = False # type: bool # It's OK
# pep 526
child: bool # OK
if age < 18:
child = True # No need to Write bool
else:
child = False
Python - PyCon Kyushu in Okinawa 2019 May 18th 36
37. backward compatibility
# We can write pep484 style code in 3.6 backward compatiblity
hour = 24 # type: int
# PEP 526 style
hour: int; hour = 24
hour: int = 24
Python - PyCon Kyushu in Okinawa 2019 May 18th 37
38. recommend using Type Hints
PEP3107 style is also OK
>>> alice: 'well done' = 'A+'
>>> bob: 'what a shame' = 'F-'
>>> __annotations__
{'alice': 'well done', 'bob': 'what a shame'}
Type Hints
.
Python - PyCon Kyushu in Okinawa 2019 May 18th 38
39. How to get Type Hints Info
Q. where is variable annotation
A. at __annotaitons__
Python - PyCon Kyushu in Okinawa 2019 May 18th 39
40. How to get Type Hints Info
example
>>> answer:int = 42
>>> __annotations__
{'answer': <class 'int'>}
Python - PyCon Kyushu in Okinawa 2019 May 18th 40
41. Note for Class
We can only find Class variables.
>>> class Car:
... stats: ClassVar[Dict[str, int]] = {}
... def __init__(self) -> None:
... self.seats = 4
>>> c: Car = Car()
>>> c.__annotations__
{'stats': typing.ClassVar[typing.Dict[str, int]]}
# only ClassVar!
We cannot get instance's variables!
Python - PyCon Kyushu in Okinawa 2019 May 18th 41
42. Today's Outline
Ⅰ. Introduction of Type Hints
Ⅱ. Usage of Type Hints
!
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 42
43. Benefits of Type Hints
A. Code Style
B. code completion
C. statistic type analysis
D. Our story
Python - PyCon Kyushu in Okinawa 2019 May 18th 43
44. Code Style
• Simple is best
• Explicit is better than Implicit.
def greeting(name: str) -> str:
return 'Hello ' + name
We can check this by Type Hints
def greeting(name: str) -> str:
return 42
Python - PyCon Kyushu in Okinawa 2019 May 18th 44
45. Code Completion
We can use code completion in
• Editor
• Visual Studio code
• IDE
• PyCharm(IntelliJ)
Python - PyCon Kyushu in Okinawa 2019 May 18th 45
51. How to install mypy
$ pip install mypy
Notice requires Python 3.5 or later to run.
Python - PyCon Kyushu in Okinawa 2019 May 18th 51
52. mypy example
• code(example.py)
from typing import NamedTuple
class Employee(NamedTuple):
name: str
id: int
emp: Employee = Employee(name='Guido', id='x')
• run
$ mypy --python-version 3.7 example.py
example.py:16: error: Argument 2 to "Employee" has incompatible type
"str"; expected "int"
Python - PyCon Kyushu in Okinawa 2019 May 18th 52
53. mypy can use in Python2
class MyClass(object):
# For instance methods, omit `self`.
def my_method(self, num, str1):
# type: (int, str) -> str
return num * str1
def __init__(self):
# type: () -> None
pass
x = MyClass() # type: MyClass
Python - PyCon Kyushu in Okinawa 2019 May 18th 53
54. Missing imports
import type annotation
!
main.py:1: error: No library stub file for standard library module 'antigravity'
main.py:2: error: No library stub file for module 'flask'
main.py:3: error: Cannot find module named 'this_module_does_not_exist'
( )
mypy --ignore-missing-imports
Python - PyCon Kyushu in Okinawa 2019 May 18th 54
56. Continuous Integration
• we can run mypy on CI systems
• we can get results
• If you develop in a team, there are many benefits
• ex: If someone issues a pull request for your project, you can
review their code
Python - PyCon Kyushu in Okinawa 2019 May 18th 56
60. Benefits for us(Nulab)
• We use Fabric v1 and Ansible for deployment
• But it is for Only Python2
• Python2 will finished at 2020(Tokyo Olympic Year)
• We thought we should convert Python2 to Python3
Python - PyCon Kyushu in Okinawa 2019 May 18th 60
61. Recently, that date has been updated to January 1, 2020.
Python - PyCon Kyushu in Okinawa 2019 May 18th 61
62. We want to use Python3
• We use Fabric3
• It's Fabric for Python2.7 and Python3
• https://pypi.python.org/pypi/Fabric3
• We try to check this, That's good for run
Python - PyCon Kyushu in Okinawa 2019 May 18th 62
63. We(Nulab) use Python3 & Fabric3 & mypy
• We use TypeHints for rewriting our code
• We got it! -> Only 2days
• We deploy the new version of Out App every week.
• beta version every day!
Python - PyCon Kyushu in Okinawa 2019 May 18th 63
64. Python2 to 3
• Python2.7
• six ( Python2 3 )
• 2to3
Python - PyCon Kyushu in Okinawa 2019 May 18th 64
66. def is_modified(path, backup='bak'):
# type: (Text, str) -> bool
"""
path
"""
with settings(hide('warnings'), warn_only=True):
result = sudo('diff -u %s.%s %s' % (path, backup, path))
return result.return_code == 1
Python - PyCon Kyushu in Okinawa 2019 May 18th 66
67. Those are the Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 67
68. Today's Outline
✅
1. Introduction of Type Hints
✅
2. Usage of Type Hints
✅
3. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 68
71. • type-hints pep
• PEP 586 -- Literal Types
• PEP 591 -- Adding a final qualifier to typing
• mypy (plugin)
• typing module
Python - PyCon Kyushu in Okinawa 2019 May 18th 71
72. PEP 586 -- Literal Types
from typing import Literal
def accepts_only_four(x: Literal[4]) -> None:
pass
accepts_only_four(4) # OK
accepts_only_four(19) # Rejected
Python - PyCon Kyushu in Okinawa 2019 May 18th 72
73. PEP 591 -- Adding a final qualifier to typing
from typing import Final
RATE: Final = 3000
class Base:
DEFAULT_ID: Final = 0
RATE = 300 # Error: can't assign to final attribute
Base.DEFAULT_ID = 1 # Error: can't override a final attribute
Python - PyCon Kyushu in Okinawa 2019 May 18th 73