On 1/18/21 12:24 PM, Guido van Rossum wrote:
On Sun, Jan 17, 2021 at 7:33 AM Larry Hastings <larry@hastings.org> wrote:
If your imports are complicated, you could always hide them in a function.  I just tried this and it seems to work fine:

def my_imports():
    global other_mod
    import other_mod

So, you could put all your imports in such a function, run it from inside a "if typing.TYPE_CHECKING" block, and you'd have a convenient way of doing all your imports from inside IPython too.


But static type checkers won't understand such imports. (Or is this about annotations used for other purposes? Then I suppose it's fine, but only as long as you completely give up static type checks for modules that use this idiom.)


Oh, okay.  I haven't used the static type checkers, so it's not clear to me what powers they do and don't have.  It was only a minor suggestion anyway.  Perhaps PEP 649 will be slightly inconvenient to people exploring their code inside IPython.

Or maybe it'd work if they gated the if statement on running in ipython?

if typing.TYPE_CHECKING or os.path.split(sys.argv[0])[1] == "ipython3":
    import other_mod


Cheers,


/arry