From 010573ec724ebfe12fb9f16ca7b894fb643d93ea Mon Sep 17 00:00:00 2001 From: Matthias Bussonnier Date: Mon, 12 Feb 2024 12:05:54 +0100 Subject: [PATCH] Don't trigger autoimport on network requests. --- lib/python/pyflyby/_interactive.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/python/pyflyby/_interactive.py b/lib/python/pyflyby/_interactive.py index 027d6349..e13e67c5 100644 --- a/lib/python/pyflyby/_interactive.py +++ b/lib/python/pyflyby/_interactive.py @@ -1914,8 +1914,27 @@ def ofind_with_autoimport(oname, namespaces=None): is_multiline = len(ip.buffer) > 0 if namespaces is None: namespaces = _ipython_namespaces(ip) - if not is_multiline and is_identifier(oname, dotted=True): - self.auto_import(str(oname), [ns for nsname,ns in namespaces][::-1]) + is_network_request = False + frame = inspect.currentframe() + # jupyter_lab_completer seem to send inspect request when + # cycling through completions which trigger import. + # We cannot differentiate those from actual inspect when + # clicking on an object. + # So for now when we see the inspect request comes from + # ipykernel, we just don't autoimport + while frame is not None: + if "ipykernel.py" in inspect.getframeinfo(frame).filename: + is_network_request = True + break + frame = frame.f_back + if ( + not is_multiline + and is_identifier(oname, dotted=True) + and not is_network_request + ): + self.auto_import( + str(oname), [ns for nsname, ns in namespaces][::-1] + ) result = __original__(oname, namespaces=namespaces) return result return True