Add Python 2.7 support

Bug #1726399 reported by Victor Stinner
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
ospurge
New
Undecided
Unassigned

Bug Description

While Python 3 is obviously the future, Python 2 remains widely used. To ease the transition to Python 3, I would like to add back the Python 2.7 support to ospurge. It would prevent to have to maintain a private fork, write a new project from scratch, etc.

I talked with Jordan Pittier who agrees to add back Python 2 support. He told me that another company already asked him the same.

Jordan seems to even agree to completely drop type annotations. I'm not confortable with that, but I'm not used to annotations, and I don't think that it's completely possible to annotate types in comments in Python 2. Maybe we should the other approach, write annotations in a completely different .pyi file.

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix proposed to ospurge (master)

Related fix proposed to branch: master
Review: https://review.openstack.org/514315

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Related fix proposed to branch: master
Review: https://review.openstack.org/514318

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Related fix proposed to branch: master
Review: https://review.openstack.org/514319

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Related fix proposed to branch: master
Review: https://review.openstack.org/514320

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Related fix proposed to branch: master
Review: https://review.openstack.org/514336

Revision history for this message
Victor Stinner (vstinner) wrote :

For type annotations, I wrote https://review.openstack.org/514336 which moves type annotations to new .pyi files to keep them.

Revision history for this message
Victor Stinner (vstinner) wrote :

I finished to published my patches.

If all patches are merged, all tests will pass on Python 2.7... except TestOrderedMeta.

I chose to skip TestOrderedMeta with:

+@unittest.skipIf(six.PY2,
+ "Python2 doesn't support metaclass defining __prepare__")
 @mock.patch('logging.warning', mock.Mock(side_effect=WrongMethodDefOrder))
 class TestOrderedMeta(unittest.TestCase):

I included this change by mistake in https://review.openstack.org/#/c/514319/. Jordan Pittier commented:

"I agree with this approach, let's be pragmatic here. I wanted to play with __prepare__, but let's not shoot ourself in the foot with this."

Once pending patches will be merged, I will write the final patch skipping TestOrderedMeta with tox.init and setup.py to get a working "tox -e py27".

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Related fix proposed to branch: master
Review: https://review.openstack.org/518722

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix merged to ospurge (master)

Reviewed: https://review.openstack.org/514320
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=67fc801e699d8b19031b5b91c30017e6541a61d7
Submitter: Zuul
Branch: master

commit 67fc801e699d8b19031b5b91c30017e6541a61d7
Author: Victor Stinner <email address hidden>
Date: Mon Oct 23 15:46:36 2017 +0200

    Python2: Use __name__ rather than __qualname__

    The __qualname__ attribute was added in Python 3. Falls back on
    __name__ for Python 2 compatibility, when __qualname__ is not
    available.

    Related-Bug: 1726399
    Change-Id: I19d07dc978250e5340327f05ea6ab62ac55489ed

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Reviewed: https://review.openstack.org/514315
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=c2d1728deedf7e2ca03e6a55f4de59d4d3ab1dde
Submitter: Zuul
Branch: master

commit c2d1728deedf7e2ca03e6a55f4de59d4d3ab1dde
Author: Victor Stinner <email address hidden>
Date: Mon Oct 23 15:26:07 2017 +0200

    Python2: Add SimpleNamespace for Python 2

    types.SimpleNamespace was added in Python 3.3. Reimplement it in 3
    lignes to add compatibility with Python 2.7.

    Related-Bug: 1726399
    Change-Id: I1553a39ecbf315b5e89d3ec8070b15769cabb8d2

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Reviewed: https://review.openstack.org/518722
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=50560fc91cb880b7065671a28e945242b4797998
Submitter: Zuul
Branch: master

commit 50560fc91cb880b7065671a28e945242b4797998
Author: Victor Stinner <email address hidden>
Date: Thu Nov 9 13:29:23 2017 +0100

    Python2: Add funcsigs dependency

    inspect.signature() was added to Python 3.3: add funcsigs on Python
    2.7.

    funcsigs dependency is specific to Python 2.7 and so not installed on
    Python 3.

    Change-Id: I951e7df9fcebdc2148cca477f4b064fc9a028e55
    Related-Bug: 1726399

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Reviewed: https://review.openstack.org/514318
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=17162e650699a92c1a510cac572835aa188d5de2
Submitter: Zuul
Branch: master

commit 17162e650699a92c1a510cac572835aa188d5de2
Author: Victor Stinner <email address hidden>
Date: Mon Oct 23 15:26:27 2017 +0200

    Python2: Add mock and unittest2 test dependencies

    * unittest.mock was added in Python 3.3 standard library: use
      external 'mock' module, backport for Python 2.7
    * assertRaisesRegex() method was added to Python 3.2 unittest module:
      use external 'unitttest2' backport for Python 2.7 to not have to
      rewrite unit tests just because of Python 2.7

    Both added dependencies are specific to Python 2.7 and have no impact
    on Python 3.

    Related-Bug: 1726399
    Change-Id: Ieb32e85d6f40f9b7bed5ba99900be2432d18da9e

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Reviewed: https://review.openstack.org/514336
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=b73c938a93dc90839515df1dae285e9dd492aca9
Submitter: Zuul
Branch: master

commit b73c938a93dc90839515df1dae285e9dd492aca9
Author: Victor Stinner <email address hidden>
Date: Mon Oct 23 16:15:22 2017 +0200

    Python 2: Move type annotations to .pyi files

    Python 2 doesn't support type annotations: raise SyntaxError. Move
    them to separated .pyi files to add Python 2 support without loosing
    annotations.

    Keep inline comments like "# type: Optional[str]".

    Related-Bug: 1726399
    Change-Id: Ib1a837e88cf76908f1007b3881703ffb433e6c2f

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix proposed to ospurge (master)

Related fix proposed to branch: master
Review: https://review.openstack.org/518953

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Related fix merged to ospurge (master)

Reviewed: https://review.openstack.org/514319
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=c1d38f741bafd4c27fd525aff79e787e78e594f2
Submitter: Zuul
Branch: master

commit c1d38f741bafd4c27fd525aff79e787e78e594f2
Author: Victor Stinner <email address hidden>
Date: Mon Oct 23 15:36:32 2017 +0200

    Python2: Fix metaclass, super() and OrderedMeta

    * Add six dependency
    * Replace "metaclass=" syntax with six.with_metaclass()
    * Add parameters to super()
    * Only define OrderedMeta on Python 3

    Related-Bug: 1726399
    Change-Id: I21800320ef23cc95fc07278864e73b64bb7d6d08

Revision history for this message
OpenStack Infra (hudson-openstack) wrote :

Reviewed: https://review.openstack.org/518953
Committed: https://git.openstack.org/cgit/openstack/ospurge/commit/?id=af88d16028e2e266a62028fe1927c61d80c68b8c
Submitter: Zuul
Branch: master

commit af88d16028e2e266a62028fe1927c61d80c68b8c
Author: Victor Stinner <email address hidden>
Date: Fri Nov 10 16:40:45 2017 +0100

    Python2: Fix tox -e py27

    With this change, unit tests now pass on Python 2.7: tox -e py27.

    Changes:

    * Replace "yield from ..." with "for item in ...: yield item"
    * Remove last annotations from utils.py
    * tox.ini: use python3 as basepython rather than python3.5 to support
      Python 2.7 and Python 3.6
    * tox.ini: add py27 to envlist

    Related-Bug: 1726399
    Change-Id: Ief1a95402e8a31a34ada2937a7ae6a604f7f8757

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.