#1812·tqdm

`tqdm.contrib.discord` and `telegram` mask connection errors with `UnboundLocalError`

Author: yayong3Created Sep 3, 2026Updated Sep 3, 2026
  • I have marked all applicable categories:
    • exception-raising bug
    • visual output bug
  • 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:
    python
    import tqdm, sys
    print(tqdm.__version__, sys.version, sys.platform)

Summary

If post() raises before returning, req has not been assigned. The exception handler then raises UnboundLocalError, hiding the original requests exception.

This affects both implementations:

  • tqdm/contrib/discord.py, in DiscordIO.message_id
  • tqdm/contrib/telegram.py, in TelegramIO.message_id

Reproduction

The following points the Discord client at a closed local port. Disabling environment proxy settings ensures that the request reaches that address directly.

python
from requests import Session
from tqdm.contrib import discord as discord_module
from tqdm.contrib.discord import DiscordIO

class DirectSession(Session):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.trust_env = False

discord_module.Session = DirectSession
DiscordIO.API = "http://127.0.0.1:1"
DiscordIO("TOKEN", "CHANNEL")

Output:

Traceback (most recent call last):
  File "/home/test.py", line 14, in <module>
    DiscordIO("TOKEN", "CHANNEL")
  File "/home/tqdm/contrib/discord.py", line 38, in __init__
    self.message_id  # pylint: disable=pointless-statement
    ^^^^^^^^^^^^^^^
  File "/home/tqdm/tqdm/contrib/discord.py", line 52, in message_id
    if req.status_code == 429:
       ^^^
UnboundLocalError: cannot access local variable 'req' where it is not associated with a value

The original network error was masked by another internal error.

Environment

Python 3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tqdm, sys
>>> print(tqdm.__version__, sys.version, sys.platform)
4.70.0 3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0] linux