@@ -263,15 +263,11 @@ def __init__(self, module_name: str, func: Callable[P, T]):
263263
264264 super ().__init__ (func )
265265
266- # Auto-register packages used by this function
267- D3Function ._available_packages [module_name ].update (
268- pkg .to_import_statement () for pkg in self ._function_info .packages
269- )
270-
271266 # Update the function in case the function was updated in the same session.
272267 # For example, jupyter notebook server can be running, but function signature can
273268 # change constantly.
274- if self in D3Function ._available_d3functions [module_name ]:
269+ is_replacement = self in D3Function ._available_d3functions [module_name ]
270+ if is_replacement :
275271 logger .debug (
276272 "Function '%s' in module '%s' is being replaced." ,
277273 self .name ,
@@ -280,6 +276,19 @@ def __init__(self, module_name: str, func: Callable[P, T]):
280276 D3Function ._available_d3functions [module_name ].discard (self )
281277 D3Function ._available_d3functions [module_name ].add (self )
282278
279+ if is_replacement :
280+ # Full rebuild needed to evict stale imports from the replaced function.
281+ D3Function ._available_packages [module_name ] = {
282+ pkg .to_import_statement ()
283+ for f in D3Function ._available_d3functions [module_name ]
284+ for pkg in f ._function_info .packages
285+ }
286+ else :
287+ # New function: incrementally add its packages. No stale imports to remove.
288+ D3Function ._available_packages [module_name ].update (
289+ pkg .to_import_statement () for pkg in self ._function_info .packages
290+ )
291+
283292 def __eq__ (self , other : object ) -> bool :
284293 """Check equality based on function name for unique registration.
285294
0 commit comments