Skip to content

Commit 46a483d

Browse files
committed
MyPy typing fixes
1 parent da109d6 commit 46a483d

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

src/google/adk/agents/config_agent_utils.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def from_config(config_path: str) -> BaseNode:
6363
cache = None
6464

6565
if cache is None:
66-
cache_dict = {}
66+
cache_dict: dict[str, BaseNode] = {}
6767
token = _loaded_nodes_cache.set(cache_dict)
6868
try:
6969
return _from_config(abs_path)
@@ -158,11 +158,23 @@ def _from_config(abs_path: str) -> BaseNode:
158158
agent_config = agent_class.config_type.model_validate(
159159
agent_config.model_dump()
160160
)
161-
return agent_class.from_config(agent_config, abs_path)
161+
from_config_fn = getattr(agent_class, "from_config", None)
162+
if not from_config_fn:
163+
raise ValueError(
164+
f"Agent class {agent_class.__name__} does not implement 'from_config'"
165+
)
166+
node: BaseNode = from_config_fn(agent_config, abs_path)
167+
return node
162168
else:
163169
# For built-in agent classes, no need to re-validate.
164170
agent_class = _resolve_node_class(agent_config.agent_class)
165-
return agent_class.from_config(agent_config, abs_path)
171+
from_config_fn = getattr(agent_class, "from_config", None)
172+
if not from_config_fn:
173+
raise ValueError(
174+
f"Agent class {agent_class.__name__} does not implement 'from_config'"
175+
)
176+
built_in_node: BaseNode = from_config_fn(agent_config, abs_path)
177+
return built_in_node
166178

167179

168180
def _resolve_node_class(agent_class: str) -> type[BaseNode]:
@@ -210,7 +222,8 @@ def _load_config_from_path(config_path: str) -> AgentConfig:
210222
with open(config_path, "r", encoding="utf-8") as f:
211223
config_data = yaml.safe_load(f)
212224

213-
return AgentConfig.model_validate(config_data)
225+
config: AgentConfig = AgentConfig.model_validate(config_data)
226+
return config
214227

215228

216229
@experimental(FeatureName.AGENT_CONFIG)
@@ -239,21 +252,23 @@ def resolve_agent_reference(
239252
"""
240253
if ref_config.config_path:
241254
if os.path.isabs(ref_config.config_path):
242-
return from_config(ref_config.config_path)
255+
node: BaseNode = from_config(ref_config.config_path)
256+
return node
243257
else:
244-
return from_config(
258+
rel_node: BaseNode = from_config(
245259
os.path.join(
246260
os.path.dirname(referencing_agent_config_abs_path),
247261
ref_config.config_path,
248262
)
249263
)
264+
return rel_node
250265
elif ref_config.code:
251266
return _resolve_agent_code_reference(ref_config.code)
252267
else:
253268
raise ValueError("AgentRefConfig must have either 'code' or 'config_path'")
254269

255270

256-
def _resolve_agent_code_reference(code: str) -> Any:
271+
def _resolve_agent_code_reference(code: str) -> BaseNode:
257272
"""Resolve a code reference to an actual agent instance.
258273
259274
Args:

0 commit comments

Comments
 (0)