mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-04-28 15:01:34 +08:00
Prevent gateway.platforms.discord from crashing at import time when discord.py is unavailable. Python 3.11 eagerly evaluates annotations, so using discord.Interaction and similar annotations caused an AttributeError after the optional import fallback set discord=None. Add postponed annotation evaluation and a regression test covering import without discord installed.
24 lines
868 B
Python
24 lines
868 B
Python
"""Import-safety tests for the Discord gateway adapter."""
|
|
|
|
import builtins
|
|
import importlib
|
|
import sys
|
|
|
|
|
|
class TestDiscordImportSafety:
|
|
def test_module_imports_even_when_discord_dependency_is_missing(self, monkeypatch):
|
|
original_import = builtins.__import__
|
|
|
|
def fake_import(name, globals=None, locals=None, fromlist=(), level=0):
|
|
if name == "discord" or name.startswith("discord."):
|
|
raise ImportError("discord unavailable for test")
|
|
return original_import(name, globals, locals, fromlist, level)
|
|
|
|
monkeypatch.delitem(sys.modules, "gateway.platforms.discord", raising=False)
|
|
monkeypatch.setattr(builtins, "__import__", fake_import)
|
|
|
|
module = importlib.import_module("gateway.platforms.discord")
|
|
|
|
assert module.DISCORD_AVAILABLE is False
|
|
assert module.discord is None
|