|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import textwrap |
| 6 | +from dataclasses import dataclass |
| 7 | +from pathlib import Path |
| 8 | +from subprocess import check_output |
| 9 | +from typing import Any, Iterator |
| 10 | + |
| 11 | +import pytest |
| 12 | +from more_itertools import one |
| 13 | + |
| 14 | +from .. import cachew |
| 15 | + |
| 16 | + |
| 17 | +# fmt: off |
| 18 | +@dataclass |
| 19 | +class NewStyleTypes1: |
| 20 | + a_str : str |
| 21 | + a_dict : dict[str, Any] |
| 22 | + a_list : list[Any] |
| 23 | + a_tuple : tuple[float, str] |
| 24 | +# fmt: on |
| 25 | + |
| 26 | + |
| 27 | +def test_types1(tmp_path: Path) -> None: |
| 28 | + if sys.version_info[:2] <= (3, 8): |
| 29 | + pytest.skip("too annoying to adjust for 3.8 and it's EOL soon anyway") |
| 30 | + |
| 31 | + # fmt: off |
| 32 | + obj = NewStyleTypes1( |
| 33 | + a_str = 'abac', |
| 34 | + a_dict = {'a': True, 'x': {'whatever': 3.14}}, |
| 35 | + a_list = ['aba', 123, None], |
| 36 | + a_tuple = (1.23, '3.2.1'), |
| 37 | + ) |
| 38 | + # fmt: on |
| 39 | + |
| 40 | + @cachew(tmp_path) |
| 41 | + def get() -> Iterator[NewStyleTypes1]: |
| 42 | + yield obj |
| 43 | + |
| 44 | + assert one(get()) == obj |
| 45 | + assert one(get()) == obj |
| 46 | + |
| 47 | + |
| 48 | +# fmt: off |
| 49 | +@dataclass |
| 50 | +class NewStyleTypes2: |
| 51 | + an_opt : str | None |
| 52 | + a_union : str | int |
| 53 | +# fmt: on |
| 54 | + |
| 55 | + |
| 56 | +def test_types2(tmp_path: Path) -> None: |
| 57 | + if sys.version_info[:2] <= (3, 9): |
| 58 | + pytest.skip("can only use new style union types from 3.10") |
| 59 | + |
| 60 | + # fmt: off |
| 61 | + obj = NewStyleTypes2( |
| 62 | + an_opt = 'hello', |
| 63 | + a_union = 999, |
| 64 | + ) |
| 65 | + # fmt: on |
| 66 | + |
| 67 | + @cachew(tmp_path) |
| 68 | + def get() -> Iterator[NewStyleTypes2]: |
| 69 | + yield obj |
| 70 | + |
| 71 | + assert one(get()) == obj |
| 72 | + assert one(get()) == obj |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize('use_future_annotations', [False, True]) |
| 76 | +@pytest.mark.parametrize('local', [False, True]) |
| 77 | +@pytest.mark.parametrize('throw', [False, True]) |
| 78 | +def test_future_annotations( |
| 79 | + *, |
| 80 | + use_future_annotations: bool, |
| 81 | + local: bool, |
| 82 | + throw: bool, |
| 83 | + tmp_path: Path, |
| 84 | +) -> None: |
| 85 | + """ |
| 86 | + Checks handling of postponed evaluation of annotations (from __future__ import annotations) |
| 87 | + """ |
| 88 | + |
| 89 | + if sys.version_info[:2] <= (3, 8): |
| 90 | + pytest.skip("too annoying to adjust for 3.8 and it's EOL soon anyway") |
| 91 | + |
| 92 | + # NOTE: to avoid weird interactions with existing interpreter in which pytest is running |
| 93 | + # , we compose a program and running in python directly instead |
| 94 | + # (also not sure if it's even possible to tweak postponed annotations without doing that) |
| 95 | + |
| 96 | + if use_future_annotations and local and throw: |
| 97 | + # when annotation is local (like inner class), then they end up as strings |
| 98 | + # so we can't eval it as we don't have access to a class defined inside function |
| 99 | + # keeping this test just to keep track of whether this is fixed at some point |
| 100 | + # possibly relevant: |
| 101 | + # - https://peps.python.org/pep-0563/#keeping-the-ability-to-use-function-local-state-when-defining-annotations |
| 102 | + pytest.skip("local aliases/classses don't work with from __future__ import annotations") |
| 103 | + |
| 104 | + _PREAMBLE = f''' |
| 105 | +from pathlib import Path |
| 106 | +import tempfile |
| 107 | +
|
| 108 | +from cachew import cachew, settings |
| 109 | +settings.THROW_ON_ERROR = {throw} |
| 110 | +
|
| 111 | +temp_dir = tempfile.TemporaryDirectory() |
| 112 | +td = Path(temp_dir.name) |
| 113 | +
|
| 114 | +''' |
| 115 | + |
| 116 | + _TEST = ''' |
| 117 | +T = int |
| 118 | +
|
| 119 | +@cachew(td) |
| 120 | +def fun() -> list[T]: |
| 121 | + print("called") |
| 122 | + return [1, 2] |
| 123 | +
|
| 124 | +assert list(fun()) == [1, 2] |
| 125 | +assert list(fun()) == [1, 2] |
| 126 | +''' |
| 127 | + |
| 128 | + if use_future_annotations: |
| 129 | + code = ''' |
| 130 | +from __future__ import annotations |
| 131 | +''' |
| 132 | + else: |
| 133 | + code = '' |
| 134 | + |
| 135 | + code += _PREAMBLE |
| 136 | + |
| 137 | + if local: |
| 138 | + code += f''' |
| 139 | +def test() -> None: |
| 140 | +{textwrap.indent(_TEST, prefix=" ")} |
| 141 | +
|
| 142 | +test() |
| 143 | +''' |
| 144 | + else: |
| 145 | + code += _TEST |
| 146 | + |
| 147 | + run_py = tmp_path / 'run.py' |
| 148 | + run_py.write_text(code) |
| 149 | + |
| 150 | + cache_dir = tmp_path / 'cache' |
| 151 | + cache_dir.mkdir() |
| 152 | + |
| 153 | + res = check_output( |
| 154 | + [sys.executable, run_py], |
| 155 | + env={'TMPDIR': str(cache_dir), **os.environ}, |
| 156 | + text=True, |
| 157 | + ) |
| 158 | + called = int(res.count('called')) |
| 159 | + if use_future_annotations and local and not throw: |
| 160 | + # cachew fails to set up, so no caching but at least it works otherwise |
| 161 | + assert called == 2 |
| 162 | + else: |
| 163 | + assert called == 1 |
0 commit comments