From d24915a92daaf4faa4caec23f0db3e6f087e6bd4 Mon Sep 17 00:00:00 2001 From: Divyansh Choudhary Date: Tue, 19 Nov 2024 13:06:46 +0530 Subject: [PATCH 1/2] Disable auto-import of lazy variables These variables come from the `pyflyby_autoimport_` namespace which exists in memory and shouldn't be added to a notebook. --- lib/python/pyflyby/_interactive.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/python/pyflyby/_interactive.py b/lib/python/pyflyby/_interactive.py index d4ef152a..a6318120 100644 --- a/lib/python/pyflyby/_interactive.py +++ b/lib/python/pyflyby/_interactive.py @@ -36,6 +36,8 @@ indent) +PYFLYBY_LAZY_LOAD_PREFIX = "from pyflyby_autoimport_" + if False: __original__ = None # for pyflakes @@ -2151,7 +2153,8 @@ def auto_import( namespaces = get_global_namespaces(self._ip) def post_import_hook(imp): - send_comm_message(MISSING_IMPORTS, {"missing_imports": str(imp)}) + if not str(imp).startswith(PYFLYBY_LAZY_LOAD_PREFIX): + send_comm_message(MISSING_IMPORTS, {"missing_imports": str(imp)}) return self._safe_call( auto_import, arg=arg, namespaces=namespaces, From e263594e940bb5952e7f79a6a83f490c25c91a65 Mon Sep 17 00:00:00 2001 From: Divyansh Choudhary Date: Tue, 26 Nov 2024 12:35:05 +0530 Subject: [PATCH 2/2] Address some review comments --- lib/python/pyflyby/_dynimp.py | 5 +++-- lib/python/pyflyby/_interactive.py | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/python/pyflyby/_dynimp.py b/lib/python/pyflyby/_dynimp.py index 69282a9c..1289358a 100644 --- a/lib/python/pyflyby/_dynimp.py +++ b/lib/python/pyflyby/_dynimp.py @@ -49,6 +49,7 @@ module_dict = {} +PYFLYBY_LAZY_LOAD_PREFIX = "from pyflyby_autoimport_" def add_import(names: str, code: str, *, strict: bool = True): """ @@ -110,8 +111,8 @@ def _add_import(ip, names: str, code: str) -> None: private version of add_import """ assert ip is not None - mang = "pyflyby_autoimport_" + names.replace(",", "_").replace(" ", "_") - a: FrozenSet[Import] = ImportSet(f"from {mang} import {names}")._importset + mang = PYFLYBY_LAZY_LOAD_PREFIX + names.replace(",", "_").replace(" ", "_") + a: FrozenSet[Import] = ImportSet(f"{mang} import {names}")._importset b: FrozenSet[Import] = ip._auto_importer.db.known_imports._importset s_import: FrozenSet[Import] = a | b diff --git a/lib/python/pyflyby/_interactive.py b/lib/python/pyflyby/_interactive.py index a6318120..ea11ecdc 100644 --- a/lib/python/pyflyby/_interactive.py +++ b/lib/python/pyflyby/_interactive.py @@ -22,7 +22,8 @@ auto_import, clear_failed_imports_cache, load_symbol) -from pyflyby._dynimp import inject as inject_dynamic_import +from pyflyby._dynimp import (inject as inject_dynamic_import, + PYFLYBY_LAZY_LOAD_PREFIX) from pyflyby._comms import (initialize_comms, remove_comms, send_comm_message, MISSING_IMPORTS) from pyflyby._file import Filename, atomic_write_file, read_file @@ -35,9 +36,6 @@ FunctionWithGlobals, NullCtx, advise, indent) - -PYFLYBY_LAZY_LOAD_PREFIX = "from pyflyby_autoimport_" - if False: __original__ = None # for pyflakes