GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `importlib.metadata.version(None)` returns arbitrary version number · Issue #93259 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
python/importlib_metadata
#391
python/importlib_metadata#391
Closed
importlib.metadata.version(None) returns arbitrary version number#93259
python/importlib_metadata
#391
Copy link
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@peterzahemszky
peterzahemszky
opened on May 26, 2022
Issue body actions

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. peterzahemszky
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on May 26, 2022
  2. warsaw commented on May 26, 2022

    @warsaw
    warsaw
    on May 26, 2022
    Member
    More actions

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

    @jaraco
    jaraco
    on Jun 23, 2022
    Member
    More actions

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

    @jaraco
    jaraco
    on Jun 23, 2022
    Member
    More actions

    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.

  5. jaraco commented on Jun 23, 2022

    @jaraco
    jaraco
    on Jun 23, 2022
    Member
    More actions

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

  6. jaraco
    added 2 commits that reference this issue on Jun 25, 2022

    In Distribution.from_name, require a non-empty string. Fixes python/c…

    This issue will close once commit f9c082a is merged into the 'main' branch.
    f9c082a

    In Distribution.from_name, require a non-empty string. Fixes python/c…

    This issue will close once commit eb19c64 is merged into the 'main' branch.
    eb19c64
  7. jaraco
    closed this as completedin importlib_metadata#391on Jun 25, 2022
  8. warsaw commented on Jun 25, 2022

    @warsaw
    warsaw
    on Jun 25, 2022
    Member
    More actions

    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.

  9. jaraco
    added a commit that references this issue on Jun 26, 2022

    gh-93259: Validate arg to ``Distribution.from_name``. (GH-94270)

    38612a0
  10. serhiy-storchaka
    added a commit that references this issue on Jun 26, 2022

    Revert 30935 defer bpo45162 to 312 (#94285)

    1ffde3e
  11. clrpackages
    added a commit that references this issue on Jun 28, 2022

    pypi-importlib_metadata: Autospec creation for update from version 4.…

    This issue will close once commit bb5ba57 is merged into the 'main' branch.
    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 errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.