82

Python doc __future__

In python doc about __future__ there is a table below where it shows that annotations "optional in" 3.7.0b1 and "mandatory in" 4.0 but I am still able to use annotations in 3.8.2 without importing annotations then what is the use of it.

>>> def add_int(a:int, b:int) -> int:
...     return a + b
>>> add_int.__annotations__
{'a': <class 'int'>, 'b': <class 'int'>, 'return': <class 'int'>}

I doubt I don't clearly understand the meaning of "optional in" and "mandatory in" here

Donald Duck
  • 8,409
  • 22
  • 75
  • 99

2 Answers2

142

Mandatory is an interesting word choice. I guess it means that it's by default in the language. You don't have to enable it with from __future__ import annotations

The annotations feature are referring to the PEP 563: Postponed evaluation of annotations. It's an enhancement to the existing annotations feature which was initially introduced in python 3.0 and redefined as type hints in python 3.5, that's why your code works under python 3.8.

Here's what optional from __future__ import annotations changes in python 3.7+:

class A:
    def f(self) -> A: # NameError: name 'A' is not defined
        pass

but this works

from __future__ import annotations

class A:
    def f(self) -> A:
        pass

See this chapter in python 3.7 what's new about postponed annotations:

Since this change breaks compatibility, the new behavior needs to be enabled on a per-module basis in Python 3.7 using a __future__ import:

from __future__ import annotations

It will become the default in Python 3.10*.

* it was announced to be default in 3.10 (when python3.7 was released), but it was now moved to a later release

RafalS
  • 5,834
  • 1
  • 20
  • 25
  • it didn't completely answered my question "Optional in" and why my code working without importing annotations from __future__ – CrazyPythonProgrammer May 01 '20 at 15:06
  • bc function __ annotations __ (dunder) were introduced in Python 3.0 per PEP 3107. (cf. bit.ly/python-annotations). Subsequent PEPs added type hints (3.5), updated syntax (3.6) changed evaluation time (3.7) and restricted annotation semantics to type hinting (3.8). HTH. – Forest Jun 10 '21 at 16:10
  • 6
    `from __future__ import annotations` will not be default in Python 3.10, but in a posterior version: https://dev.to/tiangolo/the-future-of-fastapi-and-pydantic-is-bright-3pbm (see "What's Next" section). – gorcajo Jan 14 '22 at 11:49
  • 2
    This import also allows you to use `|` to define union types in earlier versions. – Quinten C Oct 03 '22 at 20:17
  • I have a problem with "SyntaxError: future feature annotations is not defined". How to solve it? – Land Oct 22 '22 at 02:30
  • 1
    @Land it requires python 3.7+, see https://stackoverflow.com/questions/52889746/cant-import-annotations-from-future – RafalS Oct 23 '22 at 08:07
  • I think it will be implemented in Python3.12 using lazyimports (not sure though) – Bashir Abdelwahed Mar 09 '23 at 08:58
  • I expect the word "Mandatory" was used because Python is really just an standard/interface definition, though most people think of "Python" as referring to CPython – ThomasH Jul 25 '23 at 19:02
15

Mandatory as in coming by default. Optional as in needed to be "activated" from an from __future__ import annotations statement

theX
  • 1,008
  • 7
  • 22