Hi! Love the PEP. A few questions about the spec that are not clear to me on first read:
- The “Syntax restrictions” specify “only allowed at the global (module) level, not inside functions, class bodies, with
try
/with
blocks, orimport *
". It’s not clear to me if this is a list of examples or a specification list. For example, if a module containsif foo: lazy import mymod
, would that import be “at the module level” and thus valid, or not? - The
try
restriction seems to prevent using this feature in the very common pattern where an “optional” module is attempted to be loaded, and a fallback mechanism (either providing an alternative source or dummy replacement) is provided in the except clause. I’m talking about:
try:
from typing import LiteralString
except ImportError:
LiteralString = str
I understand why this is hard to do lazily, but are there any suggestions on how to approach this?
- When a module has a
lazy import foo
, and”foo”
is already onsys.modules
because some other code already needed it, does the spec say if my current module will get a real module or a proxy in its globals? - The behaviour of argumentless
dir()
is not clear. If my module containslazy import foo; dir()
, does the call force the import of foo? (The document only clarifies what happens withdir(foo)
which, quite reasonably, forces the import.) - Finally, given that there’s likely some existing code that would benefit from using lazy imports, but they may also be using
globals()
(for example, in aneval()
call). The PEP says “if you add lazy imports to your module and callglobals()
, you’re responsible for handling the lazy objects”. What would be the recommended code changes in that scenario for the owners of the module if they don’t want to have lazy objects (which I imagine is a common scenario)?
Thanks again!