Skip to content

String and bytes functions removal - #2445

Closed
Andrey-Raspopov wants to merge 21 commits into
biopython:masterfrom
Andrey-Raspopov:2420-strings
Closed

Andrey-Raspopov wants to merge 21 commits into
biopython:masterfrom
Andrey-Raspopov:2420-strings

Conversation

@Andrey-Raspopov

Copy link
Copy Markdown
Contributor

This pull request addresses issue #2420
Note: I've decided to split this issue into several PRs, since it should be easier to review it. If that's not the case, I can first merge my remaining PRs into one.

  • I hereby agree to dual licence this and any previous contributions under both
    the Biopython License Agreement AND the BSD 3-Clause License.

  • I have read the CONTRIBUTING.rst file, have run flake8 locally, and
    understand that AppVeyor and TravisCI will be used to confirm the Biopython unit
    tests and style checks pass with these changes.

  • I have added my name to the alphabetical contributors listings in the files
    NEWS.rst and CONTRIB.rst as part of this pull request, am listed
    already, or do not wish to be listed. (This acknowledgement is optional.)

@codecov

codecov Bot commented Dec 27, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #2445 into master will decrease coverage by <.01%.
The diff coverage is 88.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2445      +/-   ##
==========================================
- Coverage   84.66%   84.65%   -0.01%     
==========================================
  Files         323      323              
  Lines       52573    52622      +49     
==========================================
+ Hits        44510    44549      +39     
- Misses       8063     8073      +10
Impacted Files Coverage Δ
Bio/File.py 84.59% <ø> (-0.41%) ⬇️
Bio/_py3k/__init__.py 40% <ø> (-1.25%) ⬇️
Bio/SearchIO/ExonerateIO/exonerate_cigar.py 74.13% <0%> (-0.44%) ⬇️
Bio/SearchIO/HmmerIO/hmmer3_text.py 99.17% <100%> (-0.01%) ⬇️
Bio/Entrez/Parser.py 82.08% <100%> (+0.02%) ⬆️
Bio/SeqIO/_index.py 93.63% <100%> (-0.02%) ⬇️
Bio/SearchIO/_index.py 100% <100%> (ø) ⬆️
Bio/SearchIO/ExonerateIO/exonerate_vulgar.py 92.3% <100%> (+0.06%) ⬆️
Bio/SearchIO/FastaIO.py 95.97% <100%> (ø) ⬆️
Bio/SeqIO/SffIO.py 86.25% <100%> (ø) ⬆️
... and 61 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 12f7276...aa552c5. Read the comment docs.

@peterjc

peterjc commented Dec 27, 2019

Copy link
Copy Markdown
Member

I'm not sure about introducing from wheel.util import as_bytes versus an internal helper function (under Bio.utils made sense as in an earlier stage of this pull request).

Currently as I recall wheel is a build dependency (for making wheels), but not a Biopython run time dependency.

@Andrey-Raspopov

Copy link
Copy Markdown
Contributor Author

Okay, I'll move it back to _utils then.

@peterjc

peterjc commented Dec 27, 2019

Copy link
Copy Markdown
Member

It looks like Python 3.6 on TravisCI was failing due to this - perhaps the version of wheel is important:

Traceback (most recent call last):
  File "/opt/python/3.6.7/lib/python3.6/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "/home/travis/build/biopython/biopython/Tests/test_BioSQL_MySQLdb.py", line 11, in <module>
    from common_BioSQL import *  # noqa: F403
  File "/home/travis/build/biopython/biopython/Tests/common_BioSQL.py", line 27, in <module>
    from Bio import SeqIO
  File "/home/travis/build/biopython/biopython/build/lib.linux-x86_64-3.6/Bio/SeqIO/__init__.py", line 401, in <module>
    from . import SffIO
  File "/home/travis/build/biopython/biopython/build/lib.linux-x86_64-3.6/Bio/SeqIO/SffIO.py", line 231, in <module>
    from wheel.util import as_bytes
ImportError: cannot import name 'as_bytes'

Comment thread Bio/Entrez/Parser.py Outdated
from Bio._utils import as_bytes

handle = BytesIO(_as_bytes(handle.read()))
handle = BytesIO(as_bytes(handle.read()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why we need this; users should pass a BytesIO object, not a StringIO object to the read function. I can't think of a scenario in which the user /has/ to pass a StringIO object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A common use case would be user saved an XML file to disk, and just did open(xml_filename) which would be a unicode handle now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then Entrez.read should raise an Exception. We should not be second-guessing the user.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it fails with a clear message, that's fine by me. I would lean to doing this in a follow up pull request so as not to delay this one editing many files.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this test in test_Entrez_parser.py:

    def test_text_handle(self):
        """Test parsing a handle opened in text mode."""
        with open("Entrez/einfo1.xml", "rt") as handle:
            record = Entrez.read(handle)
        self.assertIn("DbList", record)

or this one:

    def test_StringIO(self):
        """Test parsing a StringIO handle (unicode not bytes)."""
        with open("Entrez/einfo1.xml", "rt") as in_handle:
            data = in_handle.read()
        handle = StringIO(data)
        record = Entrez.read(handle)
        self.assertIn("DbList", record)
        handle.close()

and the documentation has this example:

>>> from Bio import Entrez
>>> Entrez.email = "[email protected]"  # Always tell NCBI who you are
>>> handle = open("Homo_sapiens.xml")
>>> records = Entrez.parse(handle)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All those examples open the file in text mode, giving unicode string handles - not bytes handles.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. These should all be opening the file in binary mode. The call to _as_bytes obscures this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we leave this as is for now, merge this pull request, and then let you tackle this? It seems a bit much to ask @Andrey-Raspopov to do it here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed version >= 3, since it'll be >= 3 anyway. Also I've changed initial commentary to TODO, since it definitely looks like a work for another PR, but also it looks like a work that should be done. I can do that after we are finished with py3k or I can leave to it to @mdehoon.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can handle Bio.Entrez but please discard all changes to Bio.Entrez (and any other module still under discussion) from this pull request.

Comment thread Bio/KEGG/KGML/KGML_pathway.py Resolved
Comment thread Tests/test_bgzf.py Resolved
Comment thread Bio/SearchIO/BlastIO/blast_tab.py Resolved
Comment thread Bio/SearchIO/BlastIO/blast_xml.py Resolved
@peterjc

peterjc commented Dec 27, 2019

Copy link
Copy Markdown
Member

As @mdehoon points out, almost all the old occurrences of _as_bytes(...) can be avoided with bytes literals. That leaves just a couple where I think an explicit encode would be enough?

@mdehoon

mdehoon commented Dec 27, 2019

Copy link
Copy Markdown
Contributor

Yes using encode explicitly would be better.

@Andrey-Raspopov

Andrey-Raspopov commented Dec 27, 2019 •

Copy link
Copy Markdown
Contributor Author

But using encode on byte string won't work, so as far as I understand we can change this type of behaviour only when we know that we expect string, not bytestring? Also, original as_bytes uses latin_1 and will fail on other literals, what kind of behaviour do we want to have here?

@peterjc

peterjc commented Dec 27, 2019

Copy link
Copy Markdown
Member

Yes, the original as_bytes function was a bit lazy as it would accept bytes or unicode and given bytes - helpful for dealing with user input especially in the early days of Python 3 porting when these conventions were still very new to everyone.

There are only a few uses left (most were replaced by bytes literals), so we ought to be able to determine the expected input.

(This is probably the hardest part of the whole Bio._py3k removal work - certainly it was the main headache in porting from Python 2 to 3).

Comment thread Bio/SearchIO/HmmerIO/hmmer2_text.py Resolved
Harmitage added 3 commits December 27, 2019 23:59
…2420-strings

� Conflicts:
�	Bio/SearchIO/BlatIO.py
�	Bio/SearchIO/ExonerateIO/exonerate_text.py
�	Bio/SearchIO/ExonerateIO/exonerate_vulgar.py
�	Bio/SeqIO/AbiIO.py
�	Bio/_py3k/__init__.py
�	Bio/bgzf.py
@peterjc peterjc mentioned this pull request Dec 28, 2019
3 tasks done
peterjc pushed a commit to peterjc/biopython that referenced this pull request Dec 28, 2019
This usage dates back to when Biopython
supported Python 2.5 which did not allow
the simpler b"example" syntax.

Squashed commit of changes cherry-picked
from pull request biopython#2445.
@peterjc peterjc mentioned this pull request Dec 28, 2019
3 tasks done
peterjc pushed a commit that referenced this pull request Dec 29, 2019
This usage dates back to when Biopython
supported Python 2.5 which did not allow
the simpler b"example" syntax.

Squashed commit of changes cherry-picked
from pull request #2445.
@peterjc

peterjc commented Dec 29, 2019

Copy link
Copy Markdown
Member

Do you want me to squash this to a single commit and rebase it against the current master to deal with the conflicts, and make it easier to see where things stand?

@Andrey-Raspopov

Copy link
Copy Markdown
Contributor Author

Yes, that would be good, I'll get back to this PR tomorrow morning.

@peterjc

peterjc commented Dec 29, 2019

Copy link
Copy Markdown
Member

I've cherry-picked a couple more small edits to the master.

I think that it might be best to start again - tackling things like Bio/KEGG/KGML/KGML_pathway.py as in your most recent commit?

This time don't add or move anything to Bio._utils, just focus on removing code using Bio._py3k?

@Andrey-Raspopov

Copy link
Copy Markdown
Contributor Author

Okay, I'll delete this branch then and will make new PR, much less messy:)

@bow bow mentioned this pull request Dec 30, 2019
3 tasks done
@Andrey-Raspopov
Andrey-Raspopov deleted the 2420-strings branch January 22, 2020 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants