From 769c7e9aaba4f8df3f0f079a722a31ef0e0d2a12 Mon Sep 17 00:00:00 2001 From: v4rgas <66626747+v4rgas@users.noreply.github.com> Date: Wed, 14 May 2025 17:15:12 -0400 Subject: [PATCH] 00fafb9a-27db-4ba6-92c9-f49644c5c76a --- tqdm_3/tqdm/_tqdm.py | 11 ---------- tqdm_3/tqdm/tests/tests_tqdm.py | 37 ++++++++++++++------------------- 2 files changed, 16 insertions(+), 32 deletions(-) diff --git a/tqdm_3/tqdm/_tqdm.py b/tqdm_3/tqdm/_tqdm.py index 9e54c5f..318249b 100755 --- a/tqdm_3/tqdm/_tqdm.py +++ b/tqdm_3/tqdm/_tqdm.py @@ -947,17 +947,6 @@ def __init__(self, iterable=None, desc=None, total=None, leave=True, # NB: Avoid race conditions by setting start_t at the very end of init self.start_t = self.last_print_t - def __bool__(self): - if self.total is not None: - return self.total > 0 - if self.iterable is None: - raise TypeError('Boolean cast is undefined' - ' for tqdm objects that have no iterable or total') - return bool(self.iterable) - - def __nonzero__(self): - return self.__bool__() - def __len__(self): return self.total if self.iterable is None else \ (self.iterable.shape[0] if hasattr(self.iterable, "shape") diff --git a/tqdm_3/tqdm/tests/tests_tqdm.py b/tqdm_3/tqdm/tests/tests_tqdm.py index acad9f4..44a9aea 100644 --- a/tqdm_3/tqdm/tests/tests_tqdm.py +++ b/tqdm_3/tqdm/tests/tests_tqdm.py @@ -1710,32 +1710,27 @@ def test_threading(): @with_setup(pretest, posttest) def test_bool(): """Test boolean cast""" - def internal(our_file, disable): + with tqdm(total=10, file=our_file, disable=disable) as t: + assert t + with tqdm(total=0, file=our_file, disable=disable) as t: + assert not t with trange(10, file=our_file, disable=disable) as t: assert t with trange(0, file=our_file, disable=disable) as t: assert not t - - def get_bool_for_tqdm(*args, **kwargs): - kwargs['file'] = our_file - kwargs['disable'] = disable - with tqdm(*args, **kwargs) as t: - return bool(t) - - assert get_bool_for_tqdm(total=10) - assert not get_bool_for_tqdm(total=0) - assert not get_bool_for_tqdm([]) - assert get_bool_for_tqdm([0]) - assert get_bool_for_tqdm((x for x in [])) - assert get_bool_for_tqdm((x for x in [1,2,3])) - try: - get_bool_for_tqdm() - except TypeError: - pass - else: - raise TypeError( - "Expected tqdm() with neither total nor iterable to fail") + with tqdm([], file=our_file, disable=disable) as t: + assert not t + with tqdm([0], file=our_file, disable=disable) as t: + assert t + with tqdm(file=our_file, disable=disable) as t: + try: + print(bool(t)) + except TypeError: + pass + else: + raise TypeError( + "Expected tqdm() with neither total nor iterable to fail") # test with and without disable with closing(StringIO()) as our_file: