Skip to content
Myr

introduce int_types compat to fix python3 type error in _length_hint

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Ronny Pfannschmidt · committed by Ronny Pfannschmidt · parent 79e80fe · Browse files · 3 files+32 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+32 added−2 removed

  • tests:+28 added−0 removed
  • other:+4 added−2 removed

3 files+32 added−2 removed

modified:click/_compat.py+2 added
137 unmodified lines
138 text_type = unicode139 bytes = str140 raw_input = raw_input141 string_types = (str, unicode)142 int_types = (int, long)143 iteritems = lambda x: x.iteritems()144 range_type = xrange145146 def is_bytes(x):
65 unmodified lines
212 import io213 text_type = str214 raw_input = input215 string_types = (str,)216 int_types = (int,)217 range_type = range218 isidentifier = lambda x: x.isidentifier()219 iteritems = lambda x: iter(x.items())220
424 unmodified lines
modified:click/_termui_impl.py+2 added-2 removed
12 unmodified lines
13import sys14import time15import math16from ._compat import _default_text_stdout, range_type, PY2, isatty, \17 open_stream, strip_ansi, term_len, get_best_encoding, WIN17 open_stream, strip_ansi, term_len, get_best_encoding, WIN, int_types18from .utils import echo19from .exceptions import ClickException2021
18 unmodified lines
40 hint = get_hint(obj)41 except TypeError:42 return None43 if hint is NotImplemented or \44 not isinstance(hint, (int, long)) or \44 not isinstance(hint, int_types) or \45 hint < 0:46 return None47 return hint48
499 unmodified lines
modified:tests/test_termui.py+28 added
10 unmodified lines
11 pass1213 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)14 assert label in runner.invoke(cli, []).output151617def test_progressbar_length_hint(runner, monkeypatch):18 class Hinted(object):19 def __init__(self, n):20 self.items = list(range(n))2122 def __length_hint__(self):23 return len(self.items)2425 def __iter__(self):26 return self2728 def __next__(self):29 if self.items:30 return self.items.pop()31 else:32 raise StopIteration3334 @click.command()35 def cli():36 with click.progressbar(Hinted(10), label='test') as progress:37 for thing in progress:38 pass3940 monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)41 result = runner.invoke(cli, [])42 assert result.exception is None