Skip to content

Commit 6eb241b

Browse files
9chait9DrStrangent Bot
authored andcommitted
Refactor lazy loading to use dictionary-based mapping and fix __getattr__ scope for better maintainability.
1 parent a661baf commit 6eb241b

File tree

2 files changed

+33
-47
lines changed

2 files changed

+33
-47
lines changed

src/google/adk/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020
from .agents.llm_agent import Agent
2121
from .runners import Runner
2222
else:
23+
import importlib
24+
25+
_LAZY_IMPORTS = {
26+
"Agent": ".agents.llm_agent",
27+
"Context": ".agents.context",
28+
"Runner": ".runners",
29+
}
30+
2331
def __getattr__(name: str):
24-
if name == "Agent":
25-
from .agents.llm_agent import Agent
26-
return Agent
27-
if name == "Context":
28-
from .agents.context import Context
29-
return Context
30-
if name == "Runner":
31-
from .runners import Runner
32-
return Runner
32+
if name in _LAZY_IMPORTS:
33+
module = importlib.import_module(_LAZY_IMPORTS[name], __name__)
34+
return getattr(module, name)
3335
raise AttributeError(f"module {__name__} has no attribute {name}")
3436

3537
from . import version

src/google/adk/agents/__init__.py

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,45 +28,29 @@
2828
from .parallel_agent import ParallelAgent
2929
from .run_config import RunConfig
3030
from .sequential_agent import SequentialAgent
31+
else:
32+
import importlib
3133

32-
def __getattr__(name: str):
33-
if name == 'BaseAgent':
34-
from .base_agent import BaseAgent
35-
return BaseAgent
36-
if name == 'Context':
37-
from .context import Context
38-
return Context
39-
if name == 'InvocationContext':
40-
from .invocation_context import InvocationContext
41-
return InvocationContext
42-
if name == 'LiveRequest':
43-
from .live_request_queue import LiveRequest
44-
return LiveRequest
45-
if name == 'LiveRequestQueue':
46-
from .live_request_queue import LiveRequestQueue
47-
return LiveRequestQueue
48-
if name == 'Agent':
49-
from .llm_agent import Agent
50-
return Agent
51-
if name == 'LlmAgent':
52-
from .llm_agent import LlmAgent
53-
return LlmAgent
54-
if name == 'LoopAgent':
55-
from .loop_agent import LoopAgent
56-
return LoopAgent
57-
if name == 'McpInstructionProvider':
58-
from .mcp_instruction_provider import McpInstructionProvider
59-
return McpInstructionProvider
60-
if name == 'ParallelAgent':
61-
from .parallel_agent import ParallelAgent
62-
return ParallelAgent
63-
if name == 'RunConfig':
64-
from .run_config import RunConfig
65-
return RunConfig
66-
if name == 'SequentialAgent':
67-
from .sequential_agent import SequentialAgent
68-
return SequentialAgent
69-
raise AttributeError(f"module {__name__} has no attribute {name}")
34+
_LAZY_IMPORTS = {
35+
"BaseAgent": ".base_agent",
36+
"Context": ".context",
37+
"InvocationContext": ".invocation_context",
38+
"LiveRequest": ".live_request_queue",
39+
"LiveRequestQueue": ".live_request_queue",
40+
"Agent": ".llm_agent",
41+
"LlmAgent": ".llm_agent",
42+
"LoopAgent": ".loop_agent",
43+
"McpInstructionProvider": ".mcp_instruction_provider",
44+
"ParallelAgent": ".parallel_agent",
45+
"RunConfig": ".run_config",
46+
"SequentialAgent": ".sequential_agent",
47+
}
48+
49+
def __getattr__(name: str):
50+
if name in _LAZY_IMPORTS:
51+
module = importlib.import_module(_LAZY_IMPORTS[name], __name__)
52+
return getattr(module, name)
53+
raise AttributeError(f"module {__name__} has no attribute {name}")
7054

7155
__all__ = [
7256
'Agent',

0 commit comments

Comments
 (0)