Public | Automated Build

Last pushed: 3 months ago
Short Description
Docker image for py-backwards
Full Description


Python to python compiler that allows you to use some Python 3.6 features in older versions, you can try it in the online demo.

Requires Python 3.3+ to run, can compile down to 2.7.

Supported features

Target 3.5:

Target 3.4:

Target 3.3:

Target 3.2:

Target 2.7:

For example, if you have some python 3.6 code, like:

def returning_range(x: int):
    yield from range(x)
    return x

def x_printer(x):
    val: int
    val = yield from returning_range(x)
    print(f'val {val}')

def formatter(x: int) -> dict:
    items: list = [*x_printer(x), x]
    print(*items, *items)
    return {'items': items}

result = {'x': 10, **formatter(10)}

class NumberManager:
    def ten(self):
        return 10

    def eleven(cls):
        return 11

class ImportantNumberManager(NumberManager):
    def ten(self):
        return super().ten()

    def eleven(cls):
        return super().eleven()


You can compile it for python 2.7 with:

➜ py-backwards -i -o -t 2.7

Got some ugly code and ensure that it works:

➜ python3.6
val 10
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
{'x': 10, 'items': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
➜ python2                           
val 10
0 1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 10
{'x': 10, 'items': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}



pip install py-backwards

Compile code:

py-backwards -i src -o compiled -t 2.7

Testing compiled code

For testing compiled code with each supported python version you can use tox
and tox-py-backwards. You need to install them:

pip install tox tox-py-backwards

Fill tox.ini (py_backwards = true in testenv section enables py-backwards), like:

envlist = py27,py33,py34,py35,py36

deps = pytest
commands = py.test
py_backwards = true

And run tests with:


Distributing compiled code

For distributing packages compiled with py-backwards you can use py-backwards-packager.
Install it with:

pip install py-backwards-packager

And change setup import in to:

    from py_backwards_packager import setup
except ImportError:
    from setuptools import setup

By default all targets enabled, but you can limit them with:

      py_backwards_targets=['2.7', '3.3'])

After that your code will be automatically compiled on bdist and bdist_wheel.

Running on systems without Python 3.3+

You can use docker for running py-backwards on systems without Python 3.3+, for example
for testing on travis-ci with Python 2.7:

docker run -v $(pwd):/data/ nvbn/py-backwards -i example -o out -t 2.7



pip install .
python develop
pip install -r requirements.txt

Run tests:

 py.test -vvvv --capture=sys --enable-functional

Run tests on systems without docker:

 py.test -vvvv

Writing code transformers

First of all, you need to inherit from BaseTransformer, BaseNodeTransformer (if you want to use
NodeTransfromer interface),
or BaseImportRewrite (if you want just to change import).

If you use BaseTransformer, override class method def transform(cls, tree: ast.AST) -> TransformationResult, like:

from ..types import TransformationResult
from .base import BaseTransformer

class MyTransformer(BaseTransformer):
    def transform(cls, tree: ast.AST) -> TransformationResult:
        return TransformationResult(tree=tree,

If you use BaseNodeTransformer, override visit_* methods, for simplification this class
have a whole tree in self._tree, you should also set self._tree_changed = True if the tree
was changed:

from .base import BaseNodeTransformer

class MyTransformer(BaseNodeTransformer):
    dependencies = []  # additional dependencies

    def visit_FunctionDef(self, node: ast.FunctionDef) -> ast.FunctionDef:
        self._tree_changed = True  # Mark that transformer changed tree
        return self.generic_visit(node)

If you use BaseImportRewrite, just override rewrites, like:

from .base import BaseImportRewrite

class MyTransformer(BaseImportRewrite):
    dependencies = ['pathlib2']

    rewrites = [('pathlib', 'pathlib2')]

After that you need to add your transformer to transformers.__init__.transformers.

It's hard to write code in AST, because of that we have snippets:

from ..utils.snippet import snippet, let, extend

def my_snippet(class_name, class_body):
    class class_name:  # will be replaced with `class_name`
        extend(class_body)  # body of the class will be extended with `class_body`

        def fn(self):
            let(x)  # x will be replaced everywhere with unique name, like `_py_backwards_x_1`
            x = 10
            return x

And you can easily get content of snippet with:

                    class_body=[ast.Expr(...), ...])

Also please look at tree utils,
it contains such useful functions like find, get_parent and etc.

Related projects

License MIT

Docker Pull Command
Source Repository

Comments (0)