Skip to content
Myr

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

5 files+48 added−25 removed

modified:docs/handling-files.md+1 added-2 removed
modified:src/click/__init__.py+4 added-4 removed
120 unmodified lines
121 )122 return importlib.metadata.version("click")123124 if name == "get_binary_stream":125 from .utils import get_binary_stream125 from .utils import _get_binary_stream126127 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_stream133134 if name == "get_text_stream":135 from .utils import get_text_stream135 from .utils import _get_text_stream136137 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_stream143144 raise AttributeError(name)
modified:src/click/utils.py+11 added-12 removed
32 unmodified lines
33 return "-".join(name.split()).lower()343536def _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.3839 :meta private:40 """41
70 unmodified lines
112 return " ".join(words[:i]) + "..."113114115class _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.120121 :meta private:124122 """123124 name: str125 mode: str
219 unmodified lines
345 file.write(out) # type: ignore346 file.flush()347348352def get_binary_stream(name: t.Literal["stdin", "stdout", "stderr"]) -> t.BinaryIO:349def _get_binary_stream(name: t.Literal["stdin", "stdout", "stderr"]) -> t.BinaryIO:350 """Returns a system stream for byte processing.351355 .. deprecated:: 8.5352 .. deprecated:: 8.5.0353 Will be removed in Click 9.0.354355 :param name: the name of the stream to open. Valid names are ``'stdin'``,356 ``'stdout'`` and ``'stderr'``
5 unmodified lines
362 raise TypeError(_("Unknown standard stream '{name}'").format(name=name))363 return opener()364365369def get_text_stream(366def _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.372376 .. deprecated:: 8.5373 .. deprecated:: 8.5.0374 Will be removed in Click 9.0.375376 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.379383 :meta private:380381 :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 lines
674 "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 lines
added:tests/test_deprecations.py+25 added
1import pytest23import click456@pytest.mark.parametrize("module", [click, click.utils], ids=["click", "click.utils"])7@pytest.mark.parametrize("name", ["get_binary_stream", "get_text_stream"])8def test_stream_helper_deprecated(module, name):9 with pytest.warns(DeprecationWarning, match=name):10 getattr(module, name)111213@pytest.mark.parametrize(14 "name",15 [16 "LazyFile",17 "KeepOpenFile",18 "make_default_short_help",19 "PacifyFlushWrapper",20 "safecall",21 ],22)23def 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 lines
910import click11from click.exceptions import ClickException12from click.testing import CliRunner13from click.utils import get_binary_stream13from click.utils import _get_binary_stream141516def 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 break
8 unmodified lines
3334def 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 break
48 unmodified lines
9192def 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 break
661 unmodified lines