2015-01-08 14:10:25 -05:00
|
|
|
PEP: 482
|
2015-01-16 12:11:05 -05:00
|
|
|
Title: Literature Overview for Type Hints
|
2015-01-08 14:10:25 -05:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2018-01-27 16:19:45 -05:00
|
|
|
Author: Łukasz Langa <lukasz@python.org>
|
2022-02-27 17:46:36 -05:00
|
|
|
Discussions-To: python-ideas@python.org
|
2019-05-01 10:40:01 -04:00
|
|
|
Status: Final
|
2015-01-08 14:10:25 -05:00
|
|
|
Type: Informational
|
2022-10-06 20:36:39 -04:00
|
|
|
Topic: Typing
|
2015-01-08 14:10:25 -05:00
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 08-Jan-2015
|
|
|
|
Post-History:
|
2021-03-23 10:55:26 -04:00
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
|
|
|
This PEP is one of three related to type hinting. This PEP gives a
|
2022-01-21 06:03:51 -05:00
|
|
|
literature overview of related work. The main spec is :pep:`484`.
|
2015-01-08 14:10:25 -05:00
|
|
|
|
|
|
|
|
2015-01-09 13:52:06 -05:00
|
|
|
Existing Approaches for Python
|
|
|
|
==============================
|
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
|
|
|
|
mypy
|
|
|
|
----
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
(This section is a stub, since `mypy`__ is essentially what we're
|
2015-01-08 14:10:25 -05:00
|
|
|
proposing.)
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://mypy-lang.org
|
|
|
|
|
2015-01-09 13:52:06 -05:00
|
|
|
|
|
|
|
Reticulated Python
|
|
|
|
------------------
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
`Reticulated Python`__ by Michael Vitousek is an example of
|
2015-01-09 13:52:06 -05:00
|
|
|
a slightly different approach to gradual typing for Python. It is
|
2023-07-31 12:50:14 -04:00
|
|
|
described in an actual `academic paper`__ written by
|
2015-01-09 13:52:06 -05:00
|
|
|
Vitousek with Jeremy Siek and Jim Baker (the latter of Jython fame).
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://github.com/mvitousek/reticulated
|
|
|
|
__ http://wphomes.soic.indiana.edu/jsiek/files/2014/03/retic-python.pdf
|
2015-01-09 13:52:06 -05:00
|
|
|
|
2015-01-09 18:21:14 -05:00
|
|
|
PyCharm
|
|
|
|
-------
|
|
|
|
|
|
|
|
PyCharm by JetBrains has been providing a way to specify and check
|
2023-07-31 12:50:14 -04:00
|
|
|
types for about four years. The type system suggested by PyCharm__
|
|
|
|
grew from simple class types to tuple types, generic types,
|
2015-01-09 18:21:14 -05:00
|
|
|
function types, etc. based on feedback of many users who shared their
|
|
|
|
experience of using type hints in their code.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://github.com/JetBrains/python-skeletons#types
|
2015-01-09 18:21:14 -05:00
|
|
|
|
2015-01-09 13:52:06 -05:00
|
|
|
Others
|
|
|
|
------
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
TBD: Add sections on pyflakes__, pylint__, numpy__,
|
|
|
|
`Argument Clinic`__, pytypedecl__, numba__, obiwan__.
|
2015-01-09 13:52:06 -05:00
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://github.com/pyflakes/pyflakes/
|
|
|
|
__ https://www.pylint.org
|
|
|
|
__ https://www.numpy.org
|
|
|
|
__ https://docs.python.org/3/howto/clinic.html
|
|
|
|
__ https://github.com/google/pytypedecl
|
|
|
|
__ https://numba.pydata.org
|
|
|
|
__ https://pypi.org/project/obiwan
|
2015-01-09 13:52:06 -05:00
|
|
|
|
|
|
|
Existing Approaches in Other Languages
|
|
|
|
======================================
|
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
ActionScript
|
|
|
|
------------
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
ActionScript__ is a class-based, single inheritance,
|
2015-01-08 14:10:25 -05:00
|
|
|
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.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://livedocs.adobe.com/specs/actionscript/3/
|
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
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
|
|
|
|
----
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
Dart__ is a class-based, single inheritance, object-oriented
|
2015-01-08 14:10:25 -05:00
|
|
|
language with C-style syntax. It supports interfaces, abstract classes,
|
|
|
|
reified generics, and optional typing.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://www.dartlang.org
|
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
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
|
|
|
|
----
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
Hack__ is a programming language that interoperates seamlessly
|
2015-01-08 14:10:25 -05:00
|
|
|
with PHP. It provides opt-in static type checking, type aliasing,
|
|
|
|
generics, nullable types, and lambdas.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ https://hacklang.org
|
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
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
|
|
|
|
----------
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
TypeScript__ is a typed superset of JavaScript that adds
|
2015-01-08 14:10:25 -05:00
|
|
|
interfaces, classes, mixins and modules to the language.
|
|
|
|
|
2023-07-31 12:50:14 -04:00
|
|
|
__ http://www.typescriptlang.org
|
|
|
|
|
2015-01-08 14:10:25 -05:00
|
|
|
Type checks are duck typed. Multiple valid function signatures are
|
|
|
|
specified by supplying overloaded function declarations. Functions and
|
2020-02-24 23:20:33 -05:00
|
|
|
classes can use generics as type parameterization. Interfaces can have
|
2015-01-08 14:10:25 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|