Title: Return type annotation for `importlib.metadata.version` is incorrect · Issue #7513 · python/typeshed · GitHub
Open Graph Title: Return type annotation for `importlib.metadata.version` is incorrect · Issue #7513 · python/typeshed
X Title: Return type annotation for `importlib.metadata.version` is incorrect · Issue #7513 · python/typeshed
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: typeshed/stdlib/i...
Open Graph 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 a...
X 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.vers...
Opengraph URL: https://github.com/python/typeshed/issues/7513
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Return type annotation for `importlib.metadata.version` is incorrect","articleBody":"To my surprise, I discovered in https://github.com/matrix-org/synapse/issues/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:\r\n\r\nhttps://github.com/python/typeshed/blob/50107e6e2b7144a1b0bfe6d46a7bc81926deed2b/stdlib/importlib/metadata/__init__.pyi#L161\r\n\r\nhttps://github.com/python/typeshed/blob/50107e6e2b7144a1b0bfe6d46a7bc81926deed2b/stdlib/importlib/metadata/__init__.pyi#L213\r\n\r\nTo prove that `version` sometimes returns `None`:\r\n\r\n1. Create a new virtual environment. I'm using CPython 3.10.2 as my interpreter.\r\n2. Within the venv, `pip install bottle`. (Any package will do; I choose `bottle` because it's small and doesn't have any dependencies).\r\n3. The `version` of `bottle` agrees with what you've just installed:\r\n ```shell\r\n $ python -c \"import importlib.metadata as m; print(repr(m.version('bottle')))\"\r\n '0.12.19'\r\n ````\r\n4. Here's the dirty bit: remove the metadata files for that package but keep the metadata directory.\r\n - Use `pip show bottle` to find the `site-packages` location\r\n - From there, remove all files in the `bottle-VERSION-.dist-info` directory: `rm /path/to/site-packages/bottle-VERSION.dist-info/*'.\r\n5. The `version` of `bottle` is now judged to be `None`:\r\n ```shell\r\n $ python -c \"import importlib.metadata as m; print(repr(m.version('bottle')))\"\r\n None\r\n ````\r\n\r\nA few other notes:\r\n- For a package `foo` that's not installed, `version(\"foo\") raises a PackageNotFoundError`.\r\n- I don't know if this ought to be considered a bug in the library rather than the annotations?\r\n- I haven't dug much into the internals of `importlib.metadata` but it looks like the version is retrieved by looking inside a `metadata` attribute on a `Distribution`. I think other lookups are vulnerable to a similar problem; the only example I could find is `Distribution.name`.\r\n\r\n","author":{"url":"https://github.com/DMRobertson","@type":"Person","name":"DMRobertson"},"datePublished":"2022-03-18T17:37:34Z","url":"https://github.com/python/typeshed/issues/7513"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a324e904-fa98-4a23-8edf-ed4be44e9f94 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9EAA:220721:2AC5B4:3AE1FF:6B008804 |
| html-safe-nonce | 757fcf45522c04b6c61272391e62de950163f060f01e5da3a7628754f77caf43 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RUFBOjIyMDcyMToyQUM1QjQ6M0FFMUZGOjZCMDA4ODA0IiwidmlzaXRvcl9pZCI6IjE4OTQyNDM2MzI0NDY1MjU0OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3f87d0f7a61b8c3cd19dbcbd6b73904f0aa39b26d31a04697ec32c17efaad25d |
| hovercard-subject-tag | issue:1173847176 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/7513/issue_layout |
| twitter:image | https://opengraph.githubassets.com/9fbabe5b0b58f09d6074fdf9b82a19390b44f3e64840140b34c3ea9e522040bf/python/typeshed/issues/7513 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/9fbabe5b0b58f09d6074fdf9b82a19390b44f3e64840140b34c3ea9e522040bf/python/typeshed/issues/7513 |
| og:image:alt | 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 a... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DMRobertson |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4d3d8e50f02c1d7bf86d5acfa1d3bfe9366abd6528a6b429eb07a168429d2b61 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | db758f8e02b8257935fb03edf81a3e76eb3abd11 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width