Title: APIs related to `importlib.abc.PathEntryFinder` should use protocol instead · Issue #11541 · python/typeshed · GitHub
Open Graph Title: APIs related to `importlib.abc.PathEntryFinder` should use protocol instead · Issue #11541 · python/typeshed
X Title: APIs related to `importlib.abc.PathEntryFinder` should use protocol instead · Issue #11541 · python/typeshed
Description: Although importlib.abc.PathEntryFinder is defined as an abstract class in the standard library, path entry finders are intentionally loosely defined in the Python docs (and originated PEPs), so that the only thing that needs to be implem...
Open Graph Description: Although importlib.abc.PathEntryFinder is defined as an abstract class in the standard library, path entry finders are intentionally loosely defined in the Python docs (and originated PEPs), so tha...
X Description: Although importlib.abc.PathEntryFinder is defined as an abstract class in the standard library, path entry finders are intentionally loosely defined in the Python docs (and originated PEPs), so tha...
Opengraph URL: https://github.com/python/typeshed/issues/11541
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"APIs related to `importlib.abc.PathEntryFinder` should use protocol instead","articleBody":"Although `importlib.abc.PathEntryFinder` is defined as an abstract class in the standard library, `path entry finders` are intentionally loosely defined in the [Python docs](https://docs.python.org/3/reference/import.html#path-entry-finder-protocol) (and originated PEPs), so that the only thing that needs to be implemented is one method (`find_spec`). This matches the functioning of a protocol.\r\n\r\nThe reason why the abstract class is defined is probably informational (for documentation purposes) + extra code re-use internally in the stdlib. This situation is similar to `importlib.abc.MetaPathFinder`, and other `importlib.abc` classes (like loaders).\r\n\r\nThis affects https://github.com/python/typeshed/blob/af84d2fbfe8640c9f5049bfd0e74ea3270c8ea2c/stdlib/sys/__init__.pyi#L54 and probably other places.\r\n\r\n\r\nFor example, I would expect the following snippet to type check with no problems:\r\n```python\r\nimport sys\r\nfrom types import ModuleType\r\nfrom importlib.machinery import ModuleSpec\r\nfrom typing_extensions import Self\r\n\r\n\r\nclass Finder:\r\n @classmethod\r\n def path_hook(cls, path_entry: str) -> type[Self]:\r\n return cls # simplified mock for demonstration purposes only\r\n\r\n @classmethod\r\n def find_spec(cls, fullname: str, target: ModuleType | None = None) -> ModuleSpec | None:\r\n return None # simplified mock for demonstration purposes only\r\n \r\n \r\nsys.path_hooks.append(Finder.path_hook)\r\n```\r\n\r\n\r\nBut instead I am having an error:\r\n```console\r\nmain.py:9: error: Missing return statement [empty-body]\r\nmain.py:17: error: Argument 1 to \"append\" of \"list\" has incompatible type \"Callable[[str], type[Finder]]\"; expected \"Callable[[str], PathEntryFinder]\" [arg-type]\r\nFound 2 errors in 1 file (checked 1 source file)\r\n```\r\n\r\nhttps://mypy-play.net/?mypy=latest&python=3.12&gist=dfd3a0dcf277afc43cf59c8eb07afc5b\r\n","author":{"url":"https://github.com/abravalheri","@type":"Person","name":"abravalheri"},"datePublished":"2024-03-08T14:43:02Z","url":"https://github.com/python/typeshed/issues/11541"}
| 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:1674879f-08ff-3c62-c1df-1f53e5e6650a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9948:363D8E:29B902:36E69E:6B00C0F5 |
| html-safe-nonce | b6a3d116358f86fdc782efa3819b6e686fe0cc64f94220d9fdf58b1640c8efa2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTQ4OjM2M0Q4RToyOUI5MDI6MzZFNjlFOjZCMDBDMEY1IiwidmlzaXRvcl9pZCI6IjUwNzU3ODQxNDQ1MjU1MDg4NTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6c40bcda282ff8eb2a5613735b29fbd95eba7a43f697fe6aad5186d350a1d2a9 |
| hovercard-subject-tag | issue:2176174767 |
| 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/11541/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f19ac8ee8bd2d159a5bd4aae2d12f4709c72168f0053a5f522a7a95685459c94/python/typeshed/issues/11541 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f19ac8ee8bd2d159a5bd4aae2d12f4709c72168f0053a5f522a7a95685459c94/python/typeshed/issues/11541 |
| og:image:alt | Although importlib.abc.PathEntryFinder is defined as an abstract class in the standard library, path entry finders are intentionally loosely defined in the Python docs (and originated PEPs), so tha... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abravalheri |
| hostname | github.com |
| expected-hostname | github.com |
| None | 93edd3bebc4afb7ce711ee74cb5d2a95927850b082a4d43e212222389cb1c415 |
| 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 | 1fee81a280ea86312b69448e261b418189bfd5b5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width