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

Python と型ヒントとその使い方

  • 1.
    Python PyCon Kyushu inOkinawa 2019 May 18th in Ryukyu University, Okinawa Python - PyCon Kyushu in Okinawa 2019 May 18th 1
  • 2.
    Python - PyConKyushu 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 collaborationsoftwares. 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 ? TechnicalDetail Python - PyCon Kyushu in Okinawa 2019 May 18th 7
  • 8.
    Today's Outline Ⅰ. Introductionof Type Hints Ⅱ. Usage of Type Hints Ⅲ. Benefits of Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 8
  • 9.
    Today's Outline ! Ⅰ. Introductionof Type Hints Ⅱ. Usage of Type Hints Ⅲ. Benefits of Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 9
  • 10.
    Introduction of TypeHints • 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 TypeHints 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 TypeHints 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 TypeHints 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 TypeHints 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 TypeHints 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 isTypeHints ??? Python - PyCon Kyushu in Okinawa 2019 May 18th 16
  • 17.
    We can annotateour codes only. Python - PyCon Kyushu in Okinawa 2019 May 18th 17
  • 18.
    But You uselibrary, 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 goodpoint for checking type ??? Python - PyCon Kyushu in Okinawa 2019 May 18th 19
  • 20.
    We can checkour 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 TypeHints Python - PyCon Kyushu in Okinawa 2019 May 18th 21
  • 22.
    History of TypeHints 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) defgreeting(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) defcompile(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) defgreeting(name: str) -> str: return 'Hello ' + name • PEP-484 Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 25
  • 26.
    Points in PEP484 Pythondynamically typed language • • • Python - PyCon Kyushu in Okinawa 2019 May 18th 26
  • 27.
    inconvinient of PEP484 defgreeting(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 thetyping 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 Python3.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 Ⅰ. Introductionof Type Hints ! Ⅱ. Usage of Type Hints Ⅲ. Benefits of Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 33
  • 34.
    Usage Of TypeHints • A. How to write • B. How to get Type Hints Info Python - PyCon Kyushu in Okinawa 2019 May 18th 34
  • 35.
    How to write PEP526style def greeting(name: str) -> str: return 'Hello ' + name greet: str = greeting("Masato") Python - PyCon Kyushu in Okinawa 2019 May 18th 35
  • 36.
    difference of PEP484and 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 # Wecan 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 TypeHints 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 getType Hints Info Q. where is variable annotation A. at __annotaitons__ Python - PyCon Kyushu in Okinawa 2019 May 18th 39
  • 40.
    How to getType Hints Info example >>> answer:int = 42 >>> __annotations__ {'answer': <class 'int'>} Python - PyCon Kyushu in Okinawa 2019 May 18th 40
  • 41.
    Note for Class Wecan 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 Ⅰ. Introductionof Type Hints Ⅱ. Usage of Type Hints ! Ⅲ. Benefits of Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 42
  • 43.
    Benefits of TypeHints 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 • Simpleis 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 canuse code completion in • Editor • Visual Studio code • IDE • PyCharm(IntelliJ) Python - PyCon Kyushu in Okinawa 2019 May 18th 45
  • 46.
    example Python - PyConKyushu 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 - PyConKyushu in Okinawa 2019 May 18th 49
  • 50.
    mypy • https://github.com/python/mypy • madeby JukkaL & Guido • can output report • We can use!(PEP526 style) Python - PyCon Kyushu in Okinawa 2019 May 18th 50
  • 51.
    How to installmypy $ 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) fromtyping 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 usein 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 typeannotation ! 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 • wecan 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 - PyConKyushu in Okinawa 2019 May 18th 58
  • 59.
    example(success) Python - PyConKyushu 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 datehas been updated to January 1, 2020. Python - PyCon Kyushu in Okinawa 2019 May 18th 61
  • 62.
    We want touse 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 theBenefits of Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 67
  • 68.
    Today's Outline ✅ 1. Introductionof Type Hints ✅ 2. Usage of Type Hints ✅ 3. Benefits of Type Hints Python - PyCon Kyushu in Okinawa 2019 May 18th 68
  • 69.
    ? Python - PyConKyushu 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 • dropboxSqlAlchemy 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 • PEP561 is • • numpy stub • https://github.com/numpy/numpy-stubs Python - PyCon Kyushu in Okinawa 2019 May 18th 76
  • 77.
  • 78.
    User Defined Protocol fromtyping 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