SlideShare a Scribd company logo
1 of 79
Download to read offline
Python
PyCon Kyushu in Okinawa 2019 May 18th
in Ryukyu University, Okinawa
Python - PyCon Kyushu in Okinawa 2019 May 18th 1
Python - PyCon Kyushu in Okinawa 2019 May 18th 2
Who are you( )?
• (masahito)
• Typetalk Software Developer
• use Scala and JavaScript and Python
• Python ❤ and
☕
❤
Python - PyCon Kyushu in Okinawa 2019 May 18th 3
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
Nulab provide collaboration softwares.
Python - PyCon Kyushu in Okinawa 2019 May 18th 5
Today's Goal
Type Hints ?
Type Hints
Python2 to 3
Python - PyCon Kyushu in Okinawa 2019 May 18th 6
Not covering today
?
Technical Detail
Python - PyCon Kyushu in Okinawa 2019 May 18th 7
Today's Outline
Ⅰ. Introduction of Type Hints
Ⅱ. Usage of Type Hints
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 8
Today's Outline
!
Ⅰ. Introduction of Type Hints
Ⅱ. Usage of Type Hints
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 9
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
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
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
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
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
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
hmm, What is TypeHints ???
Python - PyCon Kyushu in Okinawa 2019 May 18th 16
We can annotate our codes only.
Python - PyCon Kyushu in Okinawa 2019 May 18th 17
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
What is good point for checking type ???
Python - PyCon Kyushu in Okinawa 2019 May 18th 19
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
History of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 21
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
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
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
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
Points in PEP484
Python dynamically typed language
•
•
•
Python - PyCon Kyushu in Okinawa 2019 May 18th 26
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
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
typing module
Python - PyCon Kyushu in Okinawa 2019 May 18th 29
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
• 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
• 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
Today's Outline
Ⅰ. Introduction of Type Hints
!
Ⅱ. Usage of Type Hints
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 33
Usage Of Type Hints
• A. How to write
• B. How to get Type Hints Info
Python - PyCon Kyushu in Okinawa 2019 May 18th 34
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
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
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
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
How to get Type Hints Info
Q. where is variable annotation
A. at __annotaitons__
Python - PyCon Kyushu in Okinawa 2019 May 18th 39
How to get Type Hints Info
example
>>> answer:int = 42
>>> __annotations__
{'answer': <class 'int'>}
Python - PyCon Kyushu in Okinawa 2019 May 18th 40
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
Today's Outline
Ⅰ. Introduction of Type Hints
Ⅱ. Usage of Type Hints
!
Ⅲ. Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 42
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
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
Code Completion
We can use code completion in
• Editor
• Visual Studio code
• IDE
• PyCharm(IntelliJ)
Python - PyCon Kyushu in Okinawa 2019 May 18th 45
example
Python - PyCon Kyushu in Okinawa 2019 May 18th 46
statistic type analysis
Python - PyCon Kyushu in Okinawa 2019 May 18th 47
statistic type analysis
• for check variable type(no runable code)
• python has some tools
• mypy
• pytypes (not talk)
Python - PyCon Kyushu in Okinawa 2019 May 18th 48
example
Python - PyCon Kyushu in Okinawa 2019 May 18th 49
mypy
• https://github.com/python/mypy
• made by JukkaL & Guido
• can output report
• We can use!(PEP526 style)
Python - PyCon Kyushu in Okinawa 2019 May 18th 50
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
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
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
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
Continuous Integration
• Jenkins
• Travis CI
• CircleCi
Python - PyCon Kyushu in Okinawa 2019 May 18th 55
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
sample for CI
Sample
Python - PyCon Kyushu in Okinawa 2019 May 18th 57
example(error)
Python - PyCon Kyushu in Okinawa 2019 May 18th 58
example(success)
Python - PyCon Kyushu in Okinawa 2019 May 18th 59
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
Recently, that date has been updated to January 1, 2020.
Python - PyCon Kyushu in Okinawa 2019 May 18th 61
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
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
Python2 to 3
• Python2.7
• six ( Python2 3 )
• 2to3
Python - PyCon Kyushu in Okinawa 2019 May 18th 64
Python2 to 3
•
•
•
• ->
•
Python - PyCon Kyushu in Okinawa 2019 May 18th 65
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
Those are the Benefits of Type Hints
Python - PyCon Kyushu in Okinawa 2019 May 18th 67
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
?
Python - PyCon Kyushu in Okinawa 2019 May 18th 69
Thank you
@masahito
Python - PyCon Kyushu in Okinawa 2019 May 18th 70
• 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
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
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
mypy plugin
• mypy
• plugin mypy plugin
• sqlAlchemy etc
plugin
Python - PyCon Kyushu in Okinawa 2019 May 18th 74
mypy plugin
• dropbox SqlAlchemy mypy-stub
• https://github.com/python/mypy/issues/3538
• https://github.com/dropbox/sqlalchemy-stubs
Python - PyCon Kyushu in Okinawa 2019 May 18th 75
mypy PEP561
• PEP 561 is
•
• numpy stub
• https://github.com/numpy/numpy-stubs
Python - PyCon Kyushu in Okinawa 2019 May 18th 76
typing extentions
• typing
https://github.com/python/typing/tree/master/
typing_extensions
• Protocol
Python - PyCon Kyushu in Okinawa 2019 May 18th 77
User Defined Protocol
from typing import Iterable
from typing_extensions import Protocol
class SupportsClose(Protocol):
def close(self) -> None:
... # Empty method body (explicit '...')
class Resource: # No SupportsClose base class!
# ... some methods ...
def close(self) -> None:
self.resource.release()
def close_all(items: Iterable[SupportsClose]) -> None:
for item in items:
item.close()
close_all([Resource(), open('some/file')]) # Okay!
DuckTyping
Python - PyCon Kyushu in Okinawa 2019 May 18th 78
Thanks again!
Python - PyCon Kyushu in Okinawa 2019 May 18th 79

More Related Content

Similar to Python Type Hints Guide for PyCon Kyushu 2019

High scalable applications with Python
High scalable applications with PythonHigh scalable applications with Python
High scalable applications with PythonGiuseppe Broccolo
 
IT talk "Python language evolution"
IT talk "Python language evolution"IT talk "Python language evolution"
IT talk "Python language evolution"DataArt
 
Andrii Soldatenko "Competitive programming using Python"
Andrii Soldatenko "Competitive programming using Python"Andrii Soldatenko "Competitive programming using Python"
Andrii Soldatenko "Competitive programming using Python"OdessaPyConference
 
Python ppt.pdf
Python ppt.pdfPython ppt.pdf
Python ppt.pdfkalai75
 
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...Piotr Przymus
 
Pycon tw 2013
Pycon tw 2013Pycon tw 2013
Pycon tw 2013show you
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonkannikadg
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019Samir Mohanty
 
Mastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment NeedsMastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment NeedsCoding Assignment Help
 
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)HemaArora2
 
pycon-2015-liza-daly
pycon-2015-liza-dalypycon-2015-liza-daly
pycon-2015-liza-dalyLiza Daly
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfoptimusnotch44
 
型ヒントについて考えよう!
型ヒントについて考えよう!型ヒントについて考えよう!
型ヒントについて考えよう!Yusuke Miyazaki
 
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...Andreu Vallbona Plazas
 

Similar to Python Type Hints Guide for PyCon Kyushu 2019 (20)

High scalable applications with Python
High scalable applications with PythonHigh scalable applications with Python
High scalable applications with Python
 
Brief Introduction to Cython
Brief Introduction to CythonBrief Introduction to Cython
Brief Introduction to Cython
 
IT talk "Python language evolution"
IT talk "Python language evolution"IT talk "Python language evolution"
IT talk "Python language evolution"
 
Andrii Soldatenko "Competitive programming using Python"
Andrii Soldatenko "Competitive programming using Python"Andrii Soldatenko "Competitive programming using Python"
Andrii Soldatenko "Competitive programming using Python"
 
Python ppt.pdf
Python ppt.pdfPython ppt.pdf
Python ppt.pdf
 
Python fundamentals
Python fundamentalsPython fundamentals
Python fundamentals
 
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
Everything You Always Wanted to Know About Memory in Python But Were Afraid t...
 
Pycon tw 2013
Pycon tw 2013Pycon tw 2013
Pycon tw 2013
 
FDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on pythonFDP-faculty deveopmemt program on python
FDP-faculty deveopmemt program on python
 
PEP 498: The Monologue
PEP 498: The MonologuePEP 498: The Monologue
PEP 498: The Monologue
 
Overview of python 2019
Overview of python 2019Overview of python 2019
Overview of python 2019
 
Mastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment NeedsMastering Coding Assignments: Expert Help for Your Coding Assignment Needs
Mastering Coding Assignments: Expert Help for Your Coding Assignment Needs
 
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)PYTHON FOR BEGINNERS (BASICS OF PYTHON)
PYTHON FOR BEGINNERS (BASICS OF PYTHON)
 
pycon-2015-liza-daly
pycon-2015-liza-dalypycon-2015-liza-daly
pycon-2015-liza-daly
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdf
 
A Python Tutorial
A Python TutorialA Python Tutorial
A Python Tutorial
 
Introduction to python3.pdf
Introduction to python3.pdfIntroduction to python3.pdf
Introduction to python3.pdf
 
Core Python.doc
Core Python.docCore Python.doc
Core Python.doc
 
型ヒントについて考えよう!
型ヒントについて考えよう!型ヒントについて考えよう!
型ヒントについて考えよう!
 
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
PyBCN - Pytest: recomendaciones, paquetes básicos para testing en Python y Dj...
 

More from masahitojp

Build a RESTful API with the Serverless Framework
Build a RESTful API with the Serverless FrameworkBuild a RESTful API with the Serverless Framework
Build a RESTful API with the Serverless Frameworkmasahitojp
 
Presentation kyushu-2018
Presentation kyushu-2018Presentation kyushu-2018
Presentation kyushu-2018masahitojp
 
serverless framework + AWS Lambda with Python
serverless framework + AWS Lambda with Pythonserverless framework + AWS Lambda with Python
serverless framework + AWS Lambda with Pythonmasahitojp
 
20170131 python3 6 PEP526
20170131 python3 6 PEP526 20170131 python3 6 PEP526
20170131 python3 6 PEP526 masahitojp
 
chat bot framework for Java8
chat bot framework for Java8chat bot framework for Java8
chat bot framework for Java8masahitojp
 
Akka meetup 2014_sep
Akka meetup 2014_sepAkka meetup 2014_sep
Akka meetup 2014_sepmasahitojp
 
Pyconjp2014_implementations
Pyconjp2014_implementationsPyconjp2014_implementations
Pyconjp2014_implementationsmasahitojp
 
Pyconsg2014 pyston
Pyconsg2014 pystonPyconsg2014 pyston
Pyconsg2014 pystonmasahitojp
 
Riak map reduce for beginners
Riak map reduce for beginnersRiak map reduce for beginners
Riak map reduce for beginnersmasahitojp
 
Play2 translate 20120714
Play2 translate 20120714Play2 translate 20120714
Play2 translate 20120714masahitojp
 
Play2の裏側
Play2の裏側Play2の裏側
Play2の裏側masahitojp
 
Play!framework2.0 introduction
Play!framework2.0 introductionPlay!framework2.0 introduction
Play!framework2.0 introductionmasahitojp
 
5分で説明する Play! scala
5分で説明する Play! scala5分で説明する Play! scala
5分で説明する Play! scalamasahitojp
 

More from masahitojp (14)

Build a RESTful API with the Serverless Framework
Build a RESTful API with the Serverless FrameworkBuild a RESTful API with the Serverless Framework
Build a RESTful API with the Serverless Framework
 
Presentation kyushu-2018
Presentation kyushu-2018Presentation kyushu-2018
Presentation kyushu-2018
 
serverless framework + AWS Lambda with Python
serverless framework + AWS Lambda with Pythonserverless framework + AWS Lambda with Python
serverless framework + AWS Lambda with Python
 
20170131 python3 6 PEP526
20170131 python3 6 PEP526 20170131 python3 6 PEP526
20170131 python3 6 PEP526
 
chat bot framework for Java8
chat bot framework for Java8chat bot framework for Java8
chat bot framework for Java8
 
Akka meetup 2014_sep
Akka meetup 2014_sepAkka meetup 2014_sep
Akka meetup 2014_sep
 
Pyconjp2014_implementations
Pyconjp2014_implementationsPyconjp2014_implementations
Pyconjp2014_implementations
 
Pyconsg2014 pyston
Pyconsg2014 pystonPyconsg2014 pyston
Pyconsg2014 pyston
 
Pykonjp2014
Pykonjp2014Pykonjp2014
Pykonjp2014
 
Riak map reduce for beginners
Riak map reduce for beginnersRiak map reduce for beginners
Riak map reduce for beginners
 
Play2 translate 20120714
Play2 translate 20120714Play2 translate 20120714
Play2 translate 20120714
 
Play2の裏側
Play2の裏側Play2の裏側
Play2の裏側
 
Play!framework2.0 introduction
Play!framework2.0 introductionPlay!framework2.0 introduction
Play!framework2.0 introduction
 
5分で説明する Play! scala
5分で説明する Play! scala5分で説明する Play! scala
5分で説明する Play! scala
 

Recently uploaded

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 

Python Type Hints Guide for PyCon Kyushu 2019

  • 1. Python PyCon Kyushu in Okinawa 2019 May 18th in Ryukyu University, Okinawa Python - PyCon Kyushu in Okinawa 2019 May 18th 1
  • 2. Python - PyCon Kyushu in Okinawa 2019 May 18th 2
  • 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
  • 5. Nulab provide collaboration softwares. Python - PyCon Kyushu in Okinawa 2019 May 18th 5
  • 6. Today's Goal Type Hints ? Type Hints Python2 to 3 Python - PyCon Kyushu in Okinawa 2019 May 18th 6
  • 7. Not covering today ? Technical Detail Python - PyCon Kyushu in Okinawa 2019 May 18th 7
  • 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
  • 29. typing module Python - PyCon Kyushu in Okinawa 2019 May 18th 29
  • 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
  • 46. example Python - PyCon Kyushu in Okinawa 2019 May 18th 46
  • 47. statistic type analysis Python - PyCon Kyushu in Okinawa 2019 May 18th 47
  • 48. statistic type analysis • for check variable type(no runable code) • python has some tools • mypy • pytypes (not talk) Python - PyCon Kyushu in Okinawa 2019 May 18th 48
  • 49. example Python - PyCon Kyushu in Okinawa 2019 May 18th 49
  • 50. mypy • https://github.com/python/mypy • made by JukkaL & Guido • can output report • We can use!(PEP526 style) Python - PyCon Kyushu in Okinawa 2019 May 18th 50
  • 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
  • 55. Continuous Integration • Jenkins • Travis CI • CircleCi Python - PyCon Kyushu in Okinawa 2019 May 18th 55
  • 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
  • 57. sample for CI Sample Python - PyCon Kyushu in Okinawa 2019 May 18th 57
  • 58. example(error) Python - PyCon Kyushu in Okinawa 2019 May 18th 58
  • 59. example(success) Python - PyCon Kyushu in Okinawa 2019 May 18th 59
  • 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
  • 65. Python2 to 3 • • • • -> • Python - PyCon Kyushu in Okinawa 2019 May 18th 65
  • 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
  • 69. ? Python - PyCon Kyushu in Okinawa 2019 May 18th 69
  • 70. Thank you @masahito Python - PyCon Kyushu in Okinawa 2019 May 18th 70
  • 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
  • 74. mypy plugin • mypy • plugin mypy plugin • sqlAlchemy etc plugin Python - PyCon Kyushu in Okinawa 2019 May 18th 74
  • 75. mypy plugin • dropbox SqlAlchemy mypy-stub • https://github.com/python/mypy/issues/3538 • https://github.com/dropbox/sqlalchemy-stubs Python - PyCon Kyushu in Okinawa 2019 May 18th 75
  • 76. mypy PEP561 • PEP 561 is • • numpy stub • https://github.com/numpy/numpy-stubs Python - PyCon Kyushu in Okinawa 2019 May 18th 76
  • 78. User Defined Protocol from typing import Iterable from typing_extensions import Protocol class SupportsClose(Protocol): def close(self) -> None: ... # Empty method body (explicit '...') class Resource: # No SupportsClose base class! # ... some methods ... def close(self) -> None: self.resource.release() def close_all(items: Iterable[SupportsClose]) -> None: for item in items: item.close() close_all([Resource(), open('some/file')]) # Okay! DuckTyping Python - PyCon Kyushu in Okinawa 2019 May 18th 78
  • 79. Thanks again! Python - PyCon Kyushu in Okinawa 2019 May 18th 79