Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tqdm_6/tqdm/_tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ def __len__(self):
return self.total if self.iterable is None else \
(self.iterable.shape[0] if hasattr(self.iterable, "shape")
else len(self.iterable) if hasattr(self.iterable, "__len__")
else getattr(self, "total", None))
else self.total)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 Warning 🐛 Bug

Calling __len__ on disabled tqdm instances raises AttributeError.

Issue Explanation
  • The __len__ method returns self.total when self.iterable is None or unsized.
  • In the early disable branch of __init__, the constructor returns before assigning self.total.
  • Disabled instances never get a total attribute, so calling __len__ on them raises AttributeError.
  • Non-disabled instances always set self.total (even if it remains None).

Reply if you have any questions or let me know if I missed something.
Don't forget to react with a 👍 or 👎 to the comments made by Blar to help us improve.


def __enter__(self):
return self
Expand Down
23 changes: 3 additions & 20 deletions tqdm_6/tqdm/tests/tests_synchronisation.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from __future__ import division
from tqdm import tqdm, TMonitor
from tests_tqdm import with_setup, pretest, posttest, SkipTest, \
StringIO, closing
from tqdm import tqdm
from tests_tqdm import with_setup, pretest, posttest, StringIO, closing
from tests_tqdm import DiscreteTimer, cpu_timify

from time import sleep
from threading import Event
from tqdm import TMonitor


class FakeSleep(object):
Expand Down Expand Up @@ -37,10 +37,6 @@ def create_fake_sleep_event():
return create_fake_sleep_event


def incr(x):
return x + 1


@with_setup(pretest, posttest)
def test_monitor_thread():
"""Test dummy monitoring thread"""
Expand Down Expand Up @@ -166,16 +162,3 @@ def test_monitoring_multi():
# Check that class var monitor is deleted if no instance left
tqdm.monitor_interval = 10
assert tqdm.monitor is None


@with_setup(pretest, posttest)
def test_imap():
"""Test multiprocessing.Pool"""
try:
from multiprocessing import Pool
except ImportError:
raise SkipTest

pool = Pool()
res = list(tqdm(pool.imap(incr, range(100)), disable=True))
assert res[-1] == 100