PEP 482 -- 类型提示的文档性概述(Literature Overview for Type Hints)

英文原文:https://www.python.org/dev/peps/pep-0482
采集日期:2020-02-27

PEP: 482
Title: Literature Overview for Type Hints
Author: Łukasz Langa lukasz@python.org
Discussions-To: Python-Ideas python-ideas@python.org
Status: Final
Type: Informational
Created: 08-Jan-2015
Post-History:
Resolution:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

目录

摘要(Abstract)

本文是有关类型提示的三篇文档之一。本文给出了类型提示相关的文档性概述。类型提示的主要规范文件是 PEP 484

已有的 Pyhton 方案(Existing Approaches for Python)

mypy

这里只是占个位(stub),因为 mypy 本质上就是这里的提案。

Reticulated Python

Michael Vitousek 的 Reticulated Python 就是一个 Python 渐进定型(gradual typing)的例子,其做法稍有差别。Vitousek 在与 Jeremy Siek、Jim Baker(后者在 Jython 界相当知名)撰写的学术论文中,对 Reticulated Python 进行了描述。

PyCharm

大约有四年了,JetBrains 的 PyCharm 已经提供了一种指定和检查类型的方案。在代码中使用类型提示并分享经验的用户有很多,根据这些用户的反馈,PyCharm 提出的类型系统已从简单的类拓展到元组、泛型、函数等类型。

其他(Others)

待确认:pyflakespylintnumpyArgument Clinicpytypedeclnumbaobiwan 的附加章节。

其他语言的已有方案(Existing Approaches in Other Languages)

ActionScript

Actionscript 是一种基于类的、单继承、面向对象的 ECMAScript 超集。它支持接口,提供强大的可运行时检查的静态类型推断能力。编译时支持一种“严格的方言”,能在编译时报告类型不符的错误。

包含类型的 ActionScript 示例代码:

  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 是一种基于类的、单继承、面向对象的语言,语法风格类似 C 语言。它支持接口、基类、具体化泛型(reified generics)和可选定型(optional typing)。

Dart 会尽可能地对类型进行推断。运行时会对两种执行模式进行区分:面向开发的检查模式(在运行时捕获类型错误)和推荐用于快速运行的生产模式(忽略类型检查和断言)。

包含类型的 Dart 示例代码:

  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 是一种可与 PHP 无缝互动的编程语言。它支持可选加入的(opt-in)静态类型检查机制、泛型、可空类型和 lambda 表达式。

包含类型的 Hack 示例代码:

  <?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 是 JavaScript 的类型化超集,加入了对接口、类、混合类(mixin)和模块的支持。

类型检查采用“鸭子”方式。只要给出重载的函数声明,即可指定多个有效的函数签名。函数和类可以把泛型用作类型的参数化。接口可以带有可选字段。接口可以指定数组和字典类型。类的构造函数可以隐式添加参数作为类的成员字段。类可以拥有静态字段。类可以拥有私有字段。类的字段可以带有 getter/setter(类似属性)。可以进行类型推断。

包含类型的 TypeScript 示例代码:

  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/(译者注:链接已失效,或可参考https://www.adobe.com/devnet/actionscript.html

【pyflakes】https://github.com/pyflakes/pyflakes/

【pylint】http://www.pylint.org

版权(Copyright)

本文已于公共领域发布。

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄