|
1 |
| -PEP: NNN |
| 1 | +PEP: 484 |
2 | 2 | Title: Type Hints
|
3 | 3 | Version: $Revision$
|
4 | 4 | Last-Modified: $Date$
|
@@ -329,262 +329,10 @@ enforcing type information during runtime, is out of scope for this PEP.
|
329 | 329 | .. FIXME: Describe run-time behavior of generic types.
|
330 | 330 |
|
331 | 331 |
|
332 |
| -Existing Approaches in Python |
333 |
| -============================= |
334 |
| - |
335 |
| -Cython and Numba |
336 |
| ----------------- |
337 |
| - |
338 |
| -Numba [numba]_ is a *just-in-time* specializing compiler producing |
339 |
| -optimized native code from annotated Python and NumPy code. |
340 |
| - |
341 |
| -obiwan |
342 |
| ------- |
343 |
| - |
344 |
| -obiwan [obiwan]_ is a library enabling runtime type checking inspired |
345 |
| -by TypeScript [typescript]_ (see `Existing Approaches in Other |
346 |
| -Languages <#typescript>`_). The syntax leverages function annotations, |
347 |
| -extending it to validate callback functions, elements of dictionaries |
348 |
| -and lists. Type checkers might be functions, in which case a type is |
349 |
| -considered valid if the type checker returns True. |
350 |
| - |
351 |
| -Examples:: |
352 |
| - |
353 |
| - def divide(a: int, b: float) -> number: |
354 |
| - return a/b |
355 |
| - |
356 |
| - def robodial(person: {"name":str, "phone": {"type":str, "number":str}}): |
357 |
| - ... |
358 |
| - |
359 |
| - def on_success(callback: function(int, any, ...)): |
360 |
| - ... |
361 |
| - |
362 |
| -pytypedecl |
363 |
| ----------- |
364 |
| - |
365 |
| -pytypedecl [pytypedecl]_ consists of a type declaration language for |
366 |
| -Python and an optional runtime type checker. Type declarations for |
367 |
| -``module.py`` are kept in a separate file called ``module.pytd``. This |
368 |
| -solves issues with declaration ordering. |
369 |
| - |
370 |
| -While initially inspired by the PEP 3107 syntax, pytypedecl diverged to |
371 |
| -support the following: overloading (specifying the same function |
372 |
| -multiple times with different argument types), union types (listing |
373 |
| -multiple possible types for a single argument), generics for |
374 |
| -collections, and exceptions raised (for documentation purposes). |
375 |
| - |
376 |
| -Example:: |
377 |
| - |
378 |
| - class Logger: |
379 |
| - def log(messages: list<str>, buffer: Readable or Writeable) raises IOError |
380 |
| - def log(messages: list<str>) -> None |
381 |
| - def setStatus(status: int or str) |
382 |
| - |
383 |
| -Argument Clinic |
384 |
| ---------------- |
385 |
| - |
386 |
| -Argument Clinic [argumentclinic]_ is a preprocessor for CPython |
387 |
| -C files, automating maintenance of argument parsing code for “builtins”. |
388 |
| - |
389 |
| -Example argument declaration:: |
390 |
| - |
391 |
| - /*[clinic input] |
392 |
| - os.chmod |
393 |
| - |
394 |
| - path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
395 |
| - Path to be modified. May always be specified as a str or bytes. |
396 |
| - |
397 |
| - mode: int |
398 |
| - Operating-system mode bitfield. |
399 |
| - |
400 |
| - * |
401 |
| - |
402 |
| - dir_fd : dir_fd(requires='fchmodat') = None |
403 |
| - If not None, it should be a file descriptor open to a dir, and |
404 |
| - path should be relative; path will then be relative to that |
405 |
| - dir. |
406 |
| - |
407 |
| - follow_symlinks: bool = True |
408 |
| - If False, and the last element of the path is a symlink, chmod |
409 |
| - will modify the symlink itself instead of the file the link |
410 |
| - points to. |
411 |
| - |
412 |
| - Change the access permissions of a file. |
413 |
| - [clinic start generated code]*/ |
414 |
| - |
415 |
| - |
416 |
| -NumPy |
417 |
| ------ |
418 |
| - |
419 |
| -NumPy [numpy]_ is an extension to Python adding support for |
420 |
| -multi-dimensional arrays, matrices and operations to operate on those. |
421 |
| - |
422 |
| -The project requires typing information in the API documentation. There |
423 |
| -is an unambiguous syntax for that type of documentation. Example |
424 |
| -documentation with types:: |
425 |
| - |
426 |
| - ndarray.item(*args) |
427 |
| - |
428 |
| - Copy an element of an array to a standard Python scalar and return it. |
429 |
| - |
430 |
| - Parameters |
431 |
| - ---------- |
432 |
| - \\*args : Arguments (variable number and type) |
433 |
| - |
434 |
| - * none: in this case, the method only works for arrays |
435 |
| - with one element (`a.size == 1`), which element is |
436 |
| - copied into a standard Python scalar object and returned. |
437 |
| - |
438 |
| - * int_type: this argument is interpreted as a flat index into |
439 |
| - the array, specifying which element to copy and return. |
440 |
| - |
441 |
| - * tuple of int_types: functions as does a single int_type argument, |
442 |
| - except that the argument is interpreted as an nd-index into the |
443 |
| - array. |
444 |
| - |
445 |
| - Returns |
446 |
| - ------- |
447 |
| - z : Standard Python scalar object |
448 |
| - A copy of the specified element of the array as a suitable |
449 |
| - Python scalar |
450 |
| - |
451 |
| - |
452 |
| -Existing Approaches in Other Languages |
453 |
| -====================================== |
454 |
| - |
455 |
| -ActionScript |
456 |
| ------------- |
457 |
| - |
458 |
| -ActionScript [actionscript]_ is a class-based, single inheritance, |
459 |
| -object-oriented superset of ECMAScript. It supports inferfaces and |
460 |
| -strong runtime-checked static typing. Compilation supports a “strict |
461 |
| -dialect” where type mismatches are reported at compile-time. |
462 |
| - |
463 |
| -Example code with types:: |
464 |
| - |
465 |
| - package { |
466 |
| - import flash.events.Event; |
467 |
| - |
468 |
| - public class BounceEvent extends Event { |
469 |
| - public static const BOUNCE:String = "bounce"; |
470 |
| - private var _side:String = "none"; |
471 |
| - |
472 |
| - public function get side():String { |
473 |
| - return _side; |
474 |
| - } |
475 |
| - |
476 |
| - public function BounceEvent(type:String, side:String){ |
477 |
| - super(type, true); |
478 |
| - _side = side; |
479 |
| - } |
480 |
| - |
481 |
| - public override function clone():Event { |
482 |
| - return new BounceEvent(type, _side); |
483 |
| - } |
484 |
| - } |
485 |
| - } |
486 |
| - |
487 |
| -Dart |
488 |
| ----- |
489 |
| - |
490 |
| -Dart [dart]_ is a class-based, single inheritance, object-oriented |
491 |
| -language with C-style syntax. It supports interfaces, abstract classes, |
492 |
| -reified generics, and optional typing. |
493 |
| - |
494 |
| -Types are inferred when possible. The runtime differentiates between two |
495 |
| -modes of execution: *checked mode* aimed for development (catching type |
496 |
| -errors at runtime) and *production mode* recommended for speed execution |
497 |
| -(ignoring types and asserts). |
498 |
| - |
499 |
| -Example code with types:: |
500 |
| - |
501 |
| - class Point { |
502 |
| - final num x, y; |
503 |
| - |
504 |
| - Point(this.x, this.y); |
505 |
| - |
506 |
| - num distanceTo(Point other) { |
507 |
| - var dx = x - other.x; |
508 |
| - var dy = y - other.y; |
509 |
| - return math.sqrt(dx * dx + dy * dy); |
510 |
| - } |
511 |
| - } |
512 |
| - |
513 |
| -Hack |
514 |
| ----- |
515 |
| - |
516 |
| -Hack [hack]_ is a programming language that interoperates seamlessly |
517 |
| -with PHP. It provides opt-in static type checking, type aliasing, |
518 |
| -generics, nullable types, and lambdas. |
519 |
| - |
520 |
| -Example code with types:: |
521 |
| - |
522 |
| - <?hh |
523 |
| - class MyClass { |
524 |
| - private ?string $x = null; |
525 |
| - |
526 |
| - public function alpha(): int { |
527 |
| - return 1; |
528 |
| - } |
529 |
| - |
530 |
| - public function beta(): string { |
531 |
| - return 'hi test'; |
532 |
| - } |
533 |
| - } |
534 |
| - |
535 |
| - function f(MyClass $my_inst): string { |
536 |
| - // Will generate a hh_client error |
537 |
| - return $my_inst->alpha(); |
538 |
| - } |
539 |
| - |
540 |
| -TypeScript |
541 |
| ----------- |
542 |
| - |
543 |
| -TypeScript [typescript]_ is a typed superset of JavaScript that adds |
544 |
| -interfaces, classes, mixins and modules to the language. |
545 |
| - |
546 |
| -Type checks are duck typed. Multiple valid function signatures are |
547 |
| -specified by supplying overloaded function declarations. Functions and |
548 |
| -classes can use generics as type parametrization. Interfaces can have |
549 |
| -optional fields. Interfaces can specify array and dictionary types. |
550 |
| -Classes can have constructors that implicitly add arguments as fields. |
551 |
| -Classes can have static fields. Classes can have private fields. |
552 |
| -Classes can have getters/setters for fields (like property). Types are |
553 |
| -inferred. |
554 |
| - |
555 |
| -Example code with types:: |
556 |
| - |
557 |
| - interface Drivable { |
558 |
| - start(): void; |
559 |
| - drive(distance: number): boolean; |
560 |
| - getPosition(): number; |
561 |
| - } |
562 |
| - |
563 |
| - class Car implements Drivable { |
564 |
| - private _isRunning: boolean; |
565 |
| - private _distanceFromStart: number; |
566 |
| - |
567 |
| - constructor() { |
568 |
| - this._isRunning = false; |
569 |
| - this._distanceFromStart = 0; |
570 |
| - } |
571 |
| - |
572 |
| - public start() { |
573 |
| - this._isRunning = true; |
574 |
| - } |
575 |
| - |
576 |
| - public drive(distance: number): boolean { |
577 |
| - if (this._isRunning) { |
578 |
| - this._distanceFromStart += distance; |
579 |
| - return true; |
580 |
| - } |
581 |
| - return false; |
582 |
| - } |
| 332 | +Existing Approaches |
| 333 | +=================== |
583 | 334 |
|
584 |
| - public getPosition(): number { |
585 |
| - return this._distanceFromStart; |
586 |
| - } |
587 |
| - } |
| 335 | +PEP 482 [pep-482]_ lists existing approaches in Python and other languages. |
588 | 336 |
|
589 | 337 |
|
590 | 338 | Is type hinting Pythonic?
|
@@ -622,37 +370,13 @@ References
|
622 | 370 | ==========
|
623 | 371 |
|
624 | 372 | .. [pep-3107]
|
625 |
| - http://www.python.org/dev/peps/pep-3107/ |
| 373 | + https://www.python.org/dev/peps/pep-3107/ |
626 | 374 |
|
627 | 375 | .. [mypy]
|
628 | 376 | http://mypy-lang.org
|
629 | 377 |
|
630 |
| -.. [obiwan] |
631 |
| - http://pypi.python.org/pypi/obiwan |
632 |
| - |
633 |
| -.. [numba] |
634 |
| - http://numba.pydata.org |
635 |
| - |
636 |
| -.. [pytypedecl] |
637 |
| - https://github.com/google/pytypedecl |
638 |
| - |
639 |
| -.. [argumentclinic] |
640 |
| - https://docs.python.org/3/howto/clinic.html |
641 |
| - |
642 |
| -.. [numpy] |
643 |
| - http://www.numpy.org |
644 |
| - |
645 |
| -.. [typescript] |
646 |
| - http://www.typescriptlang.org |
647 |
| - |
648 |
| -.. [hack] |
649 |
| - http://hacklang.org |
650 |
| - |
651 |
| -.. [dart] |
652 |
| - https://www.dartlang.org |
653 |
| - |
654 |
| -.. [actionscript] |
655 |
| - http://livedocs.adobe.com/specs/actionscript/3/ |
| 378 | +.. [pep-482] |
| 379 | + https://www.python.org/dev/peps/pep-0482/ |
656 | 380 |
|
657 | 381 | .. [pyflakes]
|
658 | 382 | https://github.com/pyflakes/pyflakes/
|
|
0 commit comments