
On Sun, Apr 25, 2021 at 10:30 AM Brett Cannon <brett@python.org> wrote:
I know I would be curious, especially if backwards compatibility can be solved reasonably (for those that haven't lived this, deferred execution historically messes up code relying on import side-effects and trackbacks are weird as they occur at access time instead of at the import statement).
I had been assuming that due to backward compatibility and performance of `LOAD_GLOBAL`, this would need to be a new form of import, syntactically distinguished. But the performance and some of the compatibility concerns could be alleviated by making all imports deferred by default, and then resolving any not-yet-resolved imports at the end of module execution. This is perhaps even better for the non-typing case, since it would generally fix most import cycle problems in Python. (It would be sort of equivalent to moving all imports that aren't needed for module execution to the end of the module, which is another ugly but effective workaround for cycles.) It would have the downside that type-only imports which will never be needed at runtime at all will still be imported, even if `__annotations__` are never accessed. I think it's still problematic for backward compatibility with import side effects, though, so if we did this at the very least it would have to be behind a `__future__` import. Carl