-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Description
To my surprise, I discovered in matrix-org/synapse#12223 that it is possible for importlib.metadata.version("foo") to return None. However, the stub suggests version and Distribution.version will always return a string:
| def version(self) -> str: ... |
| def version(distribution_name: str) -> str: ... |
To prove that version sometimes returns None:
- Create a new virtual environment. I'm using CPython 3.10.2 as my interpreter.
- Within the venv,
pip install bottle. (Any package will do; I choosebottlebecause it's small and doesn't have any dependencies). - The
versionofbottleagrees with what you've just installed:$ python -c "import importlib.metadata as m; print(repr(m.version('bottle')))" '0.12.19'
- Here's the dirty bit: remove the metadata files for that package but keep the metadata directory.
- Use
pip show bottleto find thesite-packageslocation - From there, remove all files in the
bottle-VERSION-.dist-infodirectory: `rm /path/to/site-packages/bottle-VERSION.dist-info/*'.
- Use
- The
versionofbottleis now judged to beNone:$ python -c "import importlib.metadata as m; print(repr(m.version('bottle')))" None
A few other notes:
- For a package
foothat's not installed,version("foo") raises a PackageNotFoundError. - I don't know if this ought to be considered a bug in the library rather than the annotations?
- I haven't dug much into the internals of
importlib.metadatabut it looks like the version is retrieved by looking inside ametadataattribute on aDistribution. I think other lookups are vulnerable to a similar problem; the only example I could find isDistribution.name.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels