forked from python/peps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpep-0482.txt
260 lines (187 loc) · 5.84 KB
/
pep-0482.txt
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
PEP: 482
Title: Literature Overview for Type Hints
Version: $Revision$
Last-Modified: $Date$
Author: Łukasz Langa <[email protected]>
Discussions-To: Python-Ideas <[email protected]>
Status: Final
Type: Informational
Content-Type: text/x-rst
Created: 08-Jan-2015
Post-History:
Resolution:
Abstract
========
This PEP is one of three related to type hinting. This PEP gives a
literature overview of related work. The main spec is PEP 484.
Existing Approaches for Python
==============================
mypy
----
(This section is a stub, since mypy [mypy]_ is essentially what we're
proposing.)
Reticulated Python
------------------
Reticulated Python [reticulated]_ by Michael Vitousek is an example of
a slightly different approach to gradual typing for Python. It is
described in an actual academic paper [reticulated-paper]_ written by
Vitousek with Jeremy Siek and Jim Baker (the latter of Jython fame).
PyCharm
-------
PyCharm by JetBrains has been providing a way to specify and check
types for about four years. The type system suggested by PyCharm
[pycharm]_ grew from simple class types to tuple types, generic types,
function types, etc. based on feedback of many users who shared their
experience of using type hints in their code.
Others
------
TBD: Add sections on pyflakes [pyflakes]_, pylint [pylint]_, numpy
[numpy]_, Argument Clinic [argumentclinic]_, pytypedecl [pytypedecl]_,
numba [numba]_, obiwan [obiwan]_.
Existing Approaches in Other Languages
======================================
ActionScript
------------
ActionScript [actionscript]_ is a class-based, single inheritance,
object-oriented superset of ECMAScript. It supports inferfaces and
strong runtime-checked static typing. Compilation supports a “strict
dialect” where type mismatches are reported at compile-time.
Example code with types::
package {
import flash.events.Event;
public class BounceEvent extends Event {
public static const BOUNCE:String = "bounce";
private var _side:String = "none";
public function get side():String {
return _side;
}
public function BounceEvent(type:String, side:String){
super(type, true);
_side = side;
}
public override function clone():Event {
return new BounceEvent(type, _side);
}
}
}
Dart
----
Dart [dart]_ is a class-based, single inheritance, object-oriented
language with C-style syntax. It supports interfaces, abstract classes,
reified generics, and optional typing.
Types are inferred when possible. The runtime differentiates between two
modes of execution: *checked mode* aimed for development (catching type
errors at runtime) and *production mode* recommended for speed execution
(ignoring types and asserts).
Example code with types::
class Point {
final num x, y;
Point(this.x, this.y);
num distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return math.sqrt(dx * dx + dy * dy);
}
}
Hack
----
Hack [hack]_ is a programming language that interoperates seamlessly
with PHP. It provides opt-in static type checking, type aliasing,
generics, nullable types, and lambdas.
Example code with types::
<?hh
class MyClass {
private ?string $x = null;
public function alpha(): int {
return 1;
}
public function beta(): string {
return 'hi test';
}
}
function f(MyClass $my_inst): string {
// Will generate a hh_client error
return $my_inst->alpha();
}
TypeScript
----------
TypeScript [typescript]_ is a typed superset of JavaScript that adds
interfaces, classes, mixins and modules to the language.
Type checks are duck typed. Multiple valid function signatures are
specified by supplying overloaded function declarations. Functions and
classes can use generics as type parameterization. Interfaces can have
optional fields. Interfaces can specify array and dictionary types.
Classes can have constructors that implicitly add arguments as fields.
Classes can have static fields. Classes can have private fields.
Classes can have getters/setters for fields (like property). Types are
inferred.
Example code with types::
interface Drivable {
start(): void;
drive(distance: number): boolean;
getPosition(): number;
}
class Car implements Drivable {
private _isRunning: boolean;
private _distanceFromStart: number;
constructor() {
this._isRunning = false;
this._distanceFromStart = 0;
}
public start() {
this._isRunning = true;
}
public drive(distance: number): boolean {
if (this._isRunning) {
this._distanceFromStart += distance;
return true;
}
return false;
}
public getPosition(): number {
return this._distanceFromStart;
}
}
References
==========
.. [mypy]
http://mypy-lang.org
.. [reticulated]
https://github.com/mvitousek/reticulated
.. [reticulated-paper]
http://wphomes.soic.indiana.edu/jsiek/files/2014/03/retic-python.pdf
.. [pycharm]
https://github.com/JetBrains/python-skeletons#types
.. [obiwan]
http://pypi.python.org/pypi/obiwan
.. [numba]
http://numba.pydata.org
.. [pytypedecl]
https://github.com/google/pytypedecl
.. [argumentclinic]
https://docs.python.org/3/howto/clinic.html
.. [numpy]
http://www.numpy.org
.. [typescript]
http://www.typescriptlang.org
.. [hack]
http://hacklang.org
.. [dart]
https://www.dartlang.org
.. [actionscript]
http://livedocs.adobe.com/specs/actionscript/3/
.. [pyflakes]
https://github.com/pyflakes/pyflakes/
.. [pylint]
http://www.pylint.org
Copyright
=========
This document has been placed in the public domain.
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
coding: utf-8
End: