Debian Codegraph @

tests/test_utils/test_reflection.py

raw
1 from __future__ import annotations
2
3 import sys
4 from typing import Any, Dict
5
6 import pytest
7
8 from anthropic._utils import function_has_argument, signature_without_evaluating_annotations
9
10
11 @pytest.mark.skipif(sys.version_info < (3, 14), reason="annotations are evaluated lazily from Python 3.14")
12 def test_signature_without_evaluating_annotations_tolerates_undefined_names() -> None:
13 namespace: Dict[str, Any] = {}
14 source = "def func(a: OnlyForTypeCheckers, *, b: int = 1) -> OnlyForTypeCheckers: ..."
15 # dont_inherit: this module's `from __future__ import annotations` would turn the annotations into strings.
16 exec(compile(source, "<func>", "exec", dont_inherit=True), namespace)
17
18 signature = signature_without_evaluating_annotations(namespace["func"])
19 assert list(signature.parameters) == ["a", "b"]
20 assert signature.parameters["a"].annotation == "OnlyForTypeCheckers"
21 assert signature.parameters["b"].default == 1
22 assert function_has_argument(namespace["func"], "b")