This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: importlib.util docs for namespace packages innaccurate
Type: Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7
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 "<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
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 "<stdin>", line 1, in <module>
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:10adminsetgithub: 80024
2019-03-08 18:58:36brett.cannonsetkeywords: patch, patch, patch
status: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-03-08 18:58:19miss-islingtonsetnosy: + miss-islington
messages: + msg337524
2019-03-03 23:24:07barrysetkeywords: patch, patch, patch

messages: + msg337050
2019-03-01 20:36:54brett.cannonsetkeywords: patch, patch, patch

messages: + msg336954
2019-02-14 20:51:11brett.cannonsetkeywords: patch, patch, patch
nosy: + barry, ncoghlan, eric.smith, eric.snow
2019-01-29 09:10:02ronaldoussorensetkeywords: patch, patch, patch
nosy: + ronaldoussoren
messages: + msg334506

2019-01-28 19:32:39xtreaksetkeywords: patch, patch, patch
nosy: + brett.cannon
2019-01-28 18:24:09Anthony Sottilesetkeywords: + patch
stage: patch review
pull_requests: + pull_request11531
2019-01-28 18:24:03Anthony Sottilesetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11530
2019-01-28 18:23:58Anthony Sottilesetkeywords: + patch
stage: (no value)
pull_requests: + pull_request11529
2019-01-28 18:07:26Anthony Sottilesetmessages: + msg334485
2019-01-28 18:03:10Anthony Sottilecreate