11

I keep hearing how type hinting will be a new feature in 3.5, but that makes me wonder what the arrow indicator (->) was in 3.3?

You can see it in the 3.3 grammar spec here, which I found from this question asked 2 years ago.

I'm wondering, did type hinting exist before, but in a limited fashion, and 3.5 is bringing more major support? Or is my understanding of type hinting incorrect, and it actually means something else?

Community
  • 1
  • 1
bob
  • 1,759
  • 2
  • 12
  • 24
  • See [PEP-3107](https://www.python.org/dev/peps/pep-3107/), which introduced the function annotations, and [PEP-0484](https://www.python.org/dev/peps/pep-0484/), which covers type hinting. – jonrsharpe Jun 24 '15 at 14:29
  • Related: [How / why does Python type hinting syntax work?](https://stackoverflow.com/q/29770412) – Martijn Pieters Jun 24 '15 at 16:31

1 Answers1

17

The -> is used for annotations. One of the use cases for annotations is type hinting.

Python 3.0 added annotations, Python 3.5 builds on that feature by introducing type hinting, standardising the feature.

The relevant PEP (Python Enhancement Proposals) are:

Annotations are just syntax, type hinting is specific functionality.

You can use the syntax for anything you like, like inline documentation:

def documentation(self: "the instance", arg1: "first argument") -> "nothing is returned":
    pass

All that the syntax does is attach that extra information you provided to the function object:

>>> def documentation(self: "the instance", arg1: "first argument") -> "nothing is returned":
...     pass
... 
>>> documentation.__annotations__
{'return': 'nothing is returned', 'arg1': 'first argument', 'self': 'the instance'}

The Type Hinting specification specifies how you could use those annotations to say something about what type each argument should be and what is returned. It is a specific application of annotations in that it defines how to interpret the annotations.

The Type Hinting PEP explicitly states it is not meant to be the only use of annotations:

Note that this PEP still explicitly does NOT prevent other uses of annotations, nor does it require (or forbid) any particular processing of annotations, even when they conform to this specification. It simply enables better coordination, as PEP 333 did for web frameworks.

Type hinting remains entirely optional, it is not nor will it ever be required that you use it. Again quoting the PEP:

While the proposed typing module will contain some building blocks for runtime type checking -- in particular the get_type_hints() function -- third party packages would have to be developed to implement specific runtime type checking functionality, for example using decorators or metaclasses. Using type hints for performance optimizations is left as an exercise for the reader.

It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.

Emphasis in the original.

You can install the typing module to add type hinting to earlier Python 3.x versions.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
  • Could you explain the difference? Could you give an example of an annotation, and then an example of type hinting? E.g., what will 3.5's type hinting make possible that is currently impossible with annotations? – bob Jun 24 '15 at 15:29
  • @user1903064: type hinting is an *application* of annotations. You put type hints in the annotations. You can put other things in the annotations. – Martijn Pieters Jun 24 '15 at 15:40
  • @user1903064 type hinting adds built-in functionality that was made possible by the introduction of function annotation syntax. Pre-3.5, without some external library, function annotations *don't do anything*, they're just stored for later use. – jonrsharpe Jun 24 '15 at 15:42
  • ah, okay. so is it correct to say that pre-3.5, the Python interpreter does nothing with the annotation info, but it is still useful for IDEs so they can tell you more detailed info? In 3.5, will type hinting be "enforced," so that a method that says it returns an integer must always return an integer? – bob Jun 24 '15 at 16:22
  • @user1903064: no type hinting will be enforced, it is entirely optional. The interpreter leaves well enough alone, but the functionality is being standardised and linters and IDEs can make use of it. – Martijn Pieters Jun 24 '15 at 16:23