On Sun, Apr 10, 2022 at 2:39 AM Daniel Pope <lord.mauve@gmail.com> wrote:
On Fri, 8 Apr 2022, 17:44 Guido van Rossum, <guido@python.org> wrote:
The interesting idea here seems to make "lazy imports" easier to implement by making them explicit in the code. So far, most lazy import frameworks for Python have done hacks with `__getattribute__` overrides. 

The value is more than ease of implementation. Having syntax for import expressions makes them statically analysable, which is needed for type checkers and IDE autocompletion.

But I also see value in being able to type out code that uses modules not yet imported without breaking my flow to add an import statement. I don't yet trust IDEs to do this because I've been bitten by them doing so incorrectly in the past.

The key questions to me are
- What should the notation be?

I would like to bid again for (import package.module) as an expression. Instead of doing the import and assigning package to a variable package it would evaluate to the module object package.module.

That is an extremely subtle shift for what `import x.y` does compared to `(import x.y)`. That requires a context switch of not only seeing `import` in an expression context, but that the statement also acts differently in terms of what is returned by the equivalent statement. I really don't to try and teach that distinction to a newcomer. And I don't think the ergonomics are great enough to warrant the context switch.

Plus you can do this today with imortlib.import_module(). Unless you're suggesting the name also be bound in the scope it's run in? In which case that's `(abc := importlib.import_module("collections.abc")).Mapping`.

-Brett
 

The `as` form is not needed because no name is assigned and the `from` form isn't as valuable because you can just use attribute access afterwards.

It isn't terse but it does make use of the import keyword and is thus instantly recognisable. It is even syntax highlighted correctly by much existing software. If we're using the import keyword then I think it has to look like this.

But I concede that it isn't particularly elegant to type hint things with

(import collections.abc).Mapping

...but not so inelegant that I couldn't see myself using it for a few one-off imports per module.

A quirk is that it means there's a big difference between the statements

import foo

and

(import foo)

because one assigns a variable. I don't mind that; I don't think it is too ambiguous to a reader.



_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/T6V7WZAFEXGWMHXLHS7XHYXI5OPMOZKA/
Code of Conduct: http://python.org/psf/codeofconduct/