Stop calling super() on handlers that have no base implementation #6

Merged
drawblank merged 1 commit from fix/nickname-in-use into main 2026-08-20 10:35:13 +00:00
Owner

The bot crashed on startup the moment its nick was taken:

File "bot.py", line 83, in on_nicknameinuse
    super().on_nicknameinuse(connection, event)
AttributeError: 'super' object has no attribute 'on_nicknameinuse'

Root cause

The irc library dispatches these hooks with getattr(self, "on_" + event.type, do_nothing). They are optional callbacks, not overrides — neither SingleServerIRCBot nor SimpleIRCClient defines any of them:

on_nicknameinuse     defined by: NOBODY
on_disconnect        defined by: NOBODY
on_welcome           defined by: NOBODY
on_join              defined by: NOBODY

There was nothing to call up to. The library's own bookkeeping lives in separately registered _on_* global handlers, which run regardless of these methods.

on_disconnect had the identical bug and would have crashed on the first disconnect — it simply had not fired yet.

The fix

Both super() calls removed. Since nothing upstream handles nicknameinuse, the retry has to be ours or the bot sits unregistered forever. It now:

  • appends an underscore, trimming first so an already-maximal nick still changes
  • gives up after 5 attempts rather than looping
  • resets the counter on successful registration, and warns when the nick it landed on is not the configured one

Why the tests missed it

No test had ever invoked these handlers. A call to a nonexistent base method is only reachable at runtime, so it could only fail against a live server.

The new tests/test_bot_handlers.py closes that gap two ways: it runs every on_* hook SlopBot defines, and it asserts that none of the four server-lifecycle hooks exist on any base class — so a future super() call fails in CI rather than on connect.

Confirmed by reinstating both bugs:

Bug reinstated Tests failed
The original on_nicknameinuse crash 7
The latent on_disconnect crash 3

385 tests, up from 361.

The bot crashed on startup the moment its nick was taken: ``` File "bot.py", line 83, in on_nicknameinuse super().on_nicknameinuse(connection, event) AttributeError: 'super' object has no attribute 'on_nicknameinuse' ``` ## Root cause The `irc` library dispatches these hooks with `getattr(self, "on_" + event.type, do_nothing)`. They are **optional callbacks, not overrides** — neither `SingleServerIRCBot` nor `SimpleIRCClient` defines any of them: ``` on_nicknameinuse defined by: NOBODY on_disconnect defined by: NOBODY on_welcome defined by: NOBODY on_join defined by: NOBODY ``` There was nothing to call up to. The library's own bookkeeping lives in separately registered `_on_*` global handlers, which run regardless of these methods. **`on_disconnect` had the identical bug** and would have crashed on the first disconnect — it simply had not fired yet. ## The fix Both `super()` calls removed. Since nothing upstream handles `nicknameinuse`, the retry has to be ours or the bot sits unregistered forever. It now: - appends an underscore, **trimming first** so an already-maximal nick still changes - gives up after 5 attempts rather than looping - resets the counter on successful registration, and warns when the nick it landed on is not the configured one ## Why the tests missed it **No test had ever invoked these handlers.** A call to a nonexistent base method is only reachable at runtime, so it could only fail against a live server. The new `tests/test_bot_handlers.py` closes that gap two ways: it runs **every** `on_*` hook `SlopBot` defines, and it asserts that none of the four server-lifecycle hooks exist on any base class — so a future `super()` call fails in CI rather than on connect. Confirmed by reinstating both bugs: | Bug reinstated | Tests failed | |---|---| | The original `on_nicknameinuse` crash | 7 | | The latent `on_disconnect` crash | 3 | 385 tests, up from 361.
The bot crashed on startup the moment its nick was taken:

    File "bot.py", line 83, in on_nicknameinuse
        super().on_nicknameinuse(connection, event)
    AttributeError: 'super' object has no attribute 'on_nicknameinuse'

Root cause: the irc library dispatches these hooks with
getattr(self, "on_" + event.type, do_nothing). They are optional callbacks,
not overrides -- SingleServerIRCBot and SimpleIRCClient define none of
on_welcome, on_join, on_disconnect or on_nicknameinuse. There was nothing to
call up to. The library's own bookkeeping lives in separately registered
_on_* global handlers, which run regardless.

on_disconnect had the identical bug and would have crashed on the first
disconnect; it had simply not fired yet.

Because nothing upstream handles nicknameinuse, the retry has to be ours or
the bot sits unregistered forever. It now appends an underscore, trimming
first so an already-maximal nick still changes, and gives up after 5 attempts
rather than looping. on_welcome resets the counter and warns when the nick it
landed on is not the configured one.

Why the tests missed it: no test had ever invoked these handlers, so a call
to a nonexistent base method could only fail against a live server. The new
tests run every on_* hook SlopBot defines, and assert that none of the four
server-lifecycle hooks exist on any base class. Reinstating the original bug
now fails 7 tests; the latent on_disconnect one fails 3.

Tests: 385, up from 361.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
slopnode/bot!6
No description provided.