Skip to content

Assume dict is ordered - #2518

Merged
peterjc merged 3 commits into
biopython:masterfrom
peterjc:assume_ordered_dict
Jan 10, 2020
Merged

peterjc merged 3 commits into
biopython:masterfrom
peterjc:assume_ordered_dict

Conversation

@peterjc

@peterjc peterjc commented Jan 9, 2020

Copy link
Copy Markdown
Member

Partly addresses #2490, where this was discussed.

Ordered dictionaries are a language feature in Python 3.7 onwards, but we currently support Python 3.6 onwards.

Thankfully the only major implementations of Python 3.6 we are aware of (CPython and PyPy) both implement ordered dictionaries.

  • 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.)

This is a language feature in Python 3.7 onwards.

This is behaviour in CPython 3.6 and the PyPy implementation
of Python 3.6.

We are not currently aware of any other Python 3.6 implementation
(where dictionaries might not be sorted), so this is simpler now
that we require at least Python 3.6 for Biopython.
@chris-rands

Copy link
Copy Markdown
Contributor

Is it worth updating the docstrings? e.g.

Since Python 3.7, the default dict class maintains key order, meaning
this dictionary will reflect the order of records given to it. For
CPython, this was already implemented in 3.6.
As of Biopython 1.73, we explicitly use OrderedDict for CPython older
than 3.6 (and for other Python older than 3.7) so that you can always
assume the record order is preserved.

Could also add a note about the equality testing I mentioned in the issue (order sensitive with OrderedDict vs. order insensitive with regular python dicts), since this could lead to a bug if someone does not expect the different behaviours

Thanks Chris Rands, spotted in review.
@codecov

codecov Bot commented Jan 9, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #2518 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2518      +/-   ##
==========================================
+ Coverage   84.76%   84.77%   +<.01%     
==========================================
  Files         321      321              
  Lines       52381    52368      -13     
==========================================
- Hits        44403    44394       -9     
+ Misses       7978     7974       -4
Impacted Files Coverage Δ
Bio/File.py 85.29% <100%> (+0.29%) ⬆️
Bio/SeqIO/__init__.py 84.18% <100%> (+0.29%) ⬆️
Bio/SearchIO/__init__.py 89.33% <100%> (+0.72%) ⬆️

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 57635d2...3adc761. Read the comment docs.

@peterjc

peterjc commented Jan 9, 2020

Copy link
Copy Markdown
Member Author

Good spot @chris-rands - How's this for the docstring? Or simplify further, and just say record order is preserved in the dictionary?

@chris-rands

Copy link
Copy Markdown
Contributor

Looks good, the docstring similar in SearchIO:

Since Python 3.7, the default dict class maintains key order, meaning
this dictionary will reflect the order of records given to it. For
CPython, this was already implemented in 3.6.
As of Biopython 1.73, we explicitly use OrderedDict for CPython older
than 3.6 (and for other Python older than 3.7) so that you can always
assume the record order is preserved.

Again, spotted by Chris Rands in review
@peterjc
peterjc merged commit 2812869 into biopython:master Jan 10, 2020
@peterjc
peterjc deleted the assume_ordered_dict branch January 10, 2020 10:30
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.

2 participants