From 44e5182322d42cc54f7e7bffe279638007d4d544 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Mon, 20 Dec 2021 00:28:17 +0800 Subject: [PATCH] :bug: fix parent plugin detect error --- nonebot/plugin/manager.py | 6 ++++-- nonebot/plugin/plugin.py | 14 +++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/nonebot/plugin/manager.py b/nonebot/plugin/manager.py index a4b375ab..e029efde 100644 --- a/nonebot/plugin/manager.py +++ b/nonebot/plugin/manager.py @@ -150,9 +150,11 @@ class PluginLoader(SourceFileLoader): if self.loaded: return - plugin = _new_plugin(self.name, module) + plugin = _new_plugin(self.name, module, self.manager) parent_plugin = _current_plugin.get() - if parent_plugin: + if parent_plugin and _managers.index(parent_plugin.manager) < _managers.index( + self.manager + ): plugin.parent_plugin = parent_plugin parent_plugin.sub_plugins.add(plugin) diff --git a/nonebot/plugin/plugin.py b/nonebot/plugin/plugin.py index 49e95dc2..88ebd304 100644 --- a/nonebot/plugin/plugin.py +++ b/nonebot/plugin/plugin.py @@ -1,10 +1,13 @@ from types import ModuleType from dataclasses import field, dataclass -from typing import Set, Dict, Type, Optional +from typing import TYPE_CHECKING, Set, Dict, Type, Optional from .export import Export from nonebot.matcher import Matcher +if TYPE_CHECKING: + from .manager import PluginManager + plugins: Dict[str, "Plugin"] = {} """ :类型: ``Dict[str, Plugin]`` @@ -31,6 +34,11 @@ class Plugin(object): - **类型**: ``str`` - **说明**: 点分割模块路径 """ + manager: "PluginManager" + """ + - **类型**: ``PluginManager`` + - **说明**: 导入该插件的插件管理器 + """ export: Export = field(default_factory=Export) """ - **类型**: ``Export`` @@ -83,10 +91,10 @@ def get_loaded_plugins() -> Set[Plugin]: return set(plugins.values()) -def _new_plugin(fullname: str, module: ModuleType) -> Plugin: +def _new_plugin(fullname: str, module: ModuleType, manager: "PluginManager") -> Plugin: name = fullname.rsplit(".", 1)[-1] if "." in fullname else fullname if name in plugins: raise RuntimeError("Plugin already exists! Check your plugin name.") - plugin = Plugin(name, module, fullname) + plugin = Plugin(name, module, fullname, manager) plugins[name] = plugin return plugin