Debian Codegraph @

tests/test_py314_annotations.py

raw
1 """Regression tests for issue #636.
2
3 On Python 3.14+, ``inspect.signature`` and ``inspect.getfullargspec`` eagerly
4 evaluate annotations via PEP 649's ``__annotate__``. Functions whose
5 annotations reference names defined only under ``if TYPE_CHECKING:`` must
6 still be memoizable / cacheable.
7 """
8
9 import sys
10 from typing import TYPE_CHECKING
11
12 import pytest
13
14 if TYPE_CHECKING:
15
16 class TypeCheckingOnly:
17 pass
18
19
20 pytestmark = pytest.mark.skipif(
21 sys.version_info < (3, 14),
22 reason="requires Python 3.14 __annotate__ / PEP 649 behavior",
23 )
24
25
26 def test_memoize_with_type_checking_annotation(cache):
27 @cache.memoize(60)
28 def fetch(x: int) -> TypeCheckingOnly:
29 return x * 2
30
31 assert fetch(3) == 6
32 # Cache-hit path also goes through get_function_parameters.
33 assert fetch(3) == 6
34
35
36 def test_cached_with_type_checking_annotation(app, cache):
37 @app.route("/py314-annot")
38 @cache.cached(60)
39 def view(extra: TypeCheckingOnly = None):
40 return "ok"
41
42 client = app.test_client()
43 assert client.get("/py314-annot").data == b"ok"
44 assert client.get("/py314-annot").data == b"ok"
45
46
47 def test_memoize_unless_with_type_checking_annotation(cache):
48 def skip(*args, **kwargs) -> TypeCheckingOnly:
49 return False
50
51 @cache.memoize(60, unless=skip)
52 def fetch(x: int) -> TypeCheckingOnly:
53 return x + 1
54
55 assert fetch(1) == 2