[Python-Dev] PEP 563: Postponed Evaluation of Annotations

Stéfane Fermigier sf at fermigier.com
Thu Nov 2 16:13:09 EDT 2017

On Thu, Nov 2, 2017 at 7:39 PM, Jukka Lehtosalo <jlehtosalo at gmail.com>

> As type checking has become the main use case for annotations, using
> annotations without a type checker is fast becoming a marginal use case.
> Type checkers can easily and reliably validate that names in annotations aren't
> misspelled.

Another common use case is dependency injection / IoC:

Examples include:

- Injector (https://github.com/alecthomas/injector):

>>> class Outer:
...     @inject
...     def __init__(self, inner: Inner):
...         self.inner = inner

- Flsk-Injector (ok it's the same underlying injector):

# Route with injection
def foo(db: sqlite3.Connection):
    users = db.execute('SELECT * FROM users').all()
    return render("foo.html")

- Apistar components (https://github.com/encode/apistar#components ):

def say_hello(user: User):
    return {'hello': user.username}

=> In each of the examples, the type annotation are used at runtime by the
IoC container to inject an object of the appropriate type, based on some

They may or may not be used by a typechecker too, but that's secondary.


Stefane Fermigier - http://fermigier.com/ - http://twitter.com/sfermigier -
Founder & CEO, Abilian - Enterprise Social Software -
Chairman, Free&OSS Group / Systematic Cluster -
Co-Chairman, National Council for Free & Open Source Software (CNLL) -
Founder & Organiser, PyData Paris - http://pydata.fr/
“You never change things by fighting the existing reality. To change
something, build a new model that makes the existing model obsolete.” — R.
Buckminster Fuller
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20171102/6d34b6a9/attachment.html>

More information about the Python-Dev mailing list