From 898c29d7eed95a3b5bd355a427d696fa4e0aa0fd Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Tue, 16 Aug 2022 10:03:37 +0800 Subject: [PATCH] :boom: remove deprecated `nonebot.plugins` toml table (#1151) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Feature: 移除过时的 `nonebot.plugins` toml 配置 --- nonebot/plugin/load.py | 15 ++++++--------- tests/plugins.empty.toml | 0 tests/plugins.invalid.json | 1 + tests/plugins.invalid.toml | 2 ++ tests/plugins.json | 4 ++++ tests/plugins.toml | 3 +++ tests/plugins/matcher/matcher_expire.py | 4 +++- tests/test_plugin/test_load.py | 23 +++++++++++++++++++++++ 8 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 tests/plugins.empty.toml create mode 100644 tests/plugins.invalid.json create mode 100644 tests/plugins.invalid.toml create mode 100644 tests/plugins.json create mode 100644 tests/plugins.toml diff --git a/nonebot/plugin/load.py b/nonebot/plugin/load.py index 537d3a47..1a6075b3 100644 --- a/nonebot/plugin/load.py +++ b/nonebot/plugin/load.py @@ -74,6 +74,8 @@ def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]: """ with open(file_path, "r", encoding=encoding) as f: data = json.load(f) + if not isinstance(data, dict): + raise TypeError("json file must contains a dict!") plugins = data.get("plugins") plugin_dirs = data.get("plugin_dirs") assert isinstance(plugins, list), "plugins must be a list of plugin name" @@ -103,15 +105,10 @@ def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]: data = tomlkit.parse(f.read()) # type: ignore nonebot_data = data.get("tool", {}).get("nonebot") - if not nonebot_data: - nonebot_data = data.get("nonebot", {}).get("plugins") - if nonebot_data: - warnings.warn( - "[nonebot.plugins] table is deprecated. Use [tool.nonebot] instead.", - DeprecationWarning, - ) - else: - raise ValueError("Cannot find '[tool.nonebot]' in given toml file!") + if nonebot_data is None: + raise ValueError("Cannot find '[tool.nonebot]' in given toml file!") + if not isinstance(nonebot_data, dict): + raise TypeError("'[tool.nonebot]' must be a Table!") plugins = nonebot_data.get("plugins", []) plugin_dirs = nonebot_data.get("plugin_dirs", []) assert isinstance(plugins, list), "plugins must be a list of plugin name" diff --git a/tests/plugins.empty.toml b/tests/plugins.empty.toml new file mode 100644 index 00000000..e69de29b diff --git a/tests/plugins.invalid.json b/tests/plugins.invalid.json new file mode 100644 index 00000000..fe51488c --- /dev/null +++ b/tests/plugins.invalid.json @@ -0,0 +1 @@ +[] diff --git a/tests/plugins.invalid.toml b/tests/plugins.invalid.toml new file mode 100644 index 00000000..8b44f8cd --- /dev/null +++ b/tests/plugins.invalid.toml @@ -0,0 +1,2 @@ +[tool] +nonebot = [] diff --git a/tests/plugins.json b/tests/plugins.json new file mode 100644 index 00000000..eb91dd97 --- /dev/null +++ b/tests/plugins.json @@ -0,0 +1,4 @@ +{ + "plugins": [], + "plugin_dirs": ["plugins"] +} diff --git a/tests/plugins.toml b/tests/plugins.toml new file mode 100644 index 00000000..2ddf954e --- /dev/null +++ b/tests/plugins.toml @@ -0,0 +1,3 @@ +[tool.nonebot] +plugins = [] +plugin_dirs = ["plugins"] diff --git a/tests/plugins/matcher/matcher_expire.py b/tests/plugins/matcher/matcher_expire.py index c026d679..f8aaebcc 100644 --- a/tests/plugins/matcher/matcher_expire.py +++ b/tests/plugins/matcher/matcher_expire.py @@ -3,5 +3,7 @@ from datetime import datetime, timedelta from nonebot.matcher import Matcher test_temp_matcher = Matcher.new("test", temp=True) -test_datetime_matcher = Matcher.new("test", expire_time=datetime.now()) +test_datetime_matcher = Matcher.new( + "test", expire_time=datetime.now() - timedelta(seconds=1) +) test_timedelta_matcher = Matcher.new("test", expire_time=timedelta(seconds=-1)) diff --git a/tests/test_plugin/test_load.py b/tests/test_plugin/test_load.py index 53b1d1df..ee4ce66b 100644 --- a/tests/test_plugin/test_load.py +++ b/tests/test_plugin/test_load.py @@ -51,6 +51,29 @@ async def test_load_nested_plugin(app: App, load_plugin: Set["Plugin"]): assert parent_plugin.sub_plugins == {sub_plugin, sub_plugin2} +@pytest.mark.asyncio +async def test_load_json(app: App): + import nonebot + + nonebot.load_from_json("./plugins.json") + + with pytest.raises(TypeError): + nonebot.load_from_json("./plugins.invalid.json") + + +@pytest.mark.asyncio +async def test_load_toml(app: App): + import nonebot + + nonebot.load_from_toml("./plugins.toml") + + with pytest.raises(ValueError): + nonebot.load_from_toml("./plugins.empty.toml") + + with pytest.raises(TypeError): + nonebot.load_from_toml("./plugins.invalid.toml") + + @pytest.mark.asyncio async def test_bad_plugin(app: App): import nonebot