-
Notifications
You must be signed in to change notification settings - Fork 8
/
py-type.slide
127 lines (75 loc) · 2.52 KB
/
py-type.slide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Better type at Python
7 Nov 2015
Rick Mak
Oursky
@rickmak
CC81327D
* Python 3.5 Release
https://docs.python.org/3.5/whatsnew/3.5.html
Released on September 13, 2015, 2 months ago
*New*library*modules*introduced*
`typing`: PEP 484 – Type Hints
The syntax introduced long ago, Dec-2006
PEP 3107 - Function Annotations
* Your view on type
- Anyone used typing?
- Anyone used function annotations?
- Anyone thinks using it will have performance boost or punishment?
* Get it working
- Python 3.5
import typing
- Python 3.2-3.4
pip install typing
* Some code
.code py-type/nohint.py
* Same code with type hints
.code py-type/hint.py
* More complex type hints
Dictionary
.code py-type/complex.py /1 OMIT/,/2 OMIT/
* More complex type hints
Sequence and List
.code py-type/complex.py /2 OMIT/,/3 OMIT/
* Forward references
.code py-type/complex.py /3 OMIT/,/4 OMIT/
* What are type hints good for?
- Make IDE works better
- Make developement of static analysis tools easier
- Improve readability, for tools and for human
- Serve as Documentation
* Make IDE works better
* Let see how PyCharm works
.image py-type/pycharm.png
* Make development of static analysis tools easier
* mypy
[[https://github.com/JukkaL/mypy][JukkaL/mypy]]
pip install mypy-lang
Analyse a file without running it
(venv)$ mypy callee.py
callee.py, line 7: Argument 1 to "hello" has incompatible type "int"; expected "str"
* Serve as (additional) documentation
* Docstring for sphinx to gen autodoc
.code py-type/docstring.py
* Same information using func type annotation
.code py-type/doctype.py
* Python2 with pyi stub
An *.pyi file that only have interface
.code py-type/py2.pyi
Also useful for
- Package you can't change
- Legacy package you don't want to change
There is github repos hosting stub for stdlib
[[https://github.com/python/typeshed][python/typeshed]]
* What type hints are not
- Not about code generation
- Your code can still break after type checking
- No runtime type checking, No performance overhead
* How you can find out function annotation in your code
You can use [[https://docs.python.org/3.5/library/inspect.html][inspect]] to get back the annotation.
.code py-type/complex.py /4 OMIT/,/5 OMIT/
i.e. You may abuse it to do type conversion or validation at runtime.
* Reference
- [[http://www.mypy-lang.org][mypy]]
- [[https://www.youtube.com/watch?v=Yqnrfa5ri7E][Guido van Rossum - Type Hints for Python 3.5]]
- http://blog.pirx.ru/media/files/2015/type-hinting-talk/type-hinting.html