Skip to main content

Build Status Azure DevOps coverage

future-annotations

A backport of __future__ annotations to python<3.7.

Installation

pip install future-annotations

Usage

Include the following encoding cookie at the top of your file (this replaces the utf-8 cookie if you already have it):

# -*- coding: future_annotations -*-

And then write python3.7+ forward-annotation code as usual!

# -*- coding: future_annotations -*-
class C:
    @classmethod
    def make(cls) -> C:
        return cls()

print(C.make())
$ python3.6 main.py
<__main__.C object at 0x7fb50825dd90>
$ mypy main.py
Success: no issues found in 1 source file

Showing transformed source

future-annotations also includes a cli to show transformed source.

$ future-annotations-show main.py
# ****************************** -*-
class C:
    @classmethod
    def make(cls) -> 'C':
        return cls()

print(C.make())

How does this work?

future-annotations has two parts:

  1. A utf-8 compatible codec which performs source manipulation
    • The codec first decodes the source bytes using the UTF-8 codec
    • The codec then leverages tokenize-rt to rewrite annotations.
  2. A .pth file which registers a codec on interpreter startup.

when you aren't using normal site registration

in setups (such as aws lambda) where you utilize PYTHONPATH or sys.path instead of truly installed packages, the .pth magic above will not take.

for those circumstances, you'll need to manually initialize future-annotations in a non-annotations wrapper. for instance:

import future_annotations

future_annotations.register()

from actual_main import main

if __name__ == '__main__':
    exit(main())

you may also like

Release files for future-annotations 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for future-annotations 1.0.0
File Size Uploaded
future_annotations-1.0.0.tar.gz 5.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for future-annotations 1.0.0
File Interpreter ABI Platform
future_annotations-1.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.2 kB

Release files / future_annotations-1.0.0.tar.gz

Download URL future_annotations-1.0.0.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
c707d19f7c74e08d9e67b310fd6a7438ff510ba5cbfb7334695627f8f69c6378
BLAKE2b-256 checksum
How to use checksums
453a168feb6471a3142b1a501947e2f45afbbe9753e44f9d05317553ad7c04c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

Release files / future_annotations-1.0.0-py2.py3-none-any.whl

Download URL future_annotations-1.0.0-py2.py3-none-any.whl
Size 5.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a38cc5d9602181a7eb04550d34644508a63e5550c82c4b45b28b86923be203d2
BLAKE2b-256 checksum
How to use checksums
2aa695dd5f6dd2a785d69fa2fd2c7c217c13a6eb9c1f37eddc232eb11bfd8fa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page