Skip to content

tqdm.write 'int' object has no attribute 'decode' #699

@thomasaarholt

Description

@thomasaarholt
  • I have visited the source website, and in particular
    read the known issues
  • I have searched through the issue tracker for duplicates
  • I have mentioned version numbers, operating system and
    environment, where applicable:
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)
    4.31.1 3.7.1 | packaged by conda-forge | (default, Mar 13 2019, 12:57:14) 
    [GCC 7.3.0] linux

I think the following behaviour can at least be solved by improving the tqdm.write docs:

Whentqdm.write gets non-unicode input (I've tested with writing a list as well, same error), it throws the following error:

>>> from tqdm import tqdm
>>> tqdm.write(2)

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-14-8f12b8905b28> in <module>
      1 from tqdm import tqdm
----> 2 tqdm.write(2)

/shared/users/thomasaar/py37/lib/python3.7/site-packages/tqdm/_tqdm.py in write(cls, s, file, end, nolock)
    524         with cls.external_write_mode(file=file, nolock=nolock):
    525             # Write the message
--> 526             fp.write(s)
    527             fp.write(end)
    528 

/shared/users/thomasaar/py37/lib/python3.7/site-packages/ipykernel/iostream.py in write(self, string)
    394             # Make sure that we're handling unicode
    395             if not isinstance(string, unicode_type):
--> 396                 string = string.decode(self.encoding, 'replace')
    397 
    398             is_child = (not self._is_master_process())

AttributeError: 'int' object has no attribute 'decode'

I have viewed tqdm.write as a tqdm replacement for print that doesn't break progress bar flow, and print can handle printing non-string types. As hinted, the docs aren't extensive, and suggest that the behaviour should imitate print.

My immediate suggestion it wrap whatever argument is fed to tqdm.write in a str(), since tqdm.write(str(non_string_object)) works fine. Alternatively, one could try the decode in the error message, and if that fails use the string representation instead. I'm not familiar with how python 2.6+ would handle this, however, and I'd gladly take other suggestions.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions