python / python/cpython

Prepending `None` to sys.path breaks `importlib.metadata.version`

Open
#128,046 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib topic-importlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:
import sys
sys.path.insert(0, None)
from importlib.metadata import version
print(version("requests"))

Results in

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.12/importlib/metadata/__init__.py", line 889, in version
    return distribution(distribution_name).version
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/metadata/__init__.py", line 862, in distribution
    return Distribution.from_name(distribution_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/metadata/__init__.py", line 397, in from_name
    return next(cls.discover(name=name))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/metadata/__init__.py", line 795, in <genexpr>
    path.search(prepared) for path in map(FastPath, paths)
    ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/metadata/__init__.py", line 693, in search
    return self.lookup(self.mtime).search(name)
                       ^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/metadata/__init__.py", line 698, in mtime
    return os.stat(self.root).st_mtime
           ^^^^^^^^^^^^^^^^^^
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType

The documentation for sys.path is not clear on what should happen with non-strings in the list.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example, then read importlib.metadata around Distribution.from_name, FastPath.search, and FastPath.mtime as identified in the traceback. Check the sys.path documentation and related tests to determine the intended handling of non-string entries; done means the behavior is explicitly defined and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.