Sitelet https://github.com/python/cpython/issues/93259
Skip to content

importlib.metadata.version(None) returns arbitrary version number #93259

Description

@peterzahemszky

The version() function of the importlib.metadata package returns an arbitrary version number when called with None. There's no documentation on what the return value means.

I would have expected one of the following instead:

  • raise an error saying that the distribution_name parameter cannot be None, or
  • explain what the return value means in the relevant documentation.

The 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.

>>> import sys
>>> print(sys.version)
3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)]
>>> import importlib.metadata
>>> importlib.metadata.version(None)
'21.2.3'

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

  • CPython versions tested on: 3.10.0
  • Operating system and architecture: Windows 10 Enterprise (64-bit operating system, x64-based processor)

Activity

  1. warsaw commented on May 26, 2022

    @warsaw
    Member

    I can't think of a valid meaning for distribution_name=None in this or any related API. In a fresh checkout of CPython main, you do get an exception, but probably not the right one. It should probably quick raise a ValueError.

    @jaraco

    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 None
    
  2. jaraco commented on Jun 23, 2022

    @jaraco
    Member

    I'm unsure whether the input should produce a ValueError or a TypeError, 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.0
    
  3. jaraco commented on Jun 23, 2022

    @jaraco
    Member

    Hmm. The way it's currently implemented, distribution_name is passed through as Context.name where None is defined to mean "all distributions", and Distribution._from_name is implemented to return the first matching distribution. 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.

  4. jaraco commented on Jun 23, 2022

    @jaraco
    Member

    I have a test capturing the failure and a potential fix in the aforementioned PR.

  5. added 2 commits that reference this issue on Jun 25, 2022
    f9c082a
    eb19c64
  6. warsaw commented on Jun 25, 2022

    @warsaw
    Member

    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 get 22.0.4 (from Python 3.12), which is of course the version of the pip distribution in the venv.

  7. added a commit that references this issue on Jun 26, 2022
  8. added a commit that references this issue on Jun 28, 2022
    bb5ba57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions