Issue 35843: importlib.util docs for namespace packages innaccurate (original) (raw)

Issue35843

process

Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Anthony Sottile, barry, brett.cannon, docs@python, eric.smith, eric.snow, miss-islington, ncoghlan, ronaldoussoren
Priority: normal Keywords: patch, patch, patch

Created on 2019-01-28 18:03 by Anthony Sottile, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11690 merged Anthony Sottile,2019-01-28 18:23
PR 11690 merged Anthony Sottile,2019-01-28 18:24
PR 11690 merged Anthony Sottile,2019-01-28 18:24
Messages (6)
msg334484 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2019-01-28 18:03
For instance: # `a` is an empty directory, a PEP 420 namespace package >>> import importlib.util >>> importlib.util.find_spec('a') ModuleSpec(name='a', loader=None, origin='namespace', submodule_search_locations=_NamespacePath(['/tmp/x/a'])) https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.origin > ... Normally “origin” should be set, but it may be None (the default) which indicates it is unspecified (e.g. for namespace packages). above the `origin` is `'namespace'` https://docs.python.org/3/library/importlib.html#importlib.machinery.ModuleSpec.submodule_search_locations > List of strings for where to find submodules, if a package (None otherwise). However the `_NamespacePath` object above is not indexable: >>> x = importlib.util.find_spec('a').submodule_search_locations >>> x[0] Traceback (most recent call last): File "", line 1, in TypeError: '_NamespacePath' object does not support indexing I can work around however with: >>> next(iter(x)) '/tmp/x/a' ====================== so I guess a few things can/should come out of this: - Document the `'namespace'` origin - Document that `submodule_search_paths` is a Sized[str] instead - Add `__getitem__` to `_NamespacePath` such that it implements the full `Sized` protocol
msg334485 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2019-01-28 18:07
Hmmm, it appears this was changed in python3.7 to have `None` for the origin instead of `'namespace'` -- however the `submodule_search_locations` is still not indexable: >>> importlib.util.find_spec('a') ModuleSpec(name='a', loader=None, submodule_search_locations=_NamespacePath(['/tmp/x/a'])) >>> importlib.util.find_spec('a').origin >>> spec = importlib.util.find_spec('a') >>> spec.submodule_search_locations _NamespacePath(['/tmp/x/a']) >>> spec.submodule_search_locations[0] Traceback (most recent call last): File "", line 1, in TypeError: '_NamespacePath' object does not support indexing
msg334506 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2019-01-29 09:10
See also #35673.
msg336954 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-03-01 20:36
Anyone have an opinion about the __getitem__ proposal? I'm fine with it but I wanted to make sure other import + namespace folks don't disagree.
msg337050 - (view) Author: Barry A. Warsaw (barry) * (Python committer) Date: 2019-03-03 23:24
+1
msg337524 - (view) Author: miss-islington (miss-islington) Date: 2019-03-08 18:58
New changeset ab9b31f94737895f0121f26ba3ad718ebbc24fe1 by Miss Islington (bot) (Anthony Sottile) in branch 'master': bpo-35843: Implement __getitem__ for _NamespacePath (GH-11690) https://github.com/python/cpython/commit/ab9b31f94737895f0121f26ba3ad718ebbc24fe1
History
Date User Action Args
2022-04-11 14:59:10 admin set github: 80024
2019-03-08 18:58:36 brett.cannon set keywords:patch, patch, patchstatus: open -> closedresolution: fixedstage: patch review -> resolved
2019-03-08 18:58:19 miss-islington set nosy: + miss-islingtonmessages: +
2019-03-03 23:24:07 barry set keywords:patch, patch, patchmessages: +
2019-03-01 20:36:54 brett.cannon set keywords:patch, patch, patchmessages: +
2019-02-14 20:51:11 brett.cannon set keywords:patch, patch, patchnosy: + barry, ncoghlan, eric.smith, eric.snow
2019-01-29 09:10:02 ronaldoussoren set keywords:patch, patch, patchnosy: + ronaldoussorenmessages: +
2019-01-28 19:32:39 xtreak set keywords:patch, patch, patchnosy: + brett.cannon
2019-01-28 18:24:09 Anthony Sottile set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest11531>
2019-01-28 18:24:03 Anthony Sottile set keywords: + patchstage: (no value)pull_requests: + <pull%5Frequest11530>
2019-01-28 18:23:58 Anthony Sottile set keywords: + patchstage: (no value)pull_requests: + <pull%5Frequest11529>
2019-01-28 18:07:26 Anthony Sottile set messages: +
2019-01-28 18:03:10 Anthony Sottile create