fix: stop praisonai.tools.multiedit submodule import from shadowing the multiedit function regardless of import order

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
MervinPraisonandClaude Opus 5.5 committed 2026-10-30 08:39:38 +00:00
1 parent 703057c221
commit 39c254eca8
2 files changed
+20 -5

No files matched your search

+17 -5
View File
@@ -17,6 +17,9 @@ __all__ = [
"KANBAN_TOOLS",
]
import sys as _sys
import types as _types
from .._lazy_cache import lazy_get
_LAZY_ATTRS = {
@@ -36,8 +39,17 @@ def __getattr__(name: str):
"""Lazy load tools."""
if name not in _LAZY_ATTRS:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
value = lazy_get(f"tools:{name}", _LAZY_ATTRS[name])
# Bind on the package: importing ``.multiedit`` sets the submodule as the
# ``multiedit`` attribute, which would otherwise shadow the function.
globals()[name] = value
return value
return lazy_get(f"tools:{name}", _LAZY_ATTRS[name])
class _ToolsModule(_types.ModuleType):
# Importing ``praisonai.tools.multiedit`` (from anywhere, in any order) makes
# the import system setattr the submodule onto the package, shadowing the
# ``multiedit`` function. Drop that binding so ``__getattr__`` stays in charge.
def __setattr__(self, name, value):
if name in _LAZY_ATTRS and isinstance(value, _types.ModuleType):
return
super().__setattr__(name, value)
_sys.modules[__name__].__class__ = _ToolsModule
@@ -8,8 +8,11 @@ import pytest
def test_tools_lazy_attr_is_stable_and_uses_lazy_cache():
import praisonai.tools as tools
# Importing the submodule first (as other tests do) must not shadow the function.
from praisonai.tools.multiedit import multiedit as direct
first = tools.multiedit
assert first is direct
second = tools.multiedit
assert callable(first) and first is second # not shadowed by the submodule
with pytest.raises(AttributeError):