Add tests to deprecations. Better deprecate streams.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Eddie Girling · committed by Eddie Girling · parent 7a0a344 · Browse files · 5 files·+48 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+48 added−25 removed
- tests:+32 added−7 removed
- other:+16 added−18 removed
Provenance
- Commit
051725f- Tree
d05c382- Author
Eddie Girling <67291205+Rowlando13@users.noreply.github.com>- Committer
Eddie Girling <67291205+Rowlando13@users.noreply.github.com>- Parent
7a0a344
5 files changed+48 added−25 removed
modified:docs/handling-files.md+1 added-2 removed
86 unmodified lines87888990919291929394956 unmodified lines86 unmodified lines
File open behavior can be controlled by the boolean kwarg `lazy`. If a file is opened lazily:
- A failure at first IO operation will happen by raising an {exc}`FileError`.- It can help minimize resource handling confusion. If a file is opened in lazy mode, it will call {meth}`_LazyFile.close_intelligently` to help figure out if the file needs closing or not. This is not needed for- It can help minimize resource handling confusion by lazily closing it. This is not needed for parameters, but is necessary for manually prompting. For manual prompts with the {func}`prompt` function you do not know if a stream like stdout was opened (which was already open before) or a real file was opened (that needs closing).
6 unmodified linesmodified:src/click/__init__.py+4 added-4 removed
120 unmodified lines121 )122 return importlib.metadata.version("click")123 124 if name == "get_binary_stream":125- from .utils import get_binary_stream125+ from .utils import _get_binary_stream126 127 warnings.warn(128 "'get_binary_stream' is deprecated and will be removed in Click 9.0.",129 DeprecationWarning,130 stacklevel=2,131 )132- return get_binary_stream132+ return _get_binary_stream133 134 if name == "get_text_stream":135- from .utils import get_text_stream135+ from .utils import _get_text_stream136 137 warnings.warn(138 "'get_text_stream' is deprecated and will be removed in Click 9.0.",139 DeprecationWarning,140 stacklevel=2,141 )142- return get_text_stream142+ return _get_text_stream143 144 raise AttributeError(name)modified:src/click/utils.py+11 added-12 removed
32 unmodified lines33 return "-".join(name.split()).lower()34 35 36 def _safecall(func: t.Callable[P, R]) -> t.Callable[P, R | None]:37- """38- Wraps a function so that it swallows exceptions.37+ """Wraps a function so that it swallows exceptions.38 39 :meta private:40 """41 70 unmodified lines112 return " ".join(words[:i]) + "..."113 114 115 class _LazyFile:117- """118- A lazy file works like a regular file but it does not fully open116+ """A lazy file works like a regular file but it does not fully open117 the file but it does perform some basic checks early to see if the118 filename parameter does make sense. This is useful for safely opening119 files for writing.120 121 :meta private:124-122 """123 124 name: str125 mode: str219 unmodified lines345 file.write(out) # type: ignore346 file.flush()347 348 352-def get_binary_stream(name: t.Literal["stdin", "stdout", "stderr"]) -> t.BinaryIO:349+def _get_binary_stream(name: t.Literal["stdin", "stdout", "stderr"]) -> t.BinaryIO:350 """Returns a system stream for byte processing.351 355- .. deprecated:: 8.5352+ .. deprecated:: 8.5.0353 Will be removed in Click 9.0.354 355 :param name: the name of the stream to open. Valid names are ``'stdin'``,356 ``'stdout'`` and ``'stderr'``5 unmodified lines362 raise TypeError(_("Unknown standard stream '{name}'").format(name=name))363 return opener()364 365 369-def get_text_stream(366+def _get_text_stream(367 name: t.Literal["stdin", "stdout", "stderr"],368 encoding: str | None = None,369 errors: str | None = "strict",370 ) -> t.TextIO:371 """Returns a system stream for text processing.372 376- .. deprecated:: 8.5373+ .. deprecated:: 8.5.0374 Will be removed in Click 9.0.375 376 This usually returns a wrapped stream around a binary stream returned from377 :func:`get_binary_stream` but it also can take shortcuts for already378 correctly configured streams.379 383- :meta private:380+381 :param name: the name of the stream to open. Valid names are ``'stdin'``,382 ``'stdout'`` and ``'stderr'``383 :param encoding: overrides the detected default encoding.384 :param errors: overrides the default error mode.385+ :meta private:386 """387 opener = text_streams.get(name)388 if opener is None:389 raise TypeError(_("Unknown standard stream '{name}'").format(name=name))284 unmodified lines674 "KeepOpenFile",675 "make_default_short_help",676 "PacifyFlushWrapper",677 "safecall",678+ "get_text_stream",679+ "get_binary_stream",680 }:681 warnings.warn(682- f"'click.utils.{name}' was never intentionally public and will be"683- " renamed in Click 9.0.",682+ f"'click.utils.{name}' is deprecated and will be removed in Click 9.0.",683 DeprecationWarning,684 stacklevel=2,685 )686 return globals()[f"_{name}"]2 unmodified linesadded:tests/test_deprecations.py+25 added
1+import pytest2+3+import click4+5+6+@pytest.mark.parametrize("module", [click, click.utils], ids=["click", "click.utils"])7+@pytest.mark.parametrize("name", ["get_binary_stream", "get_text_stream"])8+def test_stream_helper_deprecated(module, name):9+ with pytest.warns(DeprecationWarning, match=name):10+ getattr(module, name)11+12+13+@pytest.mark.parametrize(14+ "name",15+ [16+ "LazyFile",17+ "KeepOpenFile",18+ "make_default_short_help",19+ "PacifyFlushWrapper",20+ "safecall",21+ ],22+)23+def test_utilities_deprecated(name):24+ with pytest.warns(DeprecationWarning, match=name):25+ getattr(click.utils, name)modified:tests/test_testing.py+7 added-7 removed
8 unmodified lines9 10 import click11 from click.exceptions import ClickException12 from click.testing import CliRunner13-from click.utils import get_binary_stream13+from click.utils import _get_binary_stream14 15 16 def test_runner():17 @click.command()18 def test():19- i = get_binary_stream("stdin")20- o = get_binary_stream("stdout")19+ i = _get_binary_stream("stdin")20+ o = _get_binary_stream("stdout")21 while True:22 chunk = i.read(4096)23 if not chunk:24 break8 unmodified lines33 34 def test_echo_stdin_stream():35 @click.command()36 def test():37- i = get_binary_stream("stdin")38- o = get_binary_stream("stdout")37+ i = _get_binary_stream("stdin")38+ o = _get_binary_stream("stdout")39 while True:40 chunk = i.read(4096)41 if not chunk:42 break48 unmodified lines91 92 def test_runner_with_stream():93 @click.command()94 def test():95- i = get_binary_stream("stdin")96- o = get_binary_stream("stdout")95+ i = _get_binary_stream("stdin")96+ o = _get_binary_stream("stdout")97 while True:98 chunk = i.read(4096)99 if not chunk:100 break661 unmodified lines