type is always IOBase, handle lazy file
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 2ddc9af · Browse files · 5 files·+22 added−18 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+22 added−18 removed
- tests:+14 added−12 removed
- other:+8 added−6 removed
Provenance
- Commit
b830139- Tree
1cf5cb7- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
2ddc9af
5 files changed+22 added−18 removed
modified:click/_compat.py-2 removed
154 unmodified lines155 156 157 if PY2:158 text_type = unicode159- file_type = file159 raw_input = raw_input160 string_types = (str, unicode)161 int_types = (int, long)162 iteritems = lambda x: x.iteritems()97 unmodified lines260 return value261 else:262 import io263 text_type = str265- file_type = io.TextIOWrapper264 raw_input = input265 string_types = (str,)266 int_types = (int,)267 range_type = range503 unmodified linesmodified:click/termui.py+6 added-3 removed
1+import io2 import os3 import sys4 import struct5 import inspect6 import itertools7 8 from ._compat import raw_input, text_type, string_types, \8- isatty, strip_ansi, get_winterm_size, DEFAULT_COLUMNS, WIN, file_type9+ isatty, strip_ansi, get_winterm_size, DEFAULT_COLUMNS, WIN10 from .utils import echo11 from .exceptions import Abort, UsageError12 from .types import convert_type, Choice, Path13 from .globals import resolve_color_default14+from .utils import LazyFile15 16 17 # The prompt functions to use. The doc tools currently override these18 # functions to customize how they work.35 unmodified lines54 return prompt + suffix55 56 57 def _format_default(default):56- if isinstance(default, file_type):57- default = default.name58+ if isinstance(default, (io.IOBase, LazyFile)) and hasattr(default, "name"):59+ return default.name60+61 return default62 63 64 def prompt(text, default=None, hide_input=False, confirmation_prompt=False,557 unmodified linesdeleted:tests/static/config.json-3 removed
123{ "TEST_KEY": "foo"}modified:tests/test_termui.py+14 added-9 removed
1 unmodified line2 import pytest3 4 import click5 import time6-import os6 7 import click._termui_impl8 from click._compat import WIN9 214 unmodified lines224 result = runner.invoke(cli_without_choices, [], input='none')225 assert '(none, day, week, month)' not in result.output226 227 229-def test_default_prompt_format(runner):230- path_default = os.path.join(os.path.dirname(__file__), "static/config.json")231-228+@pytest.mark.parametrize(229+ "file_kwargs",230+ [231+ {"mode": "rt"},232+ {"mode": "rb"},233+ {"lazy": True},234+ ]235+)236+def test_file_prompt_default_format(runner, file_kwargs):237 @click.command()233- @click.option('-f', default=path_default, prompt='file', type=click.File('r'))234- def cli_with_text_io_wrapper_default(f):235- pass238+ @click.option("-f", default=__file__, prompt="file", type=click.File(**file_kwargs))239+ def cli(f):240+ click.echo(f.name)241 237- result = runner.invoke(cli_with_text_io_wrapper_default, [])238- assert result.output == 'file [%s]: \n' % path_default242+ result = runner.invoke(cli)243+ assert result.output == "file [{0}]: \n{0}\n".format(__file__)244 245 246 def test_secho(runner):247 with runner.isolation() as outstreams:66 unmodified linesmodified:CHANGES.rst+2 added-1 removed
40 unmodified lines41 - Newlines in option help text are replaced with spaces before42 re-wrapping to avoid uneven line breaks. :issue:`834`43 - ``MissingParameter`` exceptions are printable in the Python44 interpreter. :issue:`1139`45-- File option prompting now displays path only. :issue:`914`45+- Fix how default values for file-type options are shown during46+ prompts. :issue:`914`47 48 49 Version 7.050 -----------540 unmodified lines