Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

BUG: `ctx.create_bot` 未能按预期创建 Bot 对象

Aperta
#57 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
testing-qa

Direzione di ricerca

Inizia con ctx.create_bot in nonebug/mixin/call_api/init.py e confrontalo con il Bot.init dell’adattatore console mostrato in nonebot/adapters/console/bot.py. Riproduci l’esempio di test documentato e traccia gli argomenti passati al fake bot. Il lavoro è completato quando l’esempio può creare un Bot senza il AttributeError segnalato e proseguire con le relative asserzioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

使用文档中测试平台接口使用的例子

from datetime import datetime

import nonebot
import pytest
from nonebot import on_command
from nonebot.adapters.console import Bot, User, Adapter, Message, MessageEvent
from nonebug import App
from pytest_asyncio import is_async_test


def pytest_collection_modifyitems(items: list[pytest.Item]):
    pytest_asyncio_tests = (item for item in items if is_async_test(item))
    session_scope_marker = pytest.mark.asyncio(loop_scope='session')
    for async_test in pytest_asyncio_tests:
        async_test.add_marker(session_scope_marker, append=False)


@pytest.fixture(scope='session', autouse=True)
async def after_nonebot_init(after_nonebot_init: None) -> None:
    from nonebot.adapters.console import Adapter as ConsoleAdapter

    # 加载适配器
    driver = nonebot.get_driver()
    driver.register_adapter(ConsoleAdapter)


def make_event(message: str = "") -> MessageEvent:
    return MessageEvent(
        time=datetime.now(),
        self_id="test",
        message=Message(message),
        user=User(id="user"),
    )


@pytest.mark.asyncio
async def test_example(app: App):
    foo = on_command("foo")

    @foo.handle()
    async def _(bot: Bot):
        await foo.send("message")
        await bot.bell()

    async with app.test_matcher(foo) as ctx:
        adapter = nonebot.get_adapter(Adapter)
        bot = ctx.create_bot(base=Bot, adapter=adapter)
        event = make_event("/foo")
        ctx.receive_event(bot, event)
        ctx.should_call_send(event, "message", result=None, bot=bot)
        ctx.should_call_api("bell", {}, result=None, adapter=adapter)

异常信息如下:

Launching pytest with arguments var/test_foo.py::test_example --no-header --no-summary -q in C:\Users\Administrator\Project\...

============================= test session starts =============================
collecting ... collected 1 item


======================== 1 failed, 1 warning in 2.74s =========================

var/test_foo.py::test_example FAILED                                     [100%]
var\test_foo.py:46 (test_example)
app = <nonebug.app.App object at 0x000001C524EC7A10>

    @pytest.mark.asyncio
    async def test_example(app: App):
        foo = on_command("foo")
    
        @foo.handle()
        async def _(bot: Bot):
            await foo.send("message")
            await bot.bell()
    
        async with app.test_matcher(foo) as ctx:
            adapter = nonebot.get_adapter(Adapter)
>           bot = ctx.create_bot(base=Bot, adapter=adapter)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

var\test_foo.py:57: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
.venv\Lib\site-packages\nonebug\mixin\call_api\__init__.py:100: in create_bot
    bot = make_fake_bot(self, base=base)(adapter, self_id, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <[RecursionError('maximum recursion depth exceeded') raised in repr()] FakeBot object at 0x1c5250e19a0>
adapter = Adapter(name='Console'), info = 'test'

    def __init__(self, adapter: "Adapter", info: Robot):
>       super().__init__(adapter, info.id)
                                  ^^^^^^^
E       AttributeError: 'str' object has no attribute 'id'

.venv\Lib\site-packages\nonebot\adapters\console\bot.py:66: AttributeError
Lingua principale
Python
Stelle
30
Fork
7
Merge medio
2g 17h
PR unite (30g)
1

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nonebot/nonebug

Tutte le issue di nonebot/nonebug

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.