How do I make 'import x' return a subclass of types.ModuleType?
<p>Can Python's <code>import</code> statement return a subclass of <code>types.ModuleType</code> using import hooks? I would like to override <code>__getattribute__</code> to display a runtime warning when code outside a certain module reference names that aren't in <code>__all__</code>.</p>

<p>I know how to replace <code>sys.modules['foo']</code> after it's been imported. What I want is to instrument modules that match a pattern <em>as they are imported</em> so the importing code has an opportunity to trigger the warning.</p>

<p>Python frowns on the idea of making things public and private. This idea isn't about preventing your module's users from typing <code>from somemodule import sys</code>; instead, it is a documentation tool. This kind of instrumentation should make it much easier to document your module's API by including a correct  <code>__all__</code>. It should help you avoid accidentally referencing <code>sys</code> as <code>somemodule.sys</code> instead of simply <code>import sys</code>.</p>
