🐛 Bug: inherit_supported_adapters 在展开缩写前取交集 (#2654)

Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Azide 2024-04-18 18:50:11 +08:00 committed by GitHub
parent b497bb8c83
commit f5f5d93b64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 96 additions and 32 deletions

View File

@ -206,18 +206,17 @@ def inherit_supported_adapters(*names: str) -> Optional[set[str]]:
meta = plugin.metadata meta = plugin.metadata
if meta is None: if meta is None:
raise ValueError(f'Plugin "{name}" has no metadata!') raise ValueError(f'Plugin "{name}" has no metadata!')
support = meta.supported_adapters
if support is None: if (raw := meta.supported_adapters) is None:
continue continue
support = {
f"nonebot.adapters.{adapter[1:]}" if adapter.startswith("~") else adapter
for adapter in raw
}
final_supported = ( final_supported = (
support if final_supported is None else (final_supported & support) support if final_supported is None else (final_supported & support)
) )
return final_supported and { return final_supported
(
f"nonebot.adapters.{adapter_name[1:]}"
if adapter_name.startswith("~")
else adapter_name
)
for adapter_name in final_supported
}

View File

@ -0,0 +1,15 @@
from nonebot.plugin import PluginMetadata
__plugin_meta__ = PluginMetadata(
name="测试插件3",
description="测试继承适配器, 使用内置适配器全名",
usage="无法使用",
type="application",
homepage="https://nonebot.dev",
supported_adapters={
"nonebot.adapters.onebot.v11",
"nonebot.adapters.onebot.v12",
"~qq",
},
extra={"author": "NoneBot"},
)

View File

@ -149,32 +149,82 @@ async def test_plugin_metadata():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_inherit_supported_adapters(): async def test_inherit_supported_adapters_not_found():
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
inherit_supported_adapters("some_plugin_not_exist") inherit_supported_adapters("some_plugin_not_exist")
with pytest.raises(ValueError, match="has no metadata!"): with pytest.raises(ValueError, match="has no metadata!"):
inherit_supported_adapters("export") inherit_supported_adapters("export")
echo = nonebot.get_plugin("echo")
assert echo
assert echo.metadata
assert inherit_supported_adapters("echo") is None
plugin_1 = nonebot.get_plugin("metadata") @pytest.mark.asyncio
assert plugin_1 @pytest.mark.parametrize(
assert plugin_1.metadata ("inherit_plugins", "expected"),
assert inherit_supported_adapters("metadata") == { [
(("echo",), None),
(
("metadata",),
{
"nonebot.adapters.onebot.v11", "nonebot.adapters.onebot.v11",
"plugins.metadata:FakeAdapter", "plugins.metadata:FakeAdapter",
} },
),
plugin_2 = nonebot.get_plugin("metadata_2") (
assert plugin_2 ("metadata_2",),
assert plugin_2.metadata {
assert inherit_supported_adapters("metadata", "metadata_2") == { "nonebot.adapters.onebot.v11",
"nonebot.adapters.onebot.v11" "nonebot.adapters.onebot.v12",
} },
assert inherit_supported_adapters("metadata", "echo", "metadata_2") == { ),
"nonebot.adapters.onebot.v11" (
} ("metadata_3",),
{
"nonebot.adapters.onebot.v11",
"nonebot.adapters.onebot.v12",
"nonebot.adapters.qq",
},
),
(
("metadata", "metadata_2"),
{
"nonebot.adapters.onebot.v11",
},
),
(
("metadata", "metadata_3"),
{
"nonebot.adapters.onebot.v11",
},
),
(
("metadata_2", "metadata_3"),
{
"nonebot.adapters.onebot.v11",
"nonebot.adapters.onebot.v12",
},
),
(
("metadata", "metadata_2", "metadata_3"),
{
"nonebot.adapters.onebot.v11",
},
),
(
("metadata", "echo"),
{
"nonebot.adapters.onebot.v11",
"plugins.metadata:FakeAdapter",
},
),
(
("metadata", "metadata_2", "echo"),
{
"nonebot.adapters.onebot.v11",
},
),
],
)
async def test_inherit_supported_adapters_combine(
inherit_plugins: tuple[str], expected: set[str]
):
assert inherit_supported_adapters(*inherit_plugins) == expected