PEP 810: Explicit lazy imports

The dedicated syntax has a few advantages. For starters it works better with from imports - you could indeed make this work but it’s getting pretty clunky and there’s a lot of unnecessary redudancy (e.g. foo, bar, baz = lazy_import(“numpy”, (“foo”, “bar”, baz”))

It’s also just a lot more obvious what’s going on - if it’s a function you might be wondering what lazy_import is, is it some new built-in? It it something that’s been imported? And how do type checkers treat this?

And finally it just aligns better with a mix of other things which are being imported normally.

It’s also probably worth noting that lazy is just a soft keyword, so it doesn’t impact the ability to use it elsewhere.

The motivation section also calls out that there are some non-syntax related proposals that have downsides as well (https://pep-previews–4622.org.readthedocs.build/pep-0810/#motivation)

3 Likes