File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from __future__ import annotations
22
33import codecs
4+ import functools
45import sys
56from typing import TYPE_CHECKING
67
@@ -97,5 +98,10 @@ def err_print(msg: str | Exception) -> None:
9798def warn_print (msg : str | Exception ) -> None :
9899 msg = str (msg )
99100
101+ _cached_warn_print (msg )
102+
103+
104+ @functools .cache
105+ def _cached_warn_print (msg : str ) -> None :
100106 if not usethis_config .quiet :
101107 console .print (f"⚠ { msg } " , style = "yellow" )
Original file line number Diff line number Diff line change 77import pytest
88
99from usethis ._config import usethis_config
10+ from usethis ._console import _cached_warn_print
1011from usethis ._integrations .backend .uv .call import call_subprocess , call_uv_subprocess
1112from usethis ._integrations .file .pyproject_toml .io_ import PyprojectTOMLManager
1213from usethis ._test import change_cwd , is_offline
14+ from usethis ._tool .impl .import_linter import _importlinter_warn_no_packages_found
1315
1416if "UV_PYTHON" in os .environ :
1517 # To allow test subprocesses to use different versions of Python than the one
1618 # running the tests.
1719 del os .environ ["UV_PYTHON" ]
1820
1921
22+ @pytest .fixture (autouse = True )
23+ def clear_functools_caches ():
24+ """Fixture to clear functools.caches before each test."""
25+
26+ _cached_warn_print .cache_clear ()
27+ _importlinter_warn_no_packages_found .cache_clear ()
28+
29+
2030@pytest .fixture (scope = "session" )
2131def _uv_init_dir (tmp_path_factory : pytest .TempPathFactory ) -> Path :
2232 tmp_path = tmp_path_factory .mktemp ("uv_init" )
Original file line number Diff line number Diff line change @@ -166,3 +166,23 @@ def test_alert_only_doesnt_suppress(
166166 out , err = capfd .readouterr ()
167167 assert not err
168168 assert out == "⚠ Hello\n "
169+
170+ def test_cached_str (self , capfd : pytest .CaptureFixture [str ]) -> None :
171+ # Act
172+ warn_print ("Hello" )
173+ warn_print ("Hello" )
174+
175+ # Assert
176+ out , err = capfd .readouterr ()
177+ assert not err
178+ assert out == "⚠ Hello\n "
179+
180+ def test_cached_exception (self , capfd : pytest .CaptureFixture [str ]) -> None :
181+ # Act
182+ warn_print (Exception ("Hello" ))
183+ warn_print (Exception ("Hello" ))
184+
185+ # Assert
186+ out , err = capfd .readouterr ()
187+ assert not err
188+ assert out == "⚠ Hello\n "
You can’t perform that action at this time.
0 commit comments