Skip to content

Commit

Permalink
🚨 Develop: 完全使用 ruff 替代 isort 与 black (#3151)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Dec 1, 2024
1 parent 6dad4d2 commit 081dc83
Show file tree
Hide file tree
Showing 83 changed files with 1,783 additions and 1,737 deletions.
5 changes: 1 addition & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
"vscode": {
"settings": {
"python.analysis.diagnosticMode": "workspace",
"ruff.organizeImports": false,
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.fixAll.ruff": "explicit",
"source.organizeImports": "explicit"
Expand Down Expand Up @@ -43,8 +42,6 @@
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"ms-python.isort",
"ms-python.black-formatter",
"charliermarsh.ruff",
"EditorConfig.EditorConfig",
"esbenp.prettier-vscode",
Expand Down
16 changes: 3 additions & 13 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,12 @@ ci:
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.7.2
rev: v0.8.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
args: [--fix]
stages: [pre-commit]

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

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

- repo: https://github.com/nonebot/nonemoji
Expand Down
613 changes: 325 additions & 288 deletions envs/pydantic-v1/poetry.lock

Large diffs are not rendered by default.

619 changes: 328 additions & 291 deletions envs/pydantic-v2/poetry.lock

Large diffs are not rendered by default.

168 changes: 99 additions & 69 deletions envs/test/poetry.lock

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions nonebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@
description: nonebot 模块
"""

import os
from importlib.metadata import version
from typing import Any, Union, TypeVar, Optional, overload
import os
from typing import Any, Optional, TypeVar, Union, overload

import loguru

from nonebot.adapters import Adapter, Bot
from nonebot.compat import model_dump
from nonebot.config import DOTENV_TYPE, Config, Env
from nonebot.drivers import ASGIMixin, Driver, combine_driver
from nonebot.log import logger as logger
from nonebot.adapters import Bot, Adapter
from nonebot.config import DOTENV_TYPE, Env, Config
from nonebot.utils import escape_tag, resolve_dot_notation
from nonebot.drivers import Driver, ASGIMixin, combine_driver

try:
__version__ = version("nonebot2")
Expand Down Expand Up @@ -337,31 +337,31 @@ def run(*args: Any, **kwargs: Any) -> None:
get_driver().run(*args, **kwargs)


from nonebot.plugin import on as on
from nonebot.plugin import on_type as on_type
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
from nonebot.plugin import get_plugin as get_plugin
from nonebot.plugin import on_command as on_command
from nonebot.plugin import on_keyword as on_keyword
from nonebot.plugin import on_message as on_message
from nonebot.plugin import on_request as on_request
from nonebot.plugin import load_plugin as load_plugin
from nonebot.plugin import on_endswith as on_endswith
from nonebot.plugin import CommandGroup as CommandGroup
from nonebot.plugin import MatcherGroup as MatcherGroup
from nonebot.plugin import get_available_plugin_names as get_available_plugin_names
from nonebot.plugin import get_loaded_plugins as get_loaded_plugins
from nonebot.plugin import get_plugin as get_plugin
from nonebot.plugin import get_plugin_by_module_name as get_plugin_by_module_name
from nonebot.plugin import get_plugin_config as get_plugin_config
from nonebot.plugin import load_all_plugins as load_all_plugins
from nonebot.plugin import load_builtin_plugin as load_builtin_plugin
from nonebot.plugin import load_builtin_plugins as load_builtin_plugins
from nonebot.plugin import load_from_json as load_from_json
from nonebot.plugin import load_from_toml as load_from_toml
from nonebot.plugin import load_plugin as load_plugin
from nonebot.plugin import load_plugins as load_plugins
from nonebot.plugin import on as on
from nonebot.plugin import on_command as on_command
from nonebot.plugin import on_endswith as on_endswith
from nonebot.plugin import on_fullmatch as on_fullmatch
from nonebot.plugin import on_keyword as on_keyword
from nonebot.plugin import on_message as on_message
from nonebot.plugin import on_metaevent as on_metaevent
from nonebot.plugin import on_startswith as on_startswith
from nonebot.plugin import load_from_json as load_from_json
from nonebot.plugin import load_from_toml as load_from_toml
from nonebot.plugin import load_all_plugins as load_all_plugins
from nonebot.plugin import on_notice as on_notice
from nonebot.plugin import on_regex as on_regex
from nonebot.plugin import on_request as on_request
from nonebot.plugin import on_shell_command as on_shell_command
from nonebot.plugin import get_plugin_config as get_plugin_config
from nonebot.plugin import get_loaded_plugins as get_loaded_plugins
from nonebot.plugin import load_builtin_plugin as load_builtin_plugin
from nonebot.plugin import load_builtin_plugins as load_builtin_plugins
from nonebot.plugin import get_plugin_by_module_name as get_plugin_by_module_name
from nonebot.plugin import get_available_plugin_names as get_available_plugin_names
from nonebot.plugin import on_startswith as on_startswith
from nonebot.plugin import on_type as on_type
from nonebot.plugin import require as require
2 changes: 1 addition & 1 deletion nonebot/adapters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
description: nonebot.adapters 模块
"""

from nonebot.internal.adapter import Adapter as Adapter
from nonebot.internal.adapter import Bot as Bot
from nonebot.internal.adapter import Event as Event
from nonebot.internal.adapter import Adapter as Adapter
from nonebot.internal.adapter import Message as Message
from nonebot.internal.adapter import MessageSegment as MessageSegment
from nonebot.internal.adapter import MessageTemplate as MessageTemplate
Expand Down
31 changes: 15 additions & 16 deletions nonebot/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@
"""

from collections.abc import Generator
from functools import cached_property
from dataclasses import dataclass, is_dataclass
from typing_extensions import Self, get_args, get_origin, is_typeddict
from functools import cached_property
from typing import (
TYPE_CHECKING,
Annotated,
Any,
Union,
Generic,
TypeVar,
Callable,
Generic,
Optional,
Protocol,
Annotated,
TypeVar,
Union,
overload,
)
from typing_extensions import Self, get_args, get_origin, is_typeddict

from pydantic import VERSION, BaseModel

Expand All @@ -44,21 +44,21 @@ def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]: ...


__all__ = (
"Required",
"PydanticUndefined",
"PydanticUndefinedType",
"ConfigDict",
"DEFAULT_CONFIG",
"ConfigDict",
"FieldInfo",
"ModelField",
"PydanticUndefined",
"PydanticUndefinedType",
"Required",
"TypeAdapter",
"custom_validation",
"extract_field_info",
"model_fields",
"model_config",
"model_dump",
"type_validate_python",
"model_fields",
"type_validate_json",
"custom_validation",
"type_validate_python",
)

__autodoc__ = {
Expand All @@ -70,9 +70,9 @@ def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]: ...
if PYDANTIC_V2: # pragma: pydantic-v2
from pydantic import GetCoreSchemaHandler
from pydantic import TypeAdapter as TypeAdapter
from pydantic_core import CoreSchema, core_schema
from pydantic._internal._repr import display_as_type
from pydantic.fields import FieldInfo as BaseFieldInfo
from pydantic_core import CoreSchema, core_schema

Required = Ellipsis
"""Alias of Ellipsis for compatibility with pydantic v1"""
Expand Down Expand Up @@ -253,9 +253,8 @@ def custom_validation(class_: type["CVC"]) -> type["CVC"]:
return class_

else: # pragma: pydantic-v1
from pydantic import Extra
from pydantic import parse_obj_as, parse_raw_as
from pydantic import BaseConfig as PydanticConfig
from pydantic import Extra, parse_obj_as, parse_raw_as
from pydantic.fields import FieldInfo as BaseFieldInfo
from pydantic.fields import ModelField as BaseModelField
from pydantic.schema import get_annotation_from_field_info
Expand Down
18 changes: 9 additions & 9 deletions nonebot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@
description: nonebot.config 模块
"""

import os
import abc
import json
from pathlib import Path
from collections.abc import Mapping
from datetime import timedelta
from ipaddress import IPv4Address
from collections.abc import Mapping
from typing import TYPE_CHECKING, Any, Union, Optional
import json
import os
from pathlib import Path
from typing import TYPE_CHECKING, Any, Optional, Union
from typing_extensions import TypeAlias, get_args, get_origin

from dotenv import dotenv_values
from pydantic import Field, BaseModel
from pydantic import BaseModel, Field
from pydantic.networks import IPvAnyAddress

from nonebot.log import logger
from nonebot.typing import origin_is_union
from nonebot.utils import deep_update, type_is_complex, lenient_issubclass
from nonebot.compat import (
PYDANTIC_V2,
ConfigDict,
Expand All @@ -39,6 +36,9 @@
model_config,
model_fields,
)
from nonebot.log import logger
from nonebot.typing import origin_is_union
from nonebot.utils import deep_update, lenient_issubclass, type_is_complex

DOTENV_TYPE: TypeAlias = Union[
Path, str, list[Union[Path, str]], tuple[Union[Path, str], ...]
Expand Down
18 changes: 9 additions & 9 deletions nonebot/dependencies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@
"""

import abc
import inspect
from collections.abc import Awaitable, Iterable
from dataclasses import dataclass, field
from functools import partial
from dataclasses import field, dataclass
from collections.abc import Iterable, Awaitable
from typing import Any, Generic, TypeVar, Callable, Optional, cast
import inspect
from typing import Any, Callable, Generic, Optional, TypeVar, cast

import anyio
from exceptiongroup import BaseExceptionGroup, catch

from nonebot.compat import FieldInfo, ModelField, PydanticUndefined
from nonebot.exception import SkippedException
from nonebot.log import logger
from nonebot.typing import _DependentCallable
from nonebot.exception import SkippedException
from nonebot.compat import FieldInfo, ModelField, PydanticUndefined
from nonebot.utils import (
run_sync,
run_coro_with_shield,
is_coroutine_callable,
flatten_exception_group,
is_coroutine_callable,
run_coro_with_shield,
run_sync,
)

from .utils import check_field_type, get_typed_signature
Expand Down
24 changes: 12 additions & 12 deletions nonebot/drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
"""

from nonebot.internal.driver import URL as URL
from nonebot.internal.driver import Mixin as Mixin
from nonebot.internal.driver import Driver as Driver
from nonebot.internal.driver import Cookies as Cookies
from nonebot.internal.driver import Request as Request
from nonebot.internal.driver import Response as Response
from nonebot.internal.driver import ASGIMixin as ASGIMixin
from nonebot.internal.driver import WebSocket as WebSocket
from nonebot.internal.driver import HTTPVersion as HTTPVersion
from nonebot.internal.driver import ForwardMixin as ForwardMixin
from nonebot.internal.driver import ReverseMixin as ReverseMixin
from nonebot.internal.driver import Cookies as Cookies
from nonebot.internal.driver import Driver as Driver
from nonebot.internal.driver import ForwardDriver as ForwardDriver
from nonebot.internal.driver import ReverseDriver as ReverseDriver
from nonebot.internal.driver import combine_driver as combine_driver
from nonebot.internal.driver import ForwardMixin as ForwardMixin
from nonebot.internal.driver import HTTPClientMixin as HTTPClientMixin
from nonebot.internal.driver import HTTPServerSetup as HTTPServerSetup
from nonebot.internal.driver import HTTPClientSession as HTTPClientSession
from nonebot.internal.driver import HTTPServerSetup as HTTPServerSetup
from nonebot.internal.driver import HTTPVersion as HTTPVersion
from nonebot.internal.driver import Mixin as Mixin
from nonebot.internal.driver import Request as Request
from nonebot.internal.driver import Response as Response
from nonebot.internal.driver import ReverseDriver as ReverseDriver
from nonebot.internal.driver import ReverseMixin as ReverseMixin
from nonebot.internal.driver import WebSocket as WebSocket
from nonebot.internal.driver import WebSocketClientMixin as WebSocketClientMixin
from nonebot.internal.driver import WebSocketServerSetup as WebSocketServerSetup
from nonebot.internal.driver import combine_driver as combine_driver

__autodoc__ = {
"URL": True,
Expand Down
20 changes: 11 additions & 9 deletions nonebot/drivers/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@
description: nonebot.drivers.aiohttp 模块
"""

from typing_extensions import override
from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, Union, Optional
from typing import TYPE_CHECKING, Optional, Union
from typing_extensions import override

from multidict import CIMultiDict

from nonebot.exception import WebSocketClosed
from nonebot.drivers import URL, Request, Response
from nonebot.drivers.none import Driver as NoneDriver
from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.internal.driver import Cookies, QueryTypes, CookieTypes, HeaderTypes
from nonebot.drivers import (
HTTPVersion,
URL,
HTTPClientMixin,
HTTPClientSession,
HTTPVersion,
Request,
Response,
WebSocketClientMixin,
combine_driver,
)
from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers.none import Driver as NoneDriver
from nonebot.exception import WebSocketClosed
from nonebot.internal.driver import Cookies, CookieTypes, HeaderTypes, QueryTypes

try:
import aiohttp
Expand Down Expand Up @@ -174,7 +176,7 @@ async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
async with session.ws_connect(
setup.url,
method=setup.method,
timeout=setup.timeout or 10,
timeout=setup.timeout or 10, # type: ignore
headers=setup.headers,
proxy=setup.proxy,
) as ws:
Expand Down
Loading

0 comments on commit 081dc83

Please sign in to comment.