|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import asyncio |
2 | 4 | import re |
| 5 | +from typing import TYPE_CHECKING |
3 | 6 | from unittest.mock import AsyncMock, patch |
4 | 7 |
|
5 | 8 | import pytest |
|
8 | 11 |
|
9 | 12 | from kaleido import Kaleido |
10 | 13 |
|
| 14 | +if TYPE_CHECKING: |
| 15 | + from typing import AsyncGenerator, Generator |
| 16 | + |
| 17 | + from kaleido import FigureDict |
| 18 | + |
11 | 19 |
|
12 | 20 | # can't do session scope because pytest complains that its used by |
13 | 21 | # function-scoped loops. tried to create a separate loop in here with |
@@ -40,7 +48,7 @@ async def test_write_fig_from_object_sync_generator(simple_figure_with_bytes, tm |
40 | 48 |
|
41 | 49 | file_paths = [] |
42 | 50 |
|
43 | | - def fig_generator(): |
| 51 | + def fig_generator() -> Generator[FigureDict, None]: |
44 | 52 | for i in range(2): |
45 | 53 | path = tmp_path / f"test_sync_{i}.png" |
46 | 54 | file_paths.append(path) |
@@ -70,7 +78,7 @@ async def test_write_fig_from_object_async_generator( |
70 | 78 |
|
71 | 79 | file_paths = [] |
72 | 80 |
|
73 | | - async def fig_async_generator(): |
| 81 | + async def fig_async_generator() -> AsyncGenerator[FigureDict, None]: |
74 | 82 | for i in range(2): |
75 | 83 | path = tmp_path / f"test_async_{i}.png" |
76 | 84 | file_paths.append(path) |
@@ -166,7 +174,7 @@ async def test_write_fig_from_object_bare_dictionary( |
166 | 174 |
|
167 | 175 | path1 = tmp_path / "test_dict_1.png" |
168 | 176 |
|
169 | | - fig_data = { |
| 177 | + fig_data: FigureDict = { |
170 | 178 | "fig": simple_figure_with_bytes["fig"], |
171 | 179 | "path": path1, |
172 | 180 | "opts": simple_figure_with_bytes["opts"], |
@@ -303,27 +311,19 @@ async def test_calc_fig_argument_passthrough( |
303 | 311 | # Extract the generator that was passed as first argument |
304 | 312 | _, kwargs = mock_write_fig_from_object.call_args # not sure. |
305 | 313 |
|
306 | | - generator = kwargs["fig_dicts"] |
| 314 | + fig_dict = kwargs["fig_dicts"] |
307 | 315 | assert kwargs["cancel_on_error"] is True |
308 | 316 | assert kwargs["_write"] is False |
309 | 317 |
|
310 | | - # Convert generator to list to inspect its contents |
311 | | - generated_args_list = [v async for v in generator] |
312 | | - assert len(generated_args_list) == 1, ( |
313 | | - "Expected generator to yield exactly one item" |
314 | | - ) |
315 | | - |
316 | | - generated_args = generated_args_list[0] |
317 | | - |
318 | 318 | # Validate that the generated arguments match what we passed to write_fig |
319 | | - assert "fig" in generated_args, "Generated args should contain 'fig'" |
320 | | - assert "opts" in generated_args, "Generated args should contain 'opts'" |
321 | | - assert "topojson" in generated_args, "Generated args should contain 'topojson'" |
| 319 | + assert "fig" in fig_dict, "Generated args should contain 'fig'" |
| 320 | + assert "opts" in fig_dict, "Generated args should contain 'opts'" |
| 321 | + assert "topojson" in fig_dict, "Generated args should contain 'topojson'" |
322 | 322 |
|
323 | 323 | # Check that the values match |
324 | | - assert generated_args["fig"] == fig, "Figure should match" |
325 | | - assert generated_args["opts"] == opts, "Options should match" |
326 | | - assert generated_args["topojson"] == topojson, "Topojson should match" |
| 324 | + assert fig_dict["fig"] == fig, "Figure should match" |
| 325 | + assert fig_dict["opts"] == opts, "Options should match" |
| 326 | + assert fig_dict["topojson"] == topojson, "Topojson should match" |
327 | 327 |
|
328 | 328 |
|
329 | 329 | async def test_kaleido_instantiate_no_hang(): |
@@ -374,7 +374,8 @@ async def test_all_methods_non_context(simple_figure_with_bytes, tmp_path): |
374 | 374 | expected_bytes = simple_figure_with_bytes["bytes"] |
375 | 375 |
|
376 | 376 | # Test without context manager |
377 | | - k = await Kaleido() |
| 377 | + k: Kaleido = Kaleido() |
| 378 | + await k # could do it on one line but it tricks typer |
378 | 379 | try: |
379 | 380 | # Test calc_fig |
380 | 381 | calc_bytes = await k.calc_fig(fig, opts=opts) |
|
0 commit comments