Skip to content

Change traceback as suggested to improve error message in Bio.Align - #2007

Merged
peterjc merged 6 commits into
biopython:masterfrom
chris-rands:issue1991
May 8, 2019
Merged

peterjc merged 6 commits into
biopython:masterfrom
chris-rands:issue1991

Conversation

@chris-rands

Copy link
Copy Markdown
Contributor

This pull request addresses issue #1991

  • 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 and understand that AppVeyor and
    TravisCI will be used to confirm the Biopython unit tests and flake8 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.)

@peterjc peterjc left a comment

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.

Looks good to me, over to @mdehoon as the original issue author & code author.

@codecov

codecov Bot commented Apr 25, 2019

Copy link
Copy Markdown

Codecov Report

Merging #2007 into master will decrease coverage by <.01%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2007      +/-   ##
==========================================
- Coverage   85.07%   85.07%   -0.01%     
==========================================
  Files         312      312              
  Lines       50161    50161              
==========================================
- Hits        42673    42672       -1     
- Misses       7488     7489       +1
Impacted Files Coverage Δ
Bio/Align/__init__.py 86.35% <0%> (ø) ⬆️
Bio/Graphics/BasicChromosome.py 93.71% <0%> (-0.32%) ⬇️

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 efbb646...5038220. Read the comment docs.

@codecov

codecov Bot commented Apr 25, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #2007 into master will decrease coverage by 0.14%.
The diff coverage is 83.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2007      +/-   ##
==========================================
- Coverage   85.07%   84.92%   -0.15%     
==========================================
  Files         312      315       +3     
  Lines       50161    50547     +386     
==========================================
+ Hits        42673    42929     +256     
- Misses       7488     7618     +130
Impacted Files Coverage Δ
Bio/Align/__init__.py 87.4% <100%> (+1.04%) ⬆️
Bio/__init__.py 94.73% <80%> (-5.27%) ⬇️
Bio/ExPASy/__init__.py 53.84% <0%> (-3.3%) ⬇️
Bio/KEGG/__init__.py 89.36% <0%> (-1.55%) ⬇️
Bio/Cluster/__init__.py 80.66% <0%> (-1.53%) ⬇️
Bio/Graphics/BasicChromosome.py 93.71% <0%> (-0.32%) ⬇️
Bio/KEGG/Gene/__init__.py 81.94% <0%> (-0.25%) ⬇️
Bio/KEGG/Compound/__init__.py 90.47% <0%> (-0.12%) ⬇️
Bio/KEGG/Enzyme/__init__.py 83.52% <0%> (-0.1%) ⬇️
Bio/SubsMat/FreqTable.py 97.36% <0%> (ø) ⬆️
... and 31 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 efbb646...f47cc9c. Read the comment docs.

Comment thread Bio/__init__.py Resolved
@chris-rands

Copy link
Copy Markdown
Contributor Author

I implemented the suggestion to parse setup.py. We could also add a unit test to check that the first line of setup.py isn't changed if you like?

I feel this is sufficient, or should checks for Tests, Doc and Scripts directories also be added?

@peterjc

peterjc commented Apr 26, 2019

Copy link
Copy Markdown
Member

Looking at this fresh, the warning code will be in .../Bio/__init__.py, sister files or folders of interest (relative to this file):

  • ../BioSQL/ - should be there, unless eg on Debian and this sub-package not installed
  • ../setup.py or ../Tests/ or ../Doc/ or ../Scripts - sign we're in a source folder, bad, give warning
  • ../biopython-${VER}.dist-info or biopython-${VER}-${PYVER}.egg-info etc - should be installed somewhere, all fine. However details depend on Python version and how installed.

On reflection, I think probably just issue the warning if ../setup.py is present is enough?

@mdehoon

mdehoon commented Apr 27, 2019 •

Copy link
Copy Markdown
Contributor

I would suggest to parse PKG-INFO instead of setup.py.
It can be as simple as

_parent_dir = os.path.dirname(os.path.dirname(__file__))
path = os.path.join(_parent_dir, "PKG-INFO")
if os.path.exists(path):
    from email.parser import Parser
    contents = Parser().parse(open(path))
    if contents['Name'] == 'biopython':
        # issue a warning

@chris-rands

Copy link
Copy Markdown
Contributor Author

Thanks mdehoon, but what confuses me is the location of PKG-INFO on my system is different?

source:

/path/biopython/biopython.egg-info/PKG-INFO

and:

/usr/lib/python3.X/site-packages/biopython-1.X-py3.X.egg-info/PKG-INFO

@MarkusPiotrowski

MarkusPiotrowski commented Apr 27, 2019 •

Copy link
Copy Markdown
Contributor

I have two alternative suggestions, which are easy to check and should decrease the overhead:

  1. The 'ugly' alternative (easy)
    We just add a dummy file into the source tree with a less common name, e.g. Biopython_source_tree_indicator.txt (you name it) , may write something into it ('This file is just to indicate that we are in the Biopython source tree. Don't copy this file to your Python tree.') and check for the presence of this file.
  2. The less ugly alternative (won't call it 'elegant')
    Technically, I don't know how this can be accomplished (never installed from source), but is it possible to patch Bio.__init__ during the installation process to change __version__ from something like 1.74_final_dev0 to 1.74? E.g. copy the 'correct' file from somewhere else during installation? Then we could easily check like if __version__.endswith('dev0') . This would already work for our dev branch.

@chris-rands

Copy link
Copy Markdown
Contributor Author

I like Markus's ideas. If 2. is possible then it might be cleaner to create a new boolean variable like Bio.__in_source_tree__ that is set to False during installation

@mdehoon

mdehoon commented Apr 27, 2019

Copy link
Copy Markdown
Contributor

@chris-rands I meant the PKG-INFO file in the source tree. It is always in the same place. If you want to be super-precise, you can even check the version number in PKG-INFO and compare it to the version number in Bio/__init__.py.

@MarkusPiotrowski I believe the PKG-INFO file in the source tree already fulfills your option 1.

@peterjc

peterjc commented Apr 29, 2019

Copy link
Copy Markdown
Member

The PKG-INFO standard is documented as PEP314,
https://www.python.org/dev/peps/pep-0314/

Note that biopython.egg-info/PKG-INFO will not exist on a clean git checkout, and just now pip3 install . did not make this folder at all despite a successful installation. Perhaps it depends on python setup.py ... instead?

@peterjc

peterjc commented Apr 29, 2019

Copy link
Copy Markdown
Member

I'm not keen on idea 2 as written - we want our version number to be valid both for official releases and installations from git - including installing in developer mode (pip install -e . or its older equivalent with setuptools). Not saying it can't be done, but it sounds overly complicated for minimal benefit.

https://www.python.org/dev/peps/pep-0440/

Are there practical problems with just checking for ../setup.py relative to the Bio/__init__.py being executed?

@chris-rands

Copy link
Copy Markdown
Contributor Author

I'm okay with your suggestion Peter, but one last suggestion: Could we implement what numpy does? (I'm not sure exactly how to emulate this myself though)

They create a file (called __config__.py) during installation (via setup.py). And then try to import from this file in numpy/__init__.py., raising an error (warning in our case) when the import fails.

Source:

https://github.com/numpy/numpy/blob/529ee89447f03d007498cb2997eec9c93eae83fe/numpy/setup.py#L24

https://github.com/numpy/numpy/blob/529ee89447f03d007498cb2997eec9c93eae83fe/numpy/__init__.py#L164

Comment thread Bio/__init__.py Outdated

# biopython setup.py file present in parent directory
warnings.warn("You may be importing Bio/ from inside the source tree."
" This is bad practise and might lead to downstream issues."

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.

Should be practice?

@peterjc

peterjc commented May 6, 2019

Copy link
Copy Markdown
Member

@chris-rands copying what NumPy does is a lot more attractive than rolling our own 'hack', but I'm not convinced we need this complexity.

Shall we try the setup.py only check for a first iteration?

@chris-rands

Copy link
Copy Markdown
Contributor Author

@peterjc Ok sure, should we check only for the existence of setup.py or also the file contents? (and if the latter, then how)

@peterjc

peterjc commented May 6, 2019

Copy link
Copy Markdown
Member

As per #2007 (comment) I think just issue the warning if ../setup.py is present (without checking the file contents) is enough, but would not block the PR as it stands (also looking inside the setup file).

@MarkusPiotrowski @mdehoon - your thoughts?

@MarkusPiotrowski

Copy link
Copy Markdown
Contributor

Agree that just looking for setup.py without checking content should suffice.

@mdehoon

mdehoon commented May 7, 2019

Copy link
Copy Markdown
Contributor

I think it's OK to just check for the presence of setup.py.

@chris-rands

Copy link
Copy Markdown
Contributor Author

It seems everyone is satisfied- @peterjc i updated the PR- let me know if anything else is needed before proceeding with merging thanks

@peterjc
peterjc merged commit c7e9f09 into biopython:master May 8, 2019
@peterjc

peterjc commented May 8, 2019

Copy link
Copy Markdown
Member

Excellent - This PR took more discussion than usual, but I think that was wise given the potential impact of this change at the top level import. Thank you all.

@chris-rands

chris-rands commented May 9, 2019 •

Copy link
Copy Markdown
Contributor Author

Just for completeness, some timings importing from outside the source tree:

on original branch without warning:

$ python3 -m timeit 'import Bio'
2000000 loops, best of 5: 147 nsec per loop
$ python3 -m timeit 'import Bio'
2000000 loops, best of 5: 144 nsec per loop

on the new branch with warning:

$ python3 -m timeit 'import Bio'
2000000 loops, best of 5: 153 nsec per loop
$ python3 -m timeit 'import Bio'
2000000 loops, best of 5: 144 nsec per loop

Perhaps someone else would like to run the same tests to be sure, but the overhead of checking for setup.py seems negligible (unless there is some caching or something going on).

I suggest to close #1991

@peterjc

peterjc commented May 9, 2019

Copy link
Copy Markdown
Member

There likely is going to be caching at the file system level, but it looks like anyone doing lots of small Biopython jobs (i.e. calling a short script many times - the worst case I would think) won't seem much if any impact. So that's good. Thank you!

svalqui pushed a commit to svalqui/biopython that referenced this pull request May 14, 2019
Also changes traceback in Bio.Align etc when C imports fail.
@chris-rands
chris-rands deleted the issue1991 branch May 20, 2019 08:22
peterjc added a commit to peterjc/biopython that referenced this pull request Jul 5, 2021
@peterjc peterjc mentioned this pull request Jul 5, 2021
3 tasks done
peterjc added a commit that referenced this pull request Jul 19, 2021
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.

4 participants