[issue35843] importlib.util docs for namespace packages innaccurate

New submission from Anthony Sottile <asottile@umich.edu>: 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.ModuleS...
... 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.ModuleS...
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 "<stdin>", line 1, in <module> 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 ---------- assignee: docs@python components: Documentation messages: 334484 nosy: Anthony Sottile, docs@python priority: normal severity: normal status: open title: importlib.util docs for namespace packages innaccurate versions: Python 3.7, Python 3.8 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Anthony Sottile <asottile@umich.edu> added the comment: 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 "<stdin>", line 1, in <module> TypeError: '_NamespacePath' object does not support indexing
---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Change by Anthony Sottile <asottile@umich.edu>: ---------- keywords: +patch pull_requests: +11529 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Change by Anthony Sottile <asottile@umich.edu>: ---------- keywords: +patch, patch pull_requests: +11529, 11530 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Change by Anthony Sottile <asottile@umich.edu>: ---------- keywords: +patch, patch, patch pull_requests: +11529, 11530, 11531 stage: -> patch review _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Change by Karthikeyan Singaravelan <tir.karthi@gmail.com>: ---------- nosy: +brett.cannon _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Ronald Oussoren <ronaldoussoren@mac.com> added the comment: See also #35673. ---------- nosy: +ronaldoussoren _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Change by Brett Cannon <brett@python.org>: ---------- nosy: +barry, eric.smith, eric.snow, ncoghlan _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Brett Cannon <brett@python.org> added the comment: 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. ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Barry A. Warsaw <barry@python.org> added the comment: +1 ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

miss-islington <mariatta.wijaya+miss-islington@gmail.com> added the comment: 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/ab9b31f94737895f0121f26ba3ad718ebbc... ---------- nosy: +miss-islington _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________

Change by Brett Cannon <brett@python.org>: ---------- resolution: -> fixed stage: patch review -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue35843> _______________________________________
participants (6)
-
Anthony Sottile
-
Barry A. Warsaw
-
Brett Cannon
-
Karthikeyan Singaravelan
-
miss-islington
-
Ronald Oussoren