Repository navigation
importlib.metadata.version(None) returns arbitrary version number #93259
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on May 26, 2022 I can't think of a valid meaning for
distribution_name=Nonein this or any related API. In a fresh checkout of CPythonmain, you do get an exception, but probably not the right one. It should probably quick raise aValueError.Python 3.12.0a0 (heads/main:da39719483, May 26 2022, 09:01:35) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> from importlib.metadata import version >>> version(None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/barry/projects/python/cpython/Lib/importlib/metadata/__init__.py", line 991, in version return distribution(distribution_name).version ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/barry/projects/python/cpython/Lib/importlib/metadata/__init__.py", line 964, in distribution return Distribution.from_name(distribution_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/barry/projects/python/cpython/Lib/importlib/metadata/__init__.py", line 561, in from_name raise PackageNotFoundError(name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ importlib.metadata.PackageNotFoundError: No package metadata was found for NoneI'm unsure whether the input should produce a
ValueErroror aTypeError, but it also occurs to me that an empty string may also produce unexpected results.$ pip-run -q importlib_metadata -- -c "import importlib_metadata as md; print(md.version(''))" 4.11.4 $ py -3.11 -c "import importlib.metadata as md; print(md.version(''))" 16.4.0Hmm. The way it's currently implemented,
distribution_nameis passed through asContext.namewhereNoneis defined to mean "all distributions", andDistribution._from_nameis implemented to return the first matching distribution. I expect the only reason that Barry is getting thePackageNotFoundErroris because he's running in an environment where there are literally no distributions, but if there were a single distribution installed, I'd expect him to get the version for that one.I have a test capturing the failure and a potential fix in the aforementioned PR.
- added 2 commits that reference this issue
on Jun 25, 2022 I expect the only reason that Barry is getting the PackageNotFoundError is because he's running in an environment where there are literally no distributions, but if there were a single distribution installed, I'd expect him to get the version for that one.
Indeed, and all I need to do to see the difference is invoke Python from a
./python.exe -m venv. Then I get22.0.4(from Python 3.12), which is of course the version of thepipdistribution in the venv.- added a commit that references this issue
on Jun 26, 2022 - added a commit that references this issue
on Jun 28, 2022
The
version()function of theimportlib.metadatapackage returns an arbitrary version number when called withNone. There's no documentation on what the return value means.I would have expected one of the following instead:
distribution_nameparameter cannot beNone, orThe current behaviour is confusing as one could think, for example, that the return value is the version of the project/package they call it from.
I looks like the return value is the version of an arbitrary package installed in the Python virtual environment. Calling the function repeatedly seems to give the same value, but installing a new package can cause the return value to change.
System information