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
Return type annotation for importlib.metadata.version
is incorrect
#7513
Comments
I'd be happy to PR an update to the annotations making |
Not too familiar with |
There's also the backport of this module, which (strangely?) has a more complete reference. https://importlib-metadata.readthedocs.io/en/latest/api.html#importlib_metadata.version doesn't suggest anything about None there.
Thanks for confirming! I'll get filing issues elsewhere. |
Thanks! If you're reporting a bug, I'd suggest the docs should also state that it throws PackageNotFoundError for a package that is not installed. :) |
If I understand correctly, I believe changes are usually made to importlib_metadata first, and then "forward-ported" to CPython (don't ask me why). So this isn't that strange :) |
For completeness: I've raised https://bugs.python.org/issue47060. It looks like python/importlib_metadata#371 might be related too. |
To my surprise, I discovered in matrix-org/synapse#12223 that it is possible for
importlib.metadata.version("foo")
to returnNone
. However, the stub suggestsversion
andDistribution.version
will always return a string:typeshed/stdlib/importlib/metadata/__init__.pyi
Line 161 in 50107e6
typeshed/stdlib/importlib/metadata/__init__.pyi
Line 213 in 50107e6
To prove that
version
sometimes returnsNone
:pip install bottle
. (Any package will do; I choosebottle
because it's small and doesn't have any dependencies).version
ofbottle
agrees with what you've just installed:pip show bottle
to find thesite-packages
locationbottle-VERSION-.dist-info
directory: `rm /path/to/site-packages/bottle-VERSION.dist-info/*'.version
ofbottle
is now judged to beNone
:$ python -c "import importlib.metadata as m; print(repr(m.version('bottle')))" None
A few other notes:
foo
that's not installed,version("foo") raises a PackageNotFoundError
.importlib.metadata
but it looks like the version is retrieved by looking inside ametadata
attribute on aDistribution
. I think other lookups are vulnerable to a similar problem; the only example I could find isDistribution.name
.The text was updated successfully, but these errors were encountered: