Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/google/adk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,27 @@
# limitations under the License.

from __future__ import annotations
from typing import TYPE_CHECKING

from . import version
from .agents.context import Context
from .agents.llm_agent import Agent
from .runners import Runner
if TYPE_CHECKING:
from .agents.context import Context
from .agents.llm_agent import Agent
from .runners import Runner
else:
import importlib

_LAZY_IMPORTS = {
"Agent": ".agents.llm_agent",
"Context": ".agents.context",
"Runner": ".runners",
}

def __getattr__(name: str):
if name in _LAZY_IMPORTS:
module = importlib.import_module(_LAZY_IMPORTS[name], __name__)
return getattr(module, name)
raise AttributeError(f"module {__name__} has no attribute {name}")
Comment on lines +31 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid a long chain of if statements, consider using a dictionary to map attribute names to their modules. This makes the __getattr__ function more scalable and easier to read, especially if more lazy-loaded modules are added in the future.

Suggested change
def __getattr__(name: str):
if name == "Agent":
from .agents.llm_agent import Agent
return Agent
if name == "Context":
from .agents.context import Context
return Context
if name == "Runner":
from .runners import Runner
return Runner
raise AttributeError(f"module {__name__} has no attribute {name}")
import importlib
_LAZY_IMPORTS = {
"Agent": ".agents.llm_agent",
"Context": ".agents.context",
"Runner": ".runners",
}
def __getattr__(name: str):
if name in _LAZY_IMPORTS:
module = importlib.import_module(_LAZY_IMPORTS[name], __name__)
return getattr(module, name)
raise AttributeError(f"module {__name__} has no attribute {name}")


from . import version
__version__ = version.__version__
__all__ = ["Agent", "Context", "Runner"]
51 changes: 39 additions & 12 deletions src/google/adk/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,45 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from .base_agent import BaseAgent
from .context import Context
from .invocation_context import InvocationContext
from .live_request_queue import LiveRequest
from .live_request_queue import LiveRequestQueue
from .llm_agent import Agent
from .llm_agent import LlmAgent
from .loop_agent import LoopAgent
from .mcp_instruction_provider import McpInstructionProvider
from .parallel_agent import ParallelAgent
from .run_config import RunConfig
from .sequential_agent import SequentialAgent
from __future__ import annotations
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from .base_agent import BaseAgent
from .context import Context
from .invocation_context import InvocationContext
from .live_request_queue import LiveRequest
from .live_request_queue import LiveRequestQueue
from .llm_agent import Agent
from .llm_agent import LlmAgent
from .loop_agent import LoopAgent
from .mcp_instruction_provider import McpInstructionProvider
from .parallel_agent import ParallelAgent
from .run_config import RunConfig
from .sequential_agent import SequentialAgent
else:
import importlib

_LAZY_IMPORTS = {
"BaseAgent": ".base_agent",
"Context": ".context",
"InvocationContext": ".invocation_context",
"LiveRequest": ".live_request_queue",
"LiveRequestQueue": ".live_request_queue",
"Agent": ".llm_agent",
"LlmAgent": ".llm_agent",
"LoopAgent": ".loop_agent",
"McpInstructionProvider": ".mcp_instruction_provider",
"ParallelAgent": ".parallel_agent",
"RunConfig": ".run_config",
"SequentialAgent": ".sequential_agent",
}

def __getattr__(name: str):
if name in _LAZY_IMPORTS:
module = importlib.import_module(_LAZY_IMPORTS[name], __name__)
return getattr(module, name)
raise AttributeError(f"module {__name__} has no attribute {name}")

__all__ = [
'Agent',
Expand Down