Author ncoghlan
Recipients brett.cannon, martin.panter, ncoghlan, python-dev, r.david.murray
Date 2016-05-15.03:44:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1463283870.86.0.909225539599.issue25533@psf.upfronthosting.co.za>
In-reply-to
Content
To answer Brett's question about "Why does walk_packages import parent modules?", the answer is "Because __init__ can modify __path__, so if you don't import the parent module, you may miss things that actually doing the import would find".

The classic example of this is pre-3.3 namespace packages: those work by calling pkgutil.extend_path() or pkg_resources.declare_namespace() from the package's __init__ file, and they can be made to work regardless of whether or not the parent module is implemented as "foo.py" or "foo/__init__.py". 

My recollection is that the pkgutil APIs treat that capability as a basic operating assumption: they import everything they find and check it for a __path__ attribute on the grounds that arbitrary modules *might* set __path__ dynamically.


It would potentially be worthwhile introducing side-effect free variants of these two APIs: "pkgutil.iter_modules_static()" and "pkgutil.walk_packages_static()" (suggested suffix inspired by "inspect.getattr_static()".

The idea with those would be to report all packages that can be found whilst assuming that no module dynamically adds a __path__ attribute to itself, or alters a __path__ attribute calculated by a standard import hook.

Actually doing that in a generic fashion would require either expanding the APIs for meta_path importers and path import hooks or else using functools.simplegeneric to allow new walkers to be registered for unknown importers and hooks, with the latter approach being closer to the way pkgutil currently works.
History
Date User Action Args
2016-05-15 03:44:30ncoghlansetrecipients: + ncoghlan, brett.cannon, r.david.murray, python-dev, martin.panter
2016-05-15 03:44:30ncoghlansetmessageid: <1463283870.86.0.909225539599.issue25533@psf.upfronthosting.co.za>
2016-05-15 03:44:30ncoghlanlinkissue25533 messages
2016-05-15 03:44:29ncoghlancreate