Skip to content

Remove/refactor redundant sys.version_info checks #2490

Closed

Description

@chris-rands

As part of the upgrade to support Python 3.6+ only, many checks for sys.version_info are no longer required. Here is an approximate list:

$ grep "version_info" Bio/*.py Bio/*/*.py Bio/*/*/*.py Tests/*py 
Bio/File.py:if (sys.version_info >= (3, 7)
Bio/File.py:        or (sys.version_info == (3, 6)
Bio/File.py:        if sys.version_info[0] >= 3 and "U" in mode:
Bio/Seq.py:    if sys.version_info[0] == 3:
Bio/Seq.py:        if sys.version_info[0] == 3:
Bio/SeqRecord.py:            if sys.version_info[0] < 3:
Bio/bgzf.py:    if sys.version_info[0] >= 3:
Bio/Phylo/BaseTree.py:if sys.version_info[0] < 3:
Bio/SearchIO/__init__.py:if sys.version_info < (3, 6):
Bio/SearchIO/__init__.py:    if format == "blast-xml" and sys.version_info[0] > 2:
Bio/SeqIO/__init__.py:if sys.version_info < (3, 6):
Tests/common_BioSQL.py:        if sys.version_info[0] == 2:
Tests/test_Phylo.py:        if sys.version_info[0] < 3:
Tests/test_Phylo.py:            if sys.version_info[0] < 3:
Tests/test_SeqIO_SeqXML.py:    if not (sys.platform == "win32" and sys.version_info[0] >= 3):
Tests/test_SeqIO_index.py:    if sys.version_info[0] < 3 or format in SeqIO._BinaryFormats:
Tests/test_SeqRecord.py:    if sys.version_info[0] >= 3:
Tests/test_Seq_objs.py:if sys.version_info[0] < 3:
Tests/test_SffIO.py:        if sys.platform != "win32" and sys.version_info[0] < 3:
Tests/test_geo.py:if sys.version_info[0] >= 3:
Tests/test_pairwise_aligner.py:        if sys.version_info[0] >= 3:

I suggest tagging this with "Good first issue" and "Help wanted"

Note, the first case sys.version_info >= (3, 7) should be kept for now (although a separate discussion might be needed on whether or not it is safe to assume regular dicts retain insertion order from Python 3.6 onwards)

Activity

  1. added
    good first issue
    on Jan 4, 2020
  2. mdehoon commented on Jan 4, 2020

    @mdehoon
    Contributor

    Tests/test_pairwise_aligner.py is being fixed in #2491 .

    I would suggest to make separate pull requests for each file.

  3. lorforlinux commented on Jan 6, 2020

    @lorforlinux
    Contributor

    Following files are being fixed (in #2496):

    • Test/test_Phylo.py
    • Test/test_SeqIO_index.py
    • Test/test_SffIO.py
    • Test/test_SeqRecord.py
    • Test/common_BioSQL.py
    • Test/test_geo.py
    • Test/test_Seq_objs.py

    test_SeqIO_SeqXML.py requires special attention @peterjc @mdehoon

  4. nimne commented on Jan 6, 2020

    @nimne
    Contributor

    @chris-rands From the python mailing list, maintaining the insertion order of a dict is a language feature starting from 3.7. While it was a side-effect of a performance optimization in 3.6 (and wasn't recommended to be relied on), maintaining insertion order should be safe to assume in 3.7 and up.

  5. peterjc commented on Jan 6, 2020

    @peterjc
    Member

    @nimne yes, that's what the code in question does - it falls back on OrderedDict under Python 3.6 when not using CPython or PyPy:

    https://github.com/biopython/biopython/blob/biopython-176/Bio/File.py#L34

    Arguably we could say since the only Python implementations of Python 3.6 in the wild are CPython and PyPy (which both use ordered dictionaries) that we don't need to worry about this and can effectively already assume ordered dicts. My expectation is that any future Python 3 implementation (e.g. an updated Jython or IronPython?) will start with ordered dictionaries.

    My inclination is to leave this bit of code as it is - sooner or later we'll drop Python 3.6 anyway.

    --

    Happily all the other cases @chris-rands flagged are a much simpler, Python 3 or at least 3.6 onwards (which is what we now are focussed on).

  6. chris-rands commented on Jan 6, 2020

    @chris-rands
    ContributorAuthor

    @peterjc @nimne Yes, with the caveat that equality comparisons are still ordered insensitive in Python 3.7 with regular dict, which leads to different behaviours:

    >>> import sys; sys.version_info[:2] 
    (3, 7)
    >>> from collections import OrderedDict
    >>> OrderedDict([(1,1), (2,2)]) == OrderedDict([(2,2), (1,1)]) 
    False
    >>> dict([(1,1), (2,2)]) == dict([(2,2), (1,1)]) 
    True
    
  7. added a commit that references this issue on Jan 7, 2020
  8. peterjc commented on Jan 7, 2020

    @peterjc
    Member

    That's an interesting subtle gotcha - although I don't think it would be an issue for the indexing code in Bio.File.

    Anyway, Bio/File.py aside, we're making steady progress:

    $ grep "version_info" */*.py Bio/*/*.py Bio/*/*/*.py 
    Bio/File.py:    sys.version_info >= (3, 7)
    Bio/File.py:    or (sys.version_info == (3, 6) and platform.python_implementation() == "CPython")
    Bio/File.py:        if sys.version_info[0] >= 3 and "U" in mode:
    Bio/bgzf.py:    if sys.version_info[0] >= 3:
    BioSQL/BioSeqDatabase.py:        if module_name == "mysql.connector" and sys.version_info[0] == 3:
    Tests/test_SeqIO_SeqXML.py:    if not (sys.platform == "win32" and sys.version_info[0] >= 3):
    Tests/test_SeqIO_index.py:    if sys.version_info[0] < 3 or format in SeqIO._BinaryFormats:
    Bio/Phylo/BaseTree.py:if sys.version_info[0] < 3:
    Bio/SearchIO/__init__.py:if sys.version_info < (3, 6):
    Bio/SearchIO/__init__.py:    if format == "blast-xml" and sys.version_info[0] > 2:
    Bio/SeqIO/__init__.py:if sys.version_info < (3, 6):
    

    Note #2499 will do Tests/test_SeqIO_SeqXML.py

  9. peterjc commented on Jan 9, 2020

    @peterjc
    Member

    I realise now that we have ordered dictionary code in three places, Bio/File.py, Bio/SearchIO/__init__.py and Bio/SeqIO/__init__.py - and they were not consistent. Thus in #2518 I propose we remove the checks and assume ordered dictionaries.

  10. peterjc commented on Jan 9, 2020

    @peterjc
    Member

    Updated list assuming #2517 and #2518 are merged:

    $ grep "version_info" */*.py Bio/*/*.py Bio/*/*/*.py 
    Bio/File.py:        if sys.version_info[0] >= 3 and "U" in mode:
    BioSQL/BioSeqDatabase.py:        if module_name == "mysql.connector" and sys.version_info[0] == 3:
    Tests/test_SeqIO_SeqXML.py:    if not (sys.platform == "win32" and sys.version_info[0] >= 3):
    Bio/Phylo/BaseTree.py:if sys.version_info[0] < 3:
    Bio/SearchIO/__init__.py:    if format == "blast-xml" and sys.version_info[0] > 2:
    

    There are four easy cases, just removing a clause from an if-statement. That would make a nice pull request (Bio/File.py, BioSQL/BioSeqDatabase.py, Tests/test_SeqIO_SeqXML.py and Bio/SearchIO/__init__.py)

    For Bio/Phylo/BaseTree.py we need to remove the as_string function definition and replace all uses of it with str instead.

  11. lorforlinux commented on Jan 9, 2020

    @lorforlinux
    Contributor

    There are four easy cases, just removing a clause from an if-statement. That would make a nice pull request

    Fixed these in #2520

    Bio/File.py:        if sys.version_info[0] >= 3 and "U" in mode:
    BioSQL/BioSeqDatabase.py:        if module_name == "mysql.connector" and sys.version_info[0] == 3:
    Tests/test_SeqIO_SeqXML.py:    if not (sys.platform == "win32" and sys.version_info[0] >= 3):
    Bio/SearchIO/__init__.py:    if format == "blast-xml" and sys.version_info[0] > 2:
    
  12. mdehoon commented on Jan 10, 2020

    @mdehoon
    Contributor

    Bio.Phylo.BaseTree in #2526

  13. peterjc commented on Jan 10, 2020

    @peterjc
    Member

    I've cherry-picked b07676d from #2517, so once #2526 is merged we can close this issue.

  14. peterjc commented on Jan 10, 2020

    @peterjc
    Member

    Closing - thank you everyone!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions