Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between type hinting in 3.3 and 3.5?

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?

like image 211
bob Avatar asked Jun 24 '15 14:06

bob


People also ask

Do type hints do anything?

Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.

How does type hinting work in Python?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. The name: str syntax indicates the name argument should be of type str . The -> syntax indicates the greet() function will return a string.

What is a type hint?

PEP 484 introduced type hints — a way to make Python feel statically typed. While type hints can help structure your projects better, they are just that — hints — and by default do not affect the runtime.

Does Python make hinting faster?

No, type hints don't force Python to check the type of data being stored in a variable. They are just for the developer's reference and help auto-completion software. They allow IDEs to recognise data types and give suggestions based on this information.


1 Answers

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:

  • PEP 3107 Function Annotations -- added in Python 3.0
  • PEP 484 Type Hints - will be part of Python 3.5

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.

like image 55
Martijn Pieters Avatar answered Oct 26 '22 03:10

Martijn Pieters