🚨 Develop: 添加 ruff linter (#2114)

This commit is contained in:
Ju4tCode
2023-06-24 14:47:35 +08:00
committed by GitHub
parent fe21cbfa1d
commit 3d5dd5969c
53 changed files with 813 additions and 758 deletions

View File

@ -1 +1,3 @@
assert False
import pytest
pytest.fail("should not be imported")

View File

@ -1,6 +1,5 @@
from pathlib import Path
import nonebot
from nonebot.plugin import PluginManager, _managers
manager = PluginManager(

View File

@ -1 +1 @@
from .nested_subplugin2 import a # nopycln: import
from .nested_subplugin2 import a # noqa: F401

View File

@ -1,4 +1,4 @@
from nonebot.adapters import Event, Message
from nonebot.adapters import Message
from nonebot.params import Arg, ArgStr, ArgPlainText

View File

@ -1 +1 @@
from . import matchers
from . import matchers as matchers

View File

@ -4,4 +4,5 @@ test_require = require("export").test
from plugins.export import test
assert test is test_require and test() == "export", "Export Require Error"
assert test is test_require, "Export Require Error"
assert test() == "export", "Export Require Error"

View File

@ -176,7 +176,8 @@ async def test_http_client(driver: Driver, server_url: URL):
content="test",
)
response = await driver.request(request)
assert response.status_code == 200 and response.content
assert response.status_code == 200
assert response.content
data = json.loads(response.content)
assert data["method"] == "POST"
assert data["args"] == {"param": "test"}
@ -187,7 +188,8 @@ async def test_http_client(driver: Driver, server_url: URL):
# post with data body
request = Request("POST", server_url, data={"form": "test"})
response = await driver.request(request)
assert response.status_code == 200 and response.content
assert response.status_code == 200
assert response.content
data = json.loads(response.content)
assert data["method"] == "POST"
assert data["form"] == {"form": "test"}
@ -195,7 +197,8 @@ async def test_http_client(driver: Driver, server_url: URL):
# post with json body
request = Request("POST", server_url, json={"json": "test"})
response = await driver.request(request)
assert response.status_code == 200 and response.content
assert response.status_code == 200
assert response.content
data = json.loads(response.content)
assert data["method"] == "POST"
assert data["json"] == {"json": "test"}
@ -208,7 +211,8 @@ async def test_http_client(driver: Driver, server_url: URL):
files={"test": ("test.txt", b"test")},
)
response = await driver.request(request)
assert response.status_code == 200 and response.content
assert response.status_code == 200
assert response.content
data = json.loads(response.content)
assert data["method"] == "POST"
assert data["form"] == {"form": "test"}
@ -219,7 +223,7 @@ async def test_http_client(driver: Driver, server_url: URL):
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver, driver_type",
("driver", "driver_type"),
[
pytest.param(
"nonebot.drivers.fastapi:Driver+nonebot.drivers.aiohttp:Mixin",

View File

@ -34,7 +34,7 @@ async def test_init():
async def test_get_driver(app: App, monkeypatch: pytest.MonkeyPatch):
with monkeypatch.context() as m:
m.setattr(nonebot, "_driver", None)
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="initialized"):
get_driver()
@ -63,7 +63,7 @@ async def test_get_adapter(app: App, monkeypatch: pytest.MonkeyPatch):
assert get_adapters() == {adapter_name: adapter}
assert get_adapter(adapter_name) is adapter
assert get_adapter(adapter.__class__) is adapter
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="registered"):
get_adapter("not exist")
@ -74,7 +74,8 @@ async def test_run(app: App, monkeypatch: pytest.MonkeyPatch):
def mock_run(*args, **kwargs):
nonlocal runned
runned = True
assert args == ("arg",) and kwargs == {"kwarg": "kwarg"}
assert args == ("arg",)
assert kwargs == {"kwarg": "kwarg"}
driver = get_driver()
@ -89,7 +90,7 @@ async def test_run(app: App, monkeypatch: pytest.MonkeyPatch):
async def test_get_bot(app: App, monkeypatch: pytest.MonkeyPatch):
driver = get_driver()
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="no bots"):
get_bot()
with monkeypatch.context() as m:

View File

@ -120,7 +120,7 @@ async def test_matcher_overload(app: App):
async def test_matcher_destroy(app: App):
from plugins.matcher.matcher_process import test_destroy
async with app.test_matcher(test_destroy) as ctx:
async with app.test_matcher(test_destroy):
assert len(matchers) == 1
assert len(matchers[test_destroy.priority]) == 1
assert matchers[test_destroy.priority][0] is test_destroy

View File

@ -33,6 +33,8 @@ from nonebot.consts import (
CMD_WHITESPACE_KEY,
)
UNKNOWN_PARAM = "Unknown parameter"
@pytest.mark.asyncio
async def test_depend(app: App):
@ -50,7 +52,8 @@ async def test_depend(app: App):
async with app.test_dependent(depends, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
assert len(runned) == 1 and runned[0] == 1
assert len(runned) == 1
assert runned[0] == 1
runned.clear()
@ -59,7 +62,8 @@ async def test_depend(app: App):
event_next = make_fake_event()()
ctx.receive_event(bot, event_next)
assert len(runned) == 2 and runned[0] == runned[1] == 1
assert len(runned) == 2
assert runned[0] == runned[1] == 1
runned.clear()
@ -105,16 +109,15 @@ async def test_bot(app: App):
ctx.pass_params(bot=bot)
ctx.should_return(bot)
with pytest.raises(ValueError):
async with app.test_dependent(not_legacy_bot, allow_types=[BotParam]) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_legacy_bot, allow_types=[BotParam])
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot(base=FooBot)
ctx.pass_params(bot=bot)
ctx.should_return(bot)
with pytest.raises(TypeMisMatch):
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
@ -134,9 +137,8 @@ async def test_bot(app: App):
ctx.pass_params(bot=bot)
ctx.should_return(bot)
with pytest.raises(ValueError):
async with app.test_dependent(not_bot, allow_types=[BotParam]) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_bot, allow_types=[BotParam])
@pytest.mark.asyncio
@ -169,17 +171,14 @@ async def test_event(app: App):
ctx.pass_params(event=fake_event)
ctx.should_return(fake_event)
with pytest.raises(ValueError):
async with app.test_dependent(
not_legacy_event, allow_types=[EventParam]
) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_legacy_event, allow_types=[EventParam])
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_fooevent)
ctx.should_return(fake_fooevent)
with pytest.raises(TypeMisMatch):
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_event)
@ -195,9 +194,8 @@ async def test_event(app: App):
ctx.pass_params(event=fake_event)
ctx.should_return(fake_event)
with pytest.raises(ValueError):
async with app.test_dependent(not_event, allow_types=[EventParam]) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_event, allow_types=[EventParam])
async with app.test_dependent(
event_type, allow_types=[EventParam, DependParam]
@ -274,11 +272,8 @@ async def test_state(app: App):
ctx.pass_params(state=fake_state)
ctx.should_return(fake_state)
with pytest.raises(ValueError):
async with app.test_dependent(
not_legacy_state, allow_types=[StateParam]
) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_legacy_state, allow_types=[StateParam])
async with app.test_dependent(
command, allow_types=[StateParam, DependParam]
@ -398,17 +393,14 @@ async def test_matcher(app: App):
ctx.pass_params(matcher=fake_matcher)
ctx.should_return(fake_matcher)
with pytest.raises(ValueError):
async with app.test_dependent(
not_legacy_matcher, allow_types=[MatcherParam]
) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_legacy_matcher, allow_types=[MatcherParam])
async with app.test_dependent(sub_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=foo_matcher)
ctx.should_return(foo_matcher)
with pytest.raises(TypeMisMatch):
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(sub_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=fake_matcher)
@ -426,9 +418,8 @@ async def test_matcher(app: App):
ctx.pass_params(matcher=fake_matcher)
ctx.should_return(fake_matcher)
with pytest.raises(ValueError):
async with app.test_dependent(not_matcher, allow_types=[MatcherParam]) as ctx:
...
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_matcher, allow_types=[MatcherParam])
event = make_fake_event()()
fake_matcher.set_receive("test", event)

View File

@ -47,15 +47,15 @@ async def test_permission(app: App):
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await Permission(falsy)(bot, event) == False
assert await Permission(truthy)(bot, event) == True
assert await Permission(skipped)(bot, event) == False
assert await Permission(truthy, falsy)(bot, event) == True
assert await Permission(truthy, skipped)(bot, event) == True
assert await Permission(falsy)(bot, event) is False
assert await Permission(truthy)(bot, event) is True
assert await Permission(skipped)(bot, event) is False
assert await Permission(truthy, falsy)(bot, event) is True
assert await Permission(truthy, skipped)(bot, event) is True
@pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", True), ("notice", False)])
@pytest.mark.parametrize(("type", "expected"), [("message", True), ("notice", False)])
async def test_message(type: str, expected: bool):
dependent = list(MESSAGE.checkers)[0]
checker = dependent.call
@ -67,7 +67,7 @@ async def test_message(type: str, expected: bool):
@pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", False), ("notice", True)])
@pytest.mark.parametrize(("type", "expected"), [("message", False), ("notice", True)])
async def test_notice(type: str, expected: bool):
dependent = list(NOTICE.checkers)[0]
checker = dependent.call
@ -79,7 +79,7 @@ async def test_notice(type: str, expected: bool):
@pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", False), ("request", True)])
@pytest.mark.parametrize(("type", "expected"), [("message", False), ("request", True)])
async def test_request(type: str, expected: bool):
dependent = list(REQUEST.checkers)[0]
checker = dependent.call
@ -91,7 +91,9 @@ async def test_request(type: str, expected: bool):
@pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", False), ("meta_event", True)])
@pytest.mark.parametrize(
("type", "expected"), [("message", False), ("meta_event", True)]
)
async def test_metaevent(type: str, expected: bool):
dependent = list(METAEVENT.checkers)[0]
checker = dependent.call
@ -104,7 +106,7 @@ async def test_metaevent(type: str, expected: bool):
@pytest.mark.asyncio
@pytest.mark.parametrize(
"type, user_id, expected",
("type", "user_id", "expected"),
[
("message", "test", True),
("message", "foo", False),
@ -128,7 +130,7 @@ async def test_superuser(app: App, type: str, user_id: str, expected: bool):
@pytest.mark.asyncio
@pytest.mark.parametrize(
"session_ids, session_id, expected",
("session_ids", "session_id", "expected"),
[
(("user", "foo"), "user", True),
(("user", "foo"), "bar", False),

View File

@ -49,7 +49,9 @@ async def test_load_nested_plugin():
parent_plugin = nonebot.get_plugin("nested")
sub_plugin = nonebot.get_plugin("nested_subplugin")
sub_plugin2 = nonebot.get_plugin("nested_subplugin2")
assert parent_plugin and sub_plugin and sub_plugin2
assert parent_plugin
assert sub_plugin
assert sub_plugin2
assert sub_plugin.parent_plugin is parent_plugin
assert sub_plugin2.parent_plugin is parent_plugin
assert parent_plugin.sub_plugins == {sub_plugin, sub_plugin2}
@ -67,7 +69,7 @@ async def test_load_json():
async def test_load_toml():
nonebot.load_from_toml("./plugins.toml")
with pytest.raises(ValueError):
with pytest.raises(ValueError, match="Cannot find"):
nonebot.load_from_toml("./plugins.empty.toml")
with pytest.raises(TypeError):

View File

@ -20,7 +20,7 @@ from nonebot.rule import (
@pytest.mark.asyncio
@pytest.mark.parametrize(
"matcher_name, pre_rule_factory, has_permission",
("matcher_name", "pre_rule_factory", "has_permission"),
[
pytest.param("matcher_on", None, True),
pytest.param("matcher_on_metaevent", None, False),

View File

@ -74,11 +74,11 @@ async def test_rule(app: App):
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await Rule(falsy)(bot, event, {}) == False
assert await Rule(truthy)(bot, event, {}) == True
assert await Rule(skipped)(bot, event, {}) == False
assert await Rule(truthy, falsy)(bot, event, {}) == False
assert await Rule(truthy, skipped)(bot, event, {}) == False
assert await Rule(falsy)(bot, event, {}) is False
assert await Rule(truthy)(bot, event, {}) is True
assert await Rule(skipped)(bot, event, {}) is False
assert await Rule(truthy, falsy)(bot, event, {}) is False
assert await Rule(truthy, skipped)(bot, event, {}) is False
@pytest.mark.asyncio
@ -118,7 +118,7 @@ async def test_trie(app: App):
@pytest.mark.asyncio
@pytest.mark.parametrize(
"msg, ignorecase, type, text, expected",
("msg", "ignorecase", "type", "text", "expected"),
[
("prefix", False, "message", "prefix_", True),
("prefix", False, "message", "Prefix_", False),
@ -158,7 +158,7 @@ async def test_startswith(
@pytest.mark.asyncio
@pytest.mark.parametrize(
"msg, ignorecase, type, text, expected",
("msg", "ignorecase", "type", "text", "expected"),
[
("suffix", False, "message", "_suffix", True),
("suffix", False, "message", "_Suffix", False),
@ -198,7 +198,7 @@ async def test_endswith(
@pytest.mark.asyncio
@pytest.mark.parametrize(
"msg, ignorecase, type, text, expected",
("msg", "ignorecase", "type", "text", "expected"),
[
("fullmatch", False, "message", "fullmatch", True),
("fullmatch", False, "message", "Fullmatch", False),
@ -238,7 +238,7 @@ async def test_fullmatch(
@pytest.mark.asyncio
@pytest.mark.parametrize(
"kws, type, text, expected",
("kws", "type", "text", "expected"),
[
(("key",), "message", "_key_", True),
(("key", "foo"), "message", "_foo_", True),
@ -270,26 +270,26 @@ async def test_keyword(
@pytest.mark.asyncio
@pytest.mark.parametrize(
"cmds, force_whitespace, cmd, whitespace, arg_text, expected",
("cmds", "force_whitespace", "cmd", "whitespace", "arg_text", "expected"),
[
# command tests
[(("help",),), None, ("help",), None, None, True],
[(("help",),), None, ("foo",), None, None, False],
[(("help", "foo"),), None, ("help", "foo"), None, None, True],
[(("help", "foo"),), None, ("help", "bar"), None, None, False],
[(("help",), ("foo",)), None, ("help",), None, None, True],
[(("help",), ("foo",)), None, ("bar",), None, None, False],
((("help",),), None, ("help",), None, None, True),
((("help",),), None, ("foo",), None, None, False),
((("help", "foo"),), None, ("help", "foo"), None, None, True),
((("help", "foo"),), None, ("help", "bar"), None, None, False),
((("help",), ("foo",)), None, ("help",), None, None, True),
((("help",), ("foo",)), None, ("bar",), None, None, False),
# whitespace tests
[(("help",),), True, ("help",), " ", "arg", True],
[(("help",),), True, ("help",), None, "arg", False],
[(("help",),), True, ("help",), None, None, True],
[(("help",),), False, ("help",), " ", "arg", False],
[(("help",),), False, ("help",), None, "arg", True],
[(("help",),), False, ("help",), None, None, True],
[(("help",),), " ", ("help",), " ", "arg", True],
[(("help",),), " ", ("help",), "\n", "arg", False],
[(("help",),), " ", ("help",), None, "arg", False],
[(("help",),), " ", ("help",), None, None, True],
((("help",),), True, ("help",), " ", "arg", True),
((("help",),), True, ("help",), None, "arg", False),
((("help",),), True, ("help",), None, None, True),
((("help",),), False, ("help",), " ", "arg", False),
((("help",),), False, ("help",), None, "arg", True),
((("help",),), False, ("help",), None, None, True),
((("help",),), " ", ("help",), " ", "arg", True),
((("help",),), " ", ("help",), "\n", "arg", False),
((("help",),), " ", ("help",), None, "arg", False),
((("help",),), " ", ("help",), None, None, True),
],
)
async def test_command(
@ -424,7 +424,7 @@ async def test_shell_command():
@pytest.mark.asyncio
@pytest.mark.parametrize(
"pattern, type, text, expected, matched",
("pattern", "type", "text", "expected", "matched"),
[
(
r"(?P<key>key\d)",

View File

@ -16,21 +16,21 @@ async def test_matcher_mutex():
event_3 = make_fake_event(_session_id=None)()
async with am(event) as ctx:
assert ctx == False
assert ctx is False
assert not _running_matcher
async with am(event) as ctx:
async with am(event_1) as ctx_1:
assert ctx == False
assert ctx_1 == True
assert ctx is False
assert ctx_1 is True
assert not _running_matcher
async with am(event) as ctx:
async with am(event_2) as ctx_2:
assert ctx == False
assert ctx_2 == False
assert ctx is False
assert ctx_2 is False
assert not _running_matcher
async with am(event_3) as ctx_3:
assert ctx_3 == False
assert ctx_3 is False
assert not _running_matcher

View File

@ -118,7 +118,9 @@ def test_dataclass_encoder():
ms = FakeMessageSegment.nested(FakeMessage(FakeMessageSegment.text("text")))
s = json.dumps(ms, cls=DataclassEncoder)
assert (
s
== '{"type": "node", "data": {"content": [{"type": "text", "data": {"text": "text"}}]}}'
assert s == (
"{"
'"type": "node", '
'"data": {"content": [{"type": "text", "data": {"text": "text"}}]}'
"}"
)