Each endpoint Python source sets to a constant string, as (name, value): a keyword argument (`LLM(base_url=...)`), a dict entry (`client_params`), an assignment, `os.environ[...] = ...` included, or `setdefault`/`putenv`. Parsed, not matched, so line breaks, parentheses and annotations
(source: str)
| 388 | |
| 389 | |
| 390 | def _code_endpoints(source: str) -> Iterator[tuple[str, str]]: |
| 391 | """Each endpoint Python source sets to a constant string, as (name, value): |
| 392 | a keyword argument (`LLM(base_url=...)`), a dict entry (`client_params`), |
| 393 | an assignment, `os.environ[...] = ...` included, or `setdefault`/`putenv`. |
| 394 | |
| 395 | Parsed, not matched, so line breaks, parentheses and annotations do not |
| 396 | hide one. A computed value is not read: that code runs with the key and |
| 397 | could send it anywhere regardless. |
| 398 | """ |
| 399 | try: |
| 400 | tree = ast.parse(source) |
| 401 | except (SyntaxError, ValueError): |
| 402 | return # crewAI cannot import it either |
| 403 | for node in ast.walk(tree): |
| 404 | pairs: list[tuple[str | None, ast.expr | None]] = [] |
| 405 | if isinstance(node, ast.keyword): |
| 406 | pairs.append((node.arg, node.value)) |
| 407 | elif isinstance(node, ast.Dict): |
| 408 | pairs += [ |
| 409 | (_constant(key), value) |
| 410 | for key, value in zip(node.keys, node.values, strict=True) |
| 411 | ] |
| 412 | elif isinstance(node, ast.Assign | ast.AnnAssign): |
| 413 | targets = node.targets if isinstance(node, ast.Assign) else [node.target] |
| 414 | pairs += [(_target_name(target), node.value) for target in targets] |
| 415 | elif ( |
| 416 | isinstance(node, ast.Call) |
| 417 | and isinstance(node.func, ast.Attribute) |
| 418 | and node.func.attr in ("setdefault", "putenv") |
| 419 | and len(node.args) >= 2 |
| 420 | ): |
| 421 | pairs.append((_constant(node.args[0]), node.args[1])) |
| 422 | for name, value in pairs: |
| 423 | literal = _constant(value) |
| 424 | if ( |
| 425 | name |
| 426 | and literal is not None |
| 427 | and (_sets_endpoint(name) or name.lower() == "endpoint") |
| 428 | ): |
| 429 | yield name, literal |
| 430 | |
| 431 | |
| 432 | def _constant(node: ast.expr | None) -> str | None: |
no test coverage detected