12
- Multi-execute routing (all-local, all-remote, mixed, parallel)
13
"""
14
15
+import sys
16
from dataclasses import replace
17
from unittest.mock import MagicMock
18
314
local_tool, local_input = build_tool()
315
assert local_tool.input_params is local_input
316
317
+ def test_input_model_defined_after_tool_names_unresolved_annotation(self):
318
+ # https://github.com/ComposioHQ/composio/issues/5022
319
+ with pytest.raises(
320
+ ValidationError,
321
+ match=(
322
+ r"could not resolve the annotation 'LaterInput' .*"
323
+ r"NameError: name 'LaterInput' is not defined.*"
324
+ r"Define LaterInput before the tool"
325
+ ),
326
+ ):
327
+ exec(
328
+ """
329
+from __future__ import annotations
330
+
331
+from pydantic import BaseModel
332
+
333
+
334
+@exp.tool()
335
+def later_tool(input: LaterInput, ctx):
336
+ '''Input model defined below the tool.'''
337
+ return {}
338
+
339
+
340
+class LaterInput(BaseModel):
341
+ query: str
342
+""",
343
+ {"exp": exp},
344
+ )
345
+
346
+ def test_input_model_imported_under_type_checking_names_unresolved_annotation(
347
+ self,
348
+ ):
349
+ with pytest.raises(
350
+ ValidationError,
351
+ match=r"could not resolve the annotation 'GrepInput' .*TYPE_CHECKING",
352
+ ):
353
+ exec(
354
+ """
355
+from __future__ import annotations
356
+
357
+from typing import TYPE_CHECKING
358
+
359
+if TYPE_CHECKING:
360
+ from tests.test_custom_tools import GrepInput
361
+
362
+
363
+@exp.tool()
364
+def type_checking_tool(input: GrepInput, ctx):
365
+ '''Input model only imported for type checkers.'''
366
+ return {}
367
+""",
368
+ {"exp": exp},
369
+ )
370
+
371
+ def test_unresolvable_ctx_and_return_annotations_are_ignored(self):
372
+ namespace = {"exp": exp}
373
+ exec(
374
+ """
375
+from __future__ import annotations
376
+
377
+from typing import TYPE_CHECKING
378
+
379
+from pydantic import BaseModel
380
+
381
+if TYPE_CHECKING:
382
+ from composio.core.models.custom_tool import SessionContext
383
+
384
+
385
+class TypedInput(BaseModel):
386
+ query: str
387
+
388
+
389
+@exp.tool()
390
+def typed_ctx_tool(input: TypedInput, ctx: SessionContext) -> ToolResult:
391
+ '''ctx and return annotations exist only for type checkers.'''
392
+ return {}
393
+""",
394
+ namespace,
395
+ )
396
+
397
+ assert namespace["typed_ctx_tool"].input_params is namespace["TypedInput"]
398
+
399
+ @pytest.mark.skipif(
400
+ sys.version_info < (3, 14), reason="lazy annotations need Python 3.14"
401
+ )
402
+ def test_lazy_input_model_defined_after_tool_names_unresolved_annotation(self):
403
+ # dont_inherit keeps this module's compiler flags out, so the
404
+ # annotations stay lazy instead of becoming strings.
405
+ code = compile(
406
+ """
407
+from pydantic import BaseModel
408
+
409
+
410
+@exp.tool()
411
+def lazy_later_tool(input: LazyLaterInput, ctx):
412
+ '''Input model defined below the tool.'''
413
+ return {}
414
+
415
+
416
+class LazyLaterInput(BaseModel):
417
+ query: str
418
+""",
419
+ "<lazy_later_tool>",
420
+ "exec",
421
+ dont_inherit=True,
422
+ )
423
+ with pytest.raises(
424
+ ValidationError,
425
+ match=r"could not resolve the annotation 'LazyLaterInput' .*NameError",
426
+ ):
427
+ exec(code, {"exp": exp})
428
+
429
+ @pytest.mark.skipif(
430
+ sys.version_info < (3, 14), reason="lazy annotations need Python 3.14"
431
+ )
432
+ def test_lazy_unresolvable_ctx_and_return_annotations_are_ignored(self):
433
+ code = compile(
434
+ """
435
+from typing import TYPE_CHECKING
436
+
437
+from pydantic import BaseModel
438
+
439
+if TYPE_CHECKING:
440
+ from composio.core.models.custom_tool import SessionContext
441
+
442
+
443
+class LazyInput(BaseModel):
444
+ query: str
445
+
446
+
447
+@exp.tool()
448
+def lazy_ctx_tool(input: LazyInput, ctx: SessionContext) -> ToolResult:
449
+ '''ctx and return annotations exist only for type checkers.'''
450
+ return {}
451
+""",
452
+ "<lazy_ctx_tool>",
453
+ "exec",
454
+ dont_inherit=True,
455
+ )
456
+ namespace = {"exp": exp}
457
+ exec(code, namespace)
458
+
459
+ assert namespace["lazy_ctx_tool"].input_params is namespace["LazyInput"]
460
+
461
def test_async_function_rejected(self):
462
with pytest.raises(ValidationError, match="async"):
463