Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepending None to sys.path breaks importlib.metadata.version #128046

Open
jmh045000 opened this issue Dec 17, 2024 · 2 comments
Open

Prepending None to sys.path breaks importlib.metadata.version #128046

jmh045000 opened this issue Dec 17, 2024 · 2 comments
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@jmh045000
Copy link

jmh045000 commented Dec 17, 2024

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

@jmh045000 jmh045000 added the type-bug An unexpected behavior, bug, or error label Dec 17, 2024
@godlygeek
Copy link
Contributor

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

@FFY00
Copy link
Member

FFY00 commented Dec 17, 2024

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.

@picnixz picnixz added the stdlib Python modules in the Lib dir label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants