Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Simple refactoring to reduce astropy import time.
  • Loading branch information
mhvk committed Jul 12, 2018
commit 47b7dd7f867792eb97d0677c874abdc125709ab6
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ astropy.wcs
Performance Improvements
------------------------

- Reduced import time by more cautious use of the standard library. [#7647]

astropy.coordinates
^^^^^^^^^^^^^^^^^^^

Expand Down
27 changes: 10 additions & 17 deletions astropy/utils/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ def minversion(module, version, inclusive=True, version_path='__version__'):
Returns `True` if the specified Python module satisfies a minimum version
requirement, and `False` if not.

By default this uses `pkg_resources.parse_version` to do the version
comparison if available. Otherwise it falls back on
`distutils.version.LooseVersion`.

Parameters
----------

Expand Down Expand Up @@ -143,22 +139,19 @@ def minversion(module, version, inclusive=True, version_path='__version__'):
else:
have_version = resolve_name(module.__name__, version_path)

try:
from pkg_resources import parse_version
except ImportError:
from distutils.version import LooseVersion as parse_version
# LooseVersion raises a TypeError when strings like dev, rc1 are part
# of the version number. Match the dotted numbers only. Regex taken
# from PEP440, https://www.python.org/dev/peps/pep-0440/, Appendix B
expr = '^([1-9]\\d*!)?(0|[1-9]\\d*)(\\.(0|[1-9]\\d*))*'
m = re.match(expr, version)
if m:
version = m.group(0)
from distutils.version import LooseVersion
# LooseVersion raises a TypeError when strings like dev, rc1 are part

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.

I don't know if this is true anymore:

In [5]: LooseVersion('3.0dev0') < LooseVersion('3.0dev1')
Out[5]: True

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.

(this was added in #5944 so we should check why)

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.

Good point. All tests pass on python 3.7 at least without it. I'll push a new commit that removes the doctoring - we'll see if the tests #5944 added pass on all versions.

# of the version number. Match the dotted numbers only. Regex taken
# from PEP440, https://www.python.org/dev/peps/pep-0440/, Appendix B
expr = '^([1-9]\\d*!)?(0|[1-9]\\d*)(\\.(0|[1-9]\\d*))*'
m = re.match(expr, version)
if m:
version = m.group(0)

if inclusive:
return parse_version(have_version) >= parse_version(version)
return LooseVersion(have_version) >= LooseVersion(version)
else:
return parse_version(have_version) > parse_version(version)
return LooseVersion(have_version) > LooseVersion(version)


def find_current_module(depth=1, finddiff=False):
Expand Down
3 changes: 1 addition & 2 deletions astropy/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import locale
import threading
import re
import urllib.request

from itertools import zip_longest
from contextlib import contextmanager
Expand Down Expand Up @@ -195,7 +194,7 @@ def find_api_page(obj, version=None, openinbrowser=True, timeout=None):

"""
import webbrowser

import urllib.request
from zlib import decompress

if (not isinstance(obj, str) and
Expand Down
11 changes: 0 additions & 11 deletions astropy/utils/tests/test_introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,3 @@ def _minversion_test():
assert minversion(test_module, version)
for version in bad_versions:
assert not minversion(test_module, version)


def test_minversion():

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.

Does _minversion_test above still run with this removed?

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.

I agree, I think _minversion_test needs to be renamed to test_minversion

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.

Wow, proof yet again how useful review is... How could I have missed that!

import sys
if 'pkg_resources' in sys.modules:
pkg_resources_saved = sys.modules['pkg_resources']
# Force ImportError for pkg_resources in minversion()
sys.modules['pkg_resource'] = None
_minversion_test()
sys.modules['pkg_resource'] = pkg_resources_saved
_minversion_test()