Giter Site home page Giter Site logo

future-annotations's Introduction

DEPRECATED

with python3.6 reaching end of life, there is no need for this


Build Status Azure DevOps coverage pre-commit.ci status

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__':
    raise SystemExit(main())

you may also like

future-annotations's People

Contributors

asottile avatar pre-commit-ci[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.