From 1766d4da697e9323873f561f0de6b1ab97edff63 Mon Sep 17 00:00:00 2001 From: Ju4tCode <42488585+yanyongyu@users.noreply.github.com> Date: Mon, 8 Aug 2022 21:07:36 +0800 Subject: [PATCH] :boom: remove deprecated export (#1125) --- nonebot/__init__.py | 2 -- nonebot/plugin/__init__.py | 3 -- nonebot/plugin/export.py | 64 -------------------------------------- nonebot/plugin/load.py | 6 ++-- nonebot/plugin/plugin.py | 3 -- tests/plugins/export.py | 4 --- 6 files changed, 3 insertions(+), 79 deletions(-) delete mode 100644 nonebot/plugin/export.py diff --git a/nonebot/__init__.py b/nonebot/__init__.py index e3301109..f12acba0 100644 --- a/nonebot/__init__.py +++ b/nonebot/__init__.py @@ -29,7 +29,6 @@ - `get_plugin_by_module_name` => {ref}``get_plugin_by_module_name` ` - `get_loaded_plugins` => {ref}``get_loaded_plugins` ` - `get_available_plugin_names` => {ref}``get_available_plugin_names` ` -- `export` => {ref}``export` ` - `require` => {ref}``require` ` FrontMatter: @@ -262,7 +261,6 @@ def run(*args: Any, **kwargs: Any) -> None: from nonebot.plugin import on as on -from nonebot.plugin import export as export from nonebot.plugin import require as require from nonebot.plugin import on_regex as on_regex from nonebot.plugin import on_notice as on_notice diff --git a/nonebot/plugin/__init__.py b/nonebot/plugin/__init__.py index 349961d1..bc44ffb7 100644 --- a/nonebot/plugin/__init__.py +++ b/nonebot/plugin/__init__.py @@ -25,7 +25,6 @@ - `load_from_toml` => {ref}``load_from_toml` ` - `load_builtin_plugin` => {ref}``load_builtin_plugin` ` - `load_builtin_plugins` => {ref}``load_builtin_plugins` ` -- `export` => {ref}``export` ` - `require` => {ref}``require` ` FrontMatter: @@ -106,8 +105,6 @@ def get_available_plugin_names() -> Set[str]: from .on import on as on from .manager import PluginManager -from .export import Export as Export -from .export import export as export from .load import require as require from .on import on_regex as on_regex from .plugin import Plugin as Plugin diff --git a/nonebot/plugin/export.py b/nonebot/plugin/export.py deleted file mode 100644 index a64339f7..00000000 --- a/nonebot/plugin/export.py +++ /dev/null @@ -1,64 +0,0 @@ -"""本模块定义了插件导出的内容对象。 - -在新版插件系统中,推荐优先使用直接 import 所需要的插件内容。 - -FrontMatter: - sidebar_position: 4 - description: nonebot.plugin.export 模块 -""" - -import warnings - -from . import _current_plugin_chain - - -class Export(dict): - """插件导出内容以使得其他插件可以获得。 - - 用法: - ```python - nonebot.export().default = "bar" - - @nonebot.export() - def some_function(): - pass - - # this doesn't work before python 3.9 - # use - # export = nonebot.export(); @export.sub - # instead - # See also PEP-614: https://www.python.org/dev/peps/pep-0614/ - @nonebot.export().sub - def something_else(): - pass - ``` - """ - - def __call__(self, func, **kwargs): - self[func.__name__] = func - self.update(kwargs) - return func - - def __setitem__(self, key, value): - super().__setitem__(key, Export(value) if isinstance(value, dict) else value) - - def __setattr__(self, name, value): - self[name] = Export(value) if isinstance(value, dict) else value - - def __getattr__(self, name): - if name not in self: - self[name] = Export() - return self[name] - - -def export() -> Export: - """获取当前插件的导出内容对象""" - warnings.warn( - "nonebot.export() is deprecated. " - "See https://github.com/nonebot/nonebot2/issues/935.", - DeprecationWarning, - ) - plugins = _current_plugin_chain.get() - if not plugins: - raise RuntimeError("Export outside of the plugin!") - return plugins[-1].export diff --git a/nonebot/plugin/load.py b/nonebot/plugin/load.py index a241804f..537d3a47 100644 --- a/nonebot/plugin/load.py +++ b/nonebot/plugin/load.py @@ -6,11 +6,11 @@ FrontMatter: """ import json import warnings +from types import ModuleType from typing import Set, Iterable, Optional import tomlkit -from .export import Export from .plugin import Plugin from .manager import PluginManager from . import _managers, get_plugin, _module_name_to_plugin_name @@ -143,7 +143,7 @@ def _find_manager_by_name(name: str) -> Optional[PluginManager]: return manager -def require(name: str) -> Export: +def require(name: str) -> ModuleType: """获取一个插件的导出内容。 如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。 @@ -163,4 +163,4 @@ def require(name: str) -> Export: plugin = load_plugin(name) if not plugin: raise RuntimeError(f'Cannot load plugin "{name}"!') - return plugin.export + return plugin.module diff --git a/nonebot/plugin/plugin.py b/nonebot/plugin/plugin.py index a55061fc..ad0bd750 100644 --- a/nonebot/plugin/plugin.py +++ b/nonebot/plugin/plugin.py @@ -12,7 +12,6 @@ from pydantic import BaseModel from nonebot.matcher import Matcher -from .export import Export from . import _plugins as plugins # FIXME: backport for nonebug if TYPE_CHECKING: @@ -46,8 +45,6 @@ class Plugin: """点分割模块路径""" manager: "PluginManager" """导入该插件的插件管理器""" - export: Export = field(default_factory=Export) - """**Deprecated:** 插件内定义的导出内容""" matcher: Set[Type[Matcher]] = field(default_factory=set) """插件内定义的 `Matcher`""" parent_plugin: Optional["Plugin"] = None diff --git a/tests/plugins/export.py b/tests/plugins/export.py index 149b8360..514a0a76 100644 --- a/tests/plugins/export.py +++ b/tests/plugins/export.py @@ -1,6 +1,2 @@ -from nonebot import export - - -@export() def test(): return "export"