Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 适配 Pydantic V2 #88

Merged
merged 5 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ updates:
schedule:
interval: "daily"
groups:
actions-update:
actions:
patterns:
- "*"
8 changes: 4 additions & 4 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
os: [ubuntu-latest, windows-latest, macos-latest]
exclude:
- python-version: "3.11"
os: windows-latest
# exclude:
# - python-version: "3.11"
# os: windows-latest
fail-fast: false
env:
OS: ${{ matrix.os }}
Expand Down
17 changes: 3 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,15 @@ ci:
autoupdate_commit_msg: "chore: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.1
rev: v0.2.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
stages: [commit]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
stages: [commit]

- repo: https://github.com/psf/black
rev: 23.10.0
hooks:
- id: black
stages: [commit]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or: [javascript, jsx, ts, tsx, markdown, yaml, json]
Expand Down
8 changes: 4 additions & 4 deletions nonebot_plugin_treehelp/config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
""" 配置 """
from typing import List

from nonebot import get_driver
from pydantic import BaseModel, Extra
from nonebot import get_plugin_config
from pydantic import BaseModel


class Config(BaseModel, extra=Extra.ignore):
class Config(BaseModel):
treehelp_ignored_plugins: List[str] = []
"""需要忽略的插件"""


plugin_config = Config.parse_obj(get_driver().config)
plugin_config = get_plugin_config(Config)
20 changes: 7 additions & 13 deletions nonebot_plugin_treehelp/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ def map_command_to_plguin(plugin: "Plugin"):
for matcher in matchers:
checkers = matcher.rule.checkers
command_handler = next(
filter(
lambda x: isinstance(x.call, (CommandRule, ShellCommandRule)), checkers
),
filter(lambda x: isinstance(x.call, (CommandRule, ShellCommandRule)), checkers),
None,
)
if not command_handler:
Expand All @@ -41,9 +39,7 @@ def map_command_to_plguin(plugin: "Plugin"):

def format_description(plugins: List["Plugin"]) -> str:
"""格式化描述"""
return "\n".join(
sorted(f"{x.metadata.name} # {x.metadata.description}" for x in plugins)
)
return "\n".join(sorted(f"{x.metadata.name} # {x.metadata.description}" for x in plugins))


def is_supported_adapter(bot: "Bot", metadata: "PluginMetadata") -> bool:
Expand Down Expand Up @@ -112,9 +108,7 @@ def get_plugin_list(bot: "Bot", tree: bool = False) -> str:
"""获取插件列表"""
# 仅保留根插件
plugins = [
plugin
for plugin in get_plugins().values()
if plugin.parent_plugin is None and is_supported(bot, plugin)
plugin for plugin in get_plugins().values() if plugin.parent_plugin is None and is_supported(bot, plugin)
]
sorted_plugins = sorted(plugins, key=lambda x: x.metadata.name) # type: ignore

Expand Down Expand Up @@ -156,9 +150,7 @@ def get_plugin_help(bot: "Bot", name: str, tree: bool = False) -> Optional[str]:

sub_plugins = [plugin for plugin in plugin.sub_plugins if is_supported(bot, plugin)]
sub_plugins_desc = format_description(sub_plugins)
return "\n\n".join(
[x for x in [metadata.name, metadata.usage, sub_plugins_desc] if x]
)
return "\n\n".join([x for x in [metadata.name, metadata.usage, sub_plugins_desc] if x])


def get_tree_string(
Expand All @@ -178,6 +170,8 @@ def get_tree_string(
for i, plugin in enumerate(sorted_plugins, 1):
if i == total:
tree_bar = previous_tree_bar + "└"
docs.append(f"{tree_bar}── {plugin.metadata.name} # {plugin.metadata.description}") # type: ignore
docs.append(
f"{tree_bar}── {plugin.metadata.name} # {plugin.metadata.description}" # type: ignore
)
tree_bar = tree_bar.replace("└", " ")
get_tree_string(bot, docs, plugin.sub_plugins, tree_bar + " ")
1,586 changes: 583 additions & 1,003 deletions poetry.lock

Large diffs are not rendered by default.

23 changes: 9 additions & 14 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ documentation = "https://github.com/he0119/nonebot-plugin-treehelp#readme"

[tool.poetry.dependencies]
python = "^3.8"
nonebot2 = "^2.0.0"
nonebot2 = "^2.2.0"

[tool.poetry.group.dev.dependencies]
nb-cli = "^1.0.4"
nonebot-adapter-console = "^0.4.0"
nonebot-adapter-onebot = "^2.2.3"
nonebot-adapter-console = { git = "https://github.com/he0119/adapter-console.git", rev = "feat/pydantic-v2" }
nonebot-adapter-onebot = { git = "https://github.com/nonebot/adapter-onebot.git", rev = "feat/pydantic-v2" }

[tool.poetry.group.test.dependencies]
nonebug = "^0.3.1"
Expand All @@ -25,14 +24,6 @@ pytest-xdist = "^3.0.2"
pytest-mock = "^3.10.0"
pytest-asyncio = ">=0.20.2,<0.22.0"

[tool.black]
line-length = 88

[tool.isort]
profile = "black"
line_length = 88
skip_gitignore = true

[tool.pytest.ini_options]
asyncio_mode = "auto"

Expand All @@ -42,8 +33,12 @@ pythonPlatform = "All"
typeCheckingMode = "basic"

[tool.ruff]
select = ["E", "W", "F", "UP", "C", "T", "PYI", "Q"]
ignore = ["E402", "E501", "C901", "UP037"]
line-length = 120
target-version = "py38"

[tool.ruff.lint]
select = ["E", "W", "F", "UP", "C", "T", "PYI", "PT", "Q"]
ignore = ["E402", "C901", "UP037"]

[tool.nonebot]
adapters = [
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def pytest_configure(config: pytest.Config) -> None:


@pytest.fixture(autouse=True)
def register_adapters(nonebug_init: None):
def _register_adapters(nonebug_init: None):
from nonebot import get_driver
from nonebot.adapters.console import Adapter as ConsoleAdapter
from nonebot.adapters.onebot.v11 import Adapter as OnebotV11Adapter
Expand All @@ -22,7 +22,7 @@ def register_adapters(nonebug_init: None):
driver.register_adapter(OnebotV12Adapter)


@pytest.fixture
@pytest.fixture()
def app(nonebug_init: None):
clear_plugins()
# 加载插件
Expand Down
10 changes: 7 additions & 3 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ async def test_sub_plugins(app: App):
event = make_fake_event(_message=message)()

ctx.receive_event(bot, event)
ctx.should_call_send(event, "测试\n\n复杂功能 # 测试插件复杂子插件\n简单功能 # 测试插件简单子插件", True)
ctx.should_call_send(
event,
"测试\n\n复杂功能 # 测试插件复杂子插件\n简单功能 # 测试插件简单子插件",
True,
)
ctx.should_finished()


Expand All @@ -52,7 +56,7 @@ async def test_tree_view(app: App):
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
"插件:\n帮助 # 获取插件帮助信息\n测试 # 一个测试插件\n├── 复杂功能 # 测试插件复杂子插件\n│ └── 二级功能 # 测试插件二级插件\n└── 简单功能 # 测试插件简单子插件",
"插件:\n帮助 # 获取插件帮助信息\n测试 # 一个测试插件\n├── 复杂功能 # 测试插件复杂子插件\n│ └── 二级功能 # 测试插件二级插件\n└── 简单功能 # 测试插件简单子插件", # noqa: E501
True,
)
ctx.should_finished()
Expand All @@ -65,7 +69,7 @@ async def test_tree_view(app: App):
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
"测试 # 一个测试插件\n├── 复杂功能 # 测试插件复杂子插件\n│ └── 二级功能 # 测试插件二级插件\n└── 简单功能 # 测试插件简单子插件",
"测试 # 一个测试插件\n├── 复杂功能 # 测试插件复杂子插件\n│ └── 二级功能 # 测试插件二级插件\n└── 简单功能 # 测试插件简单子插件", # noqa: E501
True,
)
ctx.should_finished()
Loading