{"id":13502438,"url":"https://github.com/PythonCharmers/python-future","last_synced_at":"2025-03-29T10:33:01.019Z","repository":{"id":647477,"uuid":"11403699","full_name":"PythonCharmers/python-future","owner":"PythonCharmers","description":"Easy, clean, reliable Python 2/3 compatibility","archived":false,"fork":false,"pushed_at":"2024-07-16T00:40:28.000Z","size":4388,"stargazers_count":1177,"open_issues_count":186,"forks_count":296,"subscribers_count":38,"default_branch":"master","last_synced_at":"2025-03-25T16:17:07.316Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://python-future.org","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PythonCharmers.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":"docs/roadmap.rst","authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-07-14T13:57:47.000Z","updated_at":"2025-03-23T01:59:02.000Z","dependencies_parsed_at":"2024-01-18T09:56:53.674Z","dependency_job_id":"71e872b1-c4a2-42b8-acd1-6ff00b126451","html_url":"https://github.com/PythonCharmers/python-future","commit_stats":{"total_commits":1679,"total_committers":128,"mean_commits":13.1171875,"dds":"0.19118522930315662","last_synced_commit":"2d56f83adab5a0957cfc5abbe62db1e2d1912b61"},"previous_names":[],"tags_count":52,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PythonCharmers%2Fpython-future","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PythonCharmers%2Fpython-future/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PythonCharmers%2Fpython-future/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PythonCharmers%2Fpython-future/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PythonCharmers","download_url":"https://codeload.github.com/PythonCharmers/python-future/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246174208,"owners_count":20735406,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T22:02:13.950Z","updated_at":"2025-03-29T10:32:59.028Z","avatar_url":"https://github.com/PythonCharmers.png","language":"Python","funding_links":[],"categories":["Python","Compatiblity","Programming Languages"],"sub_categories":[],"readme":".. _overview:\n\nOverview: Easy, clean, reliable Python 2/3 compatibility\n========================================================\n\n.. image:: https://github.com/PythonCharmers/python-future/actions/workflows/ci.yml/badge.svg?branch=master\n    :target: https://github.com/PythonCharmers/python-future/actions/workflows/ci.yml?query=branch%3Amaster\n\n``python-future`` is the missing compatibility layer between Python 2 and\nPython 3. It allows you to use a single, clean Python 3.x-compatible\ncodebase to support both Python 2 and Python 3 with minimal overhead.\n\nIt provides ``future`` and ``past`` packages with backports and forward\nports of features from Python 3 and 2. It also comes with ``futurize`` and\n``pasteurize``, customized 2to3-based scripts that helps you to convert\neither Py2 or Py3 code easily to support both Python 2 and 3 in a single\nclean Py3-style codebase, module by module.\n\nThe ``python-future`` project has been downloaded over 1.7 billion times.\n\n.. _status\n\nStatus\n------\n\nThe ``python-future`` project was created in 2013 to attempt to save Python from\nthe schism of version incompatibility that was threatening to tear apart the\nlanguage (as Perl 6 contributed to the death of Perl).\n\nThat time is now past. Thanks to a huge porting effort across the Python\ncommunity, Python 3 eventually thrived. Python 2 reached its end of life in\n2020 and the ``python-future`` package should no longer be necessary. Use it to\nhelp with porting legacy code to Python 3 but don't depend on it for new code.\n\n.. _features:\n\nFeatures\n--------\n\n-   ``future.builtins`` package (also available as ``builtins`` on Py2) provides\n    backports and remappings for 20 builtins with different semantics on Py3\n    versus Py2\n\n-   support for directly importing 30 standard library modules under\n    their Python 3 names on Py2\n\n-   support for importing the other 14 refactored standard library modules\n    under their Py3 names relatively cleanly via\n    ``future.standard_library`` and ``future.moves``\n\n-   ``past.builtins`` package provides forward-ports of 19 Python 2 types and\n    builtin functions. These can aid with per-module code migrations.\n\n-   ``past.translation`` package supports transparent translation of Python 2\n    modules to Python 3 upon import. [This feature is currently in alpha.]\n\n-   1000+ unit tests, including many from the Py3.3 source tree.\n\n-   ``futurize`` and ``pasteurize`` scripts based on ``2to3`` and parts of\n    ``3to2`` and ``python-modernize``, for automatic conversion from either Py2\n    or Py3 to a clean single-source codebase compatible with Python 2.6+ and\n    Python 3.3+.\n\n-   a curated set of utility functions and decorators in ``future.utils`` and\n    ``past.utils`` selected from Py2/3 compatibility interfaces from projects\n    like ``six``, ``IPython``, ``Jinja2``, ``Django``, and ``Pandas``.\n\n-   support for the ``surrogateescape`` error handler when encoding and\n    decoding the backported ``str`` and ``bytes`` objects. [This feature is\n    currently in alpha.]\n\n-   support for pre-commit hooks\n\n.. _code-examples:\n\nCode examples\n-------------\n\nReplacements for Py2's built-in functions and types are designed to be imported\nat the top of each Python module together with Python's built-in ``__future__``\nstatements. For example, this code behaves identically on Python 2.6/2.7 after\nthese imports as it does on Python 3.3+:\n\n.. code-block:: python\n\n    from __future__ import absolute_import, division, print_function\n    from builtins import (bytes, str, open, super, range,\n                          zip, round, input, int, pow, object)\n\n    # Backported Py3 bytes object\n    b = bytes(b'ABCD')\n    assert list(b) == [65, 66, 67, 68]\n    assert repr(b) == \"b'ABCD'\"\n    # These raise TypeErrors:\n    # b + u'EFGH'\n    # bytes(b',').join([u'Fred', u'Bill'])\n\n    # Backported Py3 str object\n    s = str(u'ABCD')\n    assert s != bytes(b'ABCD')\n    assert isinstance(s.encode('utf-8'), bytes)\n    assert isinstance(b.decode('utf-8'), str)\n    assert repr(s) == \"'ABCD'\"      # consistent repr with Py3 (no u prefix)\n    # These raise TypeErrors:\n    # bytes(b'B') in s\n    # s.find(bytes(b'A'))\n\n    # Extra arguments for the open() function\n    f = open('japanese.txt', encoding='utf-8', errors='replace')\n\n    # New zero-argument super() function:\n    class VerboseList(list):\n        def append(self, item):\n            print('Adding an item')\n            super().append(item)\n\n    # New iterable range object with slicing support\n    for i in range(10**15)[:10]:\n        pass\n\n    # Other iterators: map, zip, filter\n    my_iter = zip(range(3), ['a', 'b', 'c'])\n    assert my_iter != list(my_iter)\n\n    # The round() function behaves as it does in Python 3, using\n    # \"Banker's Rounding\" to the nearest even last digit:\n    assert round(0.1250, 2) == 0.12\n\n    # input() replaces Py2's raw_input() (with no eval()):\n    name = input('What is your name? ')\n    print('Hello ' + name)\n\n    # pow() supports fractional exponents of negative numbers like in Py3:\n    z = pow(-1, 0.5)\n\n    # Compatible output from isinstance() across Py2/3:\n    assert isinstance(2**64, int)        # long integers\n    assert isinstance(u'blah', str)\n    assert isinstance('blah', str)       # only if unicode_literals is in effect\n\n    # Py3-style iterators written as new-style classes (subclasses of\n    # future.types.newobject) are automatically backward compatible with Py2:\n    class Upper(object):\n        def __init__(self, iterable):\n            self._iter = iter(iterable)\n        def __next__(self):                 # note the Py3 interface\n            return next(self._iter).upper()\n        def __iter__(self):\n            return self\n    assert list(Upper('hello')) == list('HELLO')\n\n\nThere is also support for renamed standard library modules. The recommended\ninterface works like this:\n\n.. code-block:: python\n\n    # Many Py3 module names are supported directly on both Py2.x and 3.x:\n    from http.client import HttpConnection\n    import html.parser\n    import queue\n    import xmlrpc.client\n\n    # Refactored modules with clashing names on Py2 and Py3 are supported\n    # as follows:\n    from future import standard_library\n    standard_library.install_aliases()\n\n    # Then, for example:\n    from itertools import filterfalse, zip_longest\n    from urllib.request import urlopen\n    from collections import ChainMap\n    from collections import UserDict, UserList, UserString\n    from subprocess import getoutput, getstatusoutput\n    from collections import Counter, OrderedDict   # backported to Py2.6\n\n\nAutomatic conversion to Py2/3-compatible code\n---------------------------------------------\n\n``python-future`` comes with two scripts called ``futurize`` and\n``pasteurize`` to aid in making Python 2 code or Python 3 code compatible with\nboth platforms (Py2/3). It is based on 2to3 and uses fixers from ``lib2to3``,\n``lib3to2``, and ``python-modernize``, as well as custom fixers.\n\n``futurize`` passes Python 2 code through all the appropriate fixers to turn it\ninto valid Python 3 code, and then adds ``__future__`` and ``future`` package\nimports so that it also runs under Python 2.\n\nFor conversions from Python 3 code to Py2/3, use the ``pasteurize`` script\ninstead. This converts Py3-only constructs (e.g. new metaclass syntax) to\nPy2/3 compatible constructs and adds ``__future__`` and ``future`` imports to\nthe top of each module.\n\nIn both cases, the result should be relatively clean Py3-style code that runs\nmostly unchanged on both Python 2 and Python 3.\n\nFuturize: 2 to both\n~~~~~~~~~~~~~~~~~~~\n\nFor example, running ``futurize -w mymodule.py`` turns this Python 2 code:\n\n.. code-block:: python\n\n    import Queue\n    from urllib2 import urlopen\n\n    def greet(name):\n        print 'Hello',\n        print name\n\n    print \"What's your name?\",\n    name = raw_input()\n    greet(name)\n\ninto this code which runs on both Py2 and Py3:\n\n.. code-block:: python\n\n    from __future__ import print_function\n    from future import standard_library\n    standard_library.install_aliases()\n    from builtins import input\n    import queue\n    from urllib.request import urlopen\n\n    def greet(name):\n        print('Hello', end=' ')\n        print(name)\n\n    print(\"What's your name?\", end=' ')\n    name = input()\n    greet(name)\n\nThe first four lines have no effect under Python 3 and can be removed from\nthe codebase when Python 2 compatibility is no longer required.\n\nSee :ref:`forwards-conversion` and :ref:`backwards-conversion` for more details.\n\n\nAutomatic translation\n~~~~~~~~~~~~~~~~~~~~~\n\nThe ``past`` package can automatically translate some simple Python 2\nmodules to Python 3 upon import. The goal is to support the \"long tail\" of\nreal-world Python 2 modules (e.g. on PyPI) that have not been ported yet. For\nexample, here is how to use a Python 2-only package called ``plotrique`` on\nPython 3. First install it:\n\n.. code-block:: bash\n\n    $ pip3 install plotrique==0.2.5-7 --no-compile   # to ignore SyntaxErrors\n\n(or use ``pip`` if this points to your Py3 environment.)\n\nThen pass a whitelist of module name prefixes to the ``autotranslate()`` function.\nExample:\n\n.. code-block:: bash\n\n    $ python3\n\n    \u003e\u003e\u003e from past.translation import autotranslate\n    \u003e\u003e\u003e autotranslate(['plotrique'])\n    \u003e\u003e\u003e import plotrique\n\nThis transparently translates and runs the ``plotrique`` module and any\nsubmodules in the ``plotrique`` package that ``plotrique`` imports.\n\nThis is intended to help you migrate to Python 3 without the need for all\nyour code's dependencies to support Python 3 yet. It should be used as a\nlast resort; ideally Python 2-only dependencies should be ported\nproperly to a Python 2/3 compatible codebase using a tool like\n``futurize`` and the changes should be pushed to the upstream project.\n\nNote: the auto-translation feature is still in alpha; it needs more testing and\ndevelopment, and will likely never be perfect.\n\n\nPre-commit hooks\n~~~~~~~~~~~~~~~~\n\n`Pre-commit \u003chttps://pre-commit.com/\u003e`_ is a framework for managing and maintaining\nmulti-language pre-commit hooks.\n\nIn case you need to port your project from Python 2 to Python 3, you might consider\nusing such hook during the transition period.\n\nFirst:\n\n.. code-block:: bash\n\n    $ pip install pre-commit\n\nand then in your project's directory:\n\n.. code-block:: bash\n\n    $ pre-commit install\n\nNext, you need to add this entry to your ``.pre-commit-config.yaml``\n\n.. code-block:: yaml\n\n    -   repo: https://github.com/PythonCharmers/python-future\n        rev: master\n        hooks:\n            - id: futurize\n              args: [--both-stages]\n\nThe ``args`` part is optional, by default only stage1 is applied.\n\nLicensing\n---------\n\n:Author:  Ed Schofield, Jordan M. Adler, et al\n\n:Copyright: 2013-2024 Python Charmers, Australia.\n\n:Sponsors: Python Charmers: https://pythoncharmers.com\n\n           Pinterest https://opensource.pinterest.com\n\n:Licence: MIT. See ``LICENSE.txt`` or `here \u003chttps://python-future.org/credits.html\u003e`_.\n\n:Other credits:  See `here \u003chttps://python-future.org/credits.html\u003e`_.\n\nDocs\n----\nSee the docs `here \u003chttps://python-future.org\u003e`_.\n\nNext steps\n----------\n\nIf you are new to Python-Future, check out the `Quickstart Guide\n\u003chttps://python-future.org/quickstart.html\u003e`_.\n\nFor an update on changes in the latest version, see the `What's New\n\u003chttps://python-future.org/whatsnew.html\u003e`_ page.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPythonCharmers%2Fpython-future","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPythonCharmers%2Fpython-future","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPythonCharmers%2Fpython-future/lists"}