Conversation
|
👋 Thank you for your draft pull request! Do you know that you can use |
8841ce6 to
962f99d
Compare
| test_module.__version__ = '0.12.2' | ||
| good_versions = ['0.12', '0.12.1', '0.12.0.dev', '0.12dev'] | ||
| bad_versions = ['1', '1.2rc1'] | ||
| import numpy |
There was a problem hiding this comment.
I could not use a fake module anymore with this change, but I did test edge case manually. If there is a way to pass in fake module again, please let me know. Thanks!
With this patch:
>>> from astropy.utils import minversion
>>> import numpy
>>> numpy.__version__
'1.20.2'
>>> minversion(numpy, '1.20.2')
True
>>> minversion(numpy, '1.20.2', inclusive=False)
False
This comment has been minimized.
This comment has been minimized.
mhvk
left a comment
There was a problem hiding this comment.
Wow, I had looked at this and had thought maybe to just add an extra layer, but you just went what does seem the right route, and hacked it all out. I like it! Just an in-line comment about how to get importlib.metadata on python 3.7
astropy/utils/introspection.py
Outdated
| import sys | ||
| import types | ||
| import importlib | ||
| import importlib.metadata |
There was a problem hiding this comment.
importlib.metadata requires python 3.8, so this needs to be
try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata
(Unless we drop python 3.7, which would be borderline: it was released June 27, 2018, so we're supposed to support 42 months, i.e., until Dec 27, 2021...)
There was a problem hiding this comment.
Ah, I knew I missed something. Thanks!
| bad_versions = ['1', '1.2rc1'] | ||
| import numpy | ||
| good_versions = ['1.16', '1.16.1', '1.16.0.dev', '1.16dev'] | ||
| bad_versions = ['100000', '100000.2rc1'] |
There was a problem hiding this comment.
p.s. Need to update this test when Numpy v100000 is released. 😆
|
Well, it may look good but the tests failed quite miserably!? |
|
Hmm, the problem is that |
Tests are still failing, this would imply larger API change
|
Indeed, for some packages (pyerfa too) the import name is not the package name ... |
|
@saimn - any suggestion on how to get the package name from the module? Or perhaps we should stick with supporting |
|
It seems a function was added recently to do just this: Otherwise I found a way with |
|
Does this mean switching to |
astropy/utils/introspection.py
Outdated
| module_version = metadata.version(module_name) | ||
| if inclusive: | ||
| return LooseVersion(have_version) >= LooseVersion(version) | ||
| return module_version >= version |
There was a problem hiding this comment.
importlib.metadata.version returns a string, not a version object like LooseVersion or packaging.version.Version. Strings don't compare well as versions, e.g. '3.10' < '3.9'.
|
This has languished too long. I don't remember what is the status anymore... |
|
We would need a function similar to |
0b716af to
3f740e3
Compare
|
@pllim - I pushed a new commit to your branch with the changes mentioned above. One drawback with |
mhvk
left a comment
There was a problem hiding this comment.
The need for the function and the cache are not quite as optimal as hoped (see suggestion inline for removing the cache), but I think it is definitely good to get rid of any use of distutils. And it seems that now we gotten as far as we did, we may as well go with it.
astropy/utils/introspection.py
Outdated
| m = re.match(expr, have_version) | ||
| if m: | ||
| have_version = m.group(0) | ||
| # Convert the module name to a distribution name |
There was a problem hiding this comment.
I think we mostly do not need this dict, since for most packages the name and distribution name are the same. So, might it make sense to generally avoid creating it by just doing something like:
try:
module_version = metadata.version(module_name)
except metadata.PackageNotFoundError: # module name different from package name?
module_version = metadata.version(packages_distributions()[module_name][0])
|
Code coverage we can ignore, so fine to merge once the last test has passed. |
|
Thanks, all! |
Description
This pull request is to simplify
minversioncode to useimportlib.metadata.versionthat Python provides.Fixes #11705