|
| 1 | +"""Tests that the @cachier decorator preserves function type signatures. |
| 2 | +
|
| 3 | +These tests invoke mypy programmatically and assert that decorated functions retain their parameter types and return |
| 4 | +types as seen by static analysis. |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +import textwrap |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | +mypy_api = pytest.importorskip("mypy.api", reason="mypy is required for typing tests") |
| 13 | + |
| 14 | + |
| 15 | +def _run_mypy(code: str) -> tuple[list[str], list[str]]: |
| 16 | + """Run mypy on a code snippet and return (notes, errors). |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + code : str |
| 21 | + Python source code to type-check. |
| 22 | +
|
| 23 | + Returns |
| 24 | + ------- |
| 25 | + tuple[list[str], list[str]] |
| 26 | + A tuple of (note lines, error lines) from mypy output. |
| 27 | +
|
| 28 | + """ |
| 29 | + result = mypy_api.run( |
| 30 | + [ |
| 31 | + "-c", |
| 32 | + textwrap.dedent(code), |
| 33 | + "--no-error-summary", |
| 34 | + "--hide-error-context", |
| 35 | + ] |
| 36 | + ) |
| 37 | + stdout = result[0] |
| 38 | + notes = [] |
| 39 | + errors = [] |
| 40 | + for line in stdout.splitlines(): |
| 41 | + if ": note:" in line: |
| 42 | + notes.append(line) |
| 43 | + elif ": error:" in line: |
| 44 | + errors.append(line) |
| 45 | + return notes, errors |
| 46 | + |
| 47 | + |
| 48 | +class TestSyncTyping: |
| 49 | + """Verify that synchronous decorated functions preserve types.""" |
| 50 | + |
| 51 | + def test_return_type_preserved(self) -> None: |
| 52 | + """Mypy should infer the original return type through @cachier.""" |
| 53 | + notes, errors = _run_mypy(""" |
| 54 | + from cachier import cachier |
| 55 | +
|
| 56 | + @cachier() |
| 57 | + def my_func(x: int) -> str: |
| 58 | + return str(x) |
| 59 | +
|
| 60 | + reveal_type(my_func(5)) |
| 61 | + """) |
| 62 | + assert not errors |
| 63 | + assert any('"str"' in n for n in notes) |
| 64 | + |
| 65 | + def test_param_types_preserved(self) -> None: |
| 66 | + """Mypy should see the original parameter types through @cachier.""" |
| 67 | + notes, errors = _run_mypy(""" |
| 68 | + from cachier import cachier |
| 69 | +
|
| 70 | + @cachier() |
| 71 | + def my_func(x: int, y: str) -> list[str]: |
| 72 | + return [y] * x |
| 73 | +
|
| 74 | + reveal_type(my_func) |
| 75 | + """) |
| 76 | + assert not errors |
| 77 | + assert any("int" in n and "str" in n for n in notes) |
| 78 | + |
| 79 | + def test_wrong_arg_type_is_error(self) -> None: |
| 80 | + """Mypy should reject calls with wrong argument types.""" |
| 81 | + _notes, errors = _run_mypy(""" |
| 82 | + from cachier import cachier |
| 83 | +
|
| 84 | + @cachier() |
| 85 | + def add(a: int, b: int) -> int: |
| 86 | + return a + b |
| 87 | +
|
| 88 | + add("not", "ints") |
| 89 | + """) |
| 90 | + assert errors |
| 91 | + |
| 92 | + def test_return_type_mismatch_is_error(self) -> None: |
| 93 | + """Mypy should catch assigning the result to an incompatible type.""" |
| 94 | + _notes, errors = _run_mypy(""" |
| 95 | + from cachier import cachier |
| 96 | +
|
| 97 | + @cachier() |
| 98 | + def get_name() -> str: |
| 99 | + return "hello" |
| 100 | +
|
| 101 | + x: int = get_name() |
| 102 | + """) |
| 103 | + assert errors |
| 104 | + |
| 105 | + |
| 106 | +class TestAsyncTyping: |
| 107 | + """Verify that async decorated functions preserve types.""" |
| 108 | + |
| 109 | + def test_async_return_type_preserved(self) -> None: |
| 110 | + """Mypy should infer the awaited return type for async functions.""" |
| 111 | + notes, errors = _run_mypy(""" |
| 112 | + import asyncio |
| 113 | + from cachier import cachier |
| 114 | +
|
| 115 | + @cachier() |
| 116 | + async def fetch(url: str) -> bytes: |
| 117 | + return b"data" |
| 118 | +
|
| 119 | + async def main() -> None: |
| 120 | + result = await fetch("http://example.com") |
| 121 | + reveal_type(result) |
| 122 | +
|
| 123 | + asyncio.run(main()) |
| 124 | + """) |
| 125 | + assert not errors |
| 126 | + assert any('"bytes"' in n for n in notes) |
| 127 | + |
| 128 | + def test_async_signature_preserved(self) -> None: |
| 129 | + """Mypy should see the async function as a coroutine.""" |
| 130 | + notes, errors = _run_mypy(""" |
| 131 | + from cachier import cachier |
| 132 | +
|
| 133 | + @cachier() |
| 134 | + async def fetch(url: str) -> bytes: |
| 135 | + return b"data" |
| 136 | +
|
| 137 | + reveal_type(fetch) |
| 138 | + """) |
| 139 | + assert not errors |
| 140 | + assert any("Coroutine" in n for n in notes) |
| 141 | + |
| 142 | + def test_async_wrong_arg_type_is_error(self) -> None: |
| 143 | + """Mypy should reject calls with wrong argument types for async.""" |
| 144 | + _notes, errors = _run_mypy(""" |
| 145 | + from cachier import cachier |
| 146 | +
|
| 147 | + @cachier() |
| 148 | + async def fetch(url: str) -> bytes: |
| 149 | + return b"data" |
| 150 | +
|
| 151 | + async def main() -> None: |
| 152 | + await fetch(123) |
| 153 | + """) |
| 154 | + assert errors |
| 155 | + |
| 156 | + |
| 157 | +class TestComplexSignatures: |
| 158 | + """Verify preservation of more complex type signatures.""" |
| 159 | + |
| 160 | + def test_optional_params(self) -> None: |
| 161 | + """Mypy should preserve Optional parameter types.""" |
| 162 | + notes, errors = _run_mypy(""" |
| 163 | + from typing import Optional |
| 164 | + from cachier import cachier |
| 165 | +
|
| 166 | + @cachier() |
| 167 | + def greet(name: str, greeting: Optional[str] = None) -> str: |
| 168 | + return f"{greeting or 'Hello'}, {name}" |
| 169 | +
|
| 170 | + reveal_type(greet) |
| 171 | + """) |
| 172 | + assert not errors |
| 173 | + assert any("str" in n for n in notes) |
| 174 | + |
| 175 | + def test_generic_return_type(self) -> None: |
| 176 | + """Mypy should preserve generic return types like dict.""" |
| 177 | + notes, errors = _run_mypy(""" |
| 178 | + from cachier import cachier |
| 179 | +
|
| 180 | + @cachier() |
| 181 | + def make_mapping(keys: list[str], value: int) -> dict[str, int]: |
| 182 | + return {k: value for k in keys} |
| 183 | +
|
| 184 | + reveal_type(make_mapping(["a"], 1)) |
| 185 | + """) |
| 186 | + assert not errors |
| 187 | + assert any("dict[str, int]" in n for n in notes) |
| 188 | + |
| 189 | + def test_none_return_type(self) -> None: |
| 190 | + """Mypy should preserve None return type.""" |
| 191 | + notes, errors = _run_mypy(""" |
| 192 | + from cachier import cachier |
| 193 | +
|
| 194 | + @cachier() |
| 195 | + def side_effect(x: int) -> None: |
| 196 | + pass |
| 197 | +
|
| 198 | + reveal_type(side_effect(1)) |
| 199 | + """) |
| 200 | + assert not errors |
| 201 | + assert any('"None"' in n for n in notes) |
| 202 | + |
| 203 | + |
| 204 | +class TestDecoratorWithArgs: |
| 205 | + """Verify typing works with various decorator arguments.""" |
| 206 | + |
| 207 | + def test_with_backend_arg(self) -> None: |
| 208 | + """Type preservation should work with explicit backend selection.""" |
| 209 | + notes, errors = _run_mypy(""" |
| 210 | + from cachier import cachier |
| 211 | +
|
| 212 | + @cachier(backend="memory") |
| 213 | + def compute(x: float) -> float: |
| 214 | + return x * 2.0 |
| 215 | +
|
| 216 | + reveal_type(compute(1.0)) |
| 217 | + """) |
| 218 | + assert not errors |
| 219 | + assert any('"float"' in n for n in notes) |
| 220 | + |
| 221 | + def test_with_stale_after_arg(self) -> None: |
| 222 | + """Type preservation should work with stale_after parameter.""" |
| 223 | + notes, errors = _run_mypy(""" |
| 224 | + from datetime import timedelta |
| 225 | + from cachier import cachier |
| 226 | +
|
| 227 | + @cachier(stale_after=timedelta(hours=1)) |
| 228 | + def lookup(key: str) -> list[int]: |
| 229 | + return [1, 2, 3] |
| 230 | +
|
| 231 | + reveal_type(lookup("x")) |
| 232 | + """) |
| 233 | + assert not errors |
| 234 | + assert any("list[int]" in n for n in notes) |
0 commit comments