Prepending `None` to sys.path breaks `importlib.metadata.version`
Opened this issue · 2 comments
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
Specifically, the documentation says:
Only strings should be added to sys.path; all other data types are ignored during import.
It's not clear whether this obligates importlib.metadata
to ignore non-strings as well (in which case this would be an implementation bug) or whether the wording of the sys.path
entry should be stronger (to indicate that adding other data types may break things).
While the import system doesn't blow up when encountering non-string objects, the documentation clearly states that only strings should be added to the list. IMO this isn't a bug, but we could indeed improve the documentation.