11from __future__ import annotations
22
33from collections .abc import Generator
4+ from contextlib import contextmanager
45from pathlib import Path
56
67import pytest
7- from _pytest .fixtures import SubRequest
88
99from pydocstringformatter import _formatting
1010from pydocstringformatter import _testutils as test_utils
11+ from pydocstringformatter ._formatting import Formatter
1112from pydocstringformatter ._utils import UnstableResultError
1213from pydocstringformatter .run import _Run
1314
1415
15- @pytest . fixture ( scope = "function" )
16- def patched_run (request : SubRequest ) -> Generator [type [_Run ], None , None ]:
17- """Patches the _Run class to use the MakeAFormatter and MakeBFormatter ."""
16+ @contextmanager
17+ def patched_run (formatters : list [ Formatter ] ) -> Generator [type [_Run ], None , None ]:
18+ """Patches the _Run class to use only the provided formatters ."""
1819 # This monkeypatches the formatters to make them conflict.
1920 # and the only formatters applied
2021
2122 old_formatters = _formatting .FORMATTERS
22- _formatting . FORMATTERS = request . param
23-
24- def cleanup () -> None :
25- """Returns the formatters to their original state."""
23+ try :
24+ _formatting . FORMATTERS = formatters
25+ yield _Run
26+ finally :
2627 _formatting .FORMATTERS = old_formatters
2728
28- yield _Run
29- cleanup ()
30-
3129
3230@pytest .mark .parametrize (
33- "patched_run,expect_errors " ,
31+ "formatters,expected_errors " ,
3432 [
3533 (
3634 [test_utils .MakeAFormatter (), test_utils .MakeBFormatter ()],
@@ -49,11 +47,10 @@ def cleanup() -> None:
4947 ["Conflicting formatters:" , "Diff too intricate to compute" ],
5048 ),
5149 ],
52- indirect = ["patched_run" ],
5350)
5451def test_conflicting_formatters (
55- patched_run : type [ _Run ], # pylint: disable=redefined-outer-name
56- expect_errors : list [str ],
52+ formatters : list [ Formatter ], # pylint: disable=redefined-outer-name
53+ expected_errors : list [str ],
5754 tmp_path : Path ,
5855) -> None :
5956 """Tests that conflicting formatters raise an error."""
@@ -64,8 +61,9 @@ def test_conflicting_formatters(
6461 ]
6562 f .writelines (content )
6663
67- with pytest .raises (UnstableResultError ) as err :
68- patched_run ([str (tmp_file )])
64+ with patched_run (formatters ) as run :
65+ with pytest .raises (UnstableResultError ) as err :
66+ run ([str (tmp_file )])
6967
70- for expect_err in expect_errors :
68+ for expect_err in expected_errors :
7169 assert expect_err in str (err .value ), str (err .value )
0 commit comments