Skip to content

Commit d2c31dc

Browse files
committed
fix: resolve mypy and lint errors for jinja2 templating
- Annotate the inner _artifact helper with a -> str return type to satisfy mypy --strict (no-untyped-def). - Assign render_async() result to a str-typed variable before returning so mypy does not flag returning Any from a str-typed function (no-any-return). - Collapse the Jinja2 conditional test template onto a single line to match pyink formatting.
1 parent 0632736 commit d2c31dc

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/google/adk/utils/instructions_utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ async def _render_with_jinja2(
216216
invocation_context = readonly_context._invocation_context
217217
session_state = invocation_context.session.state
218218

219-
async def _artifact(name: str):
219+
async def _artifact(name: str) -> str:
220220
if invocation_context.artifact_service is None:
221221
raise ValueError('Artifact service is not initialized.')
222222
artifact = await invocation_context.artifact_service.load_artifact(
@@ -229,10 +229,11 @@ async def _artifact(name: str):
229229

230230
env = SandboxedEnvironment(enable_async=True)
231231
jinja_template = env.from_string(template)
232-
return await jinja_template.render_async(
232+
rendered: str = await jinja_template.render_async(
233233
state=session_state,
234234
artifact=_artifact,
235235
)
236+
return rendered
236237

237238

238239
def _is_valid_state_name(var_name):

tests/unittests/utils/test_instructions_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,7 @@ async def test_inject_session_state_jinja2_basic_substitution():
285285
@pytest.mark.asyncio
286286
async def test_inject_session_state_jinja2_conditional():
287287
instruction_template = (
288-
"{% if state['is_premium'] %}Premium user.{% else %}Free user."
289-
"{% endif %}"
288+
"{% if state['is_premium'] %}Premium user.{% else %}Free user.{% endif %}"
290289
)
291290
premium_context = await _create_test_readonly_context(
292291
state={"is_premium": True}

0 commit comments

Comments
 (0)