From a2e52fa8741d9a9b30f6f328b2a22262675fe86b Mon Sep 17 00:00:00 2001 From: Wu Zhenyu Date: Wed, 14 Feb 2024 12:21:23 +0800 Subject: [PATCH] :construction_worker: Use ruff --- .pre-commit-config.yaml | 37 ++++++------------- docs/conf.py | 1 + pyproject.toml | 37 ++++++++++++------- src/termux_language_server/__init__.py | 1 + src/termux_language_server/__main__.py | 4 +- src/termux_language_server/finders.py | 1 + src/termux_language_server/misc/__init__.py | 1 + src/termux_language_server/misc/color_map.py | 8 +++- src/termux_language_server/misc/ebuild.py | 8 +++- src/termux_language_server/misc/licenses.py | 1 + src/termux_language_server/misc/make_conf.py | 8 +++- .../misc/makepkg_conf.py | 6 ++- src/termux_language_server/misc/pkgbuild.py | 37 ++++++++++--------- src/termux_language_server/misc/termux.py | 36 ++++++++++-------- .../packages/__init__.py | 1 + .../packages/pkgbuild.py | 1 + src/termux_language_server/schema.py | 1 + src/termux_language_server/server.py | 1 + src/termux_language_server/tools/namcap.py | 1 + src/termux_language_server/utils.py | 3 +- tests/test_schema.py | 6 +-- tests/test_utils.py | 1 + 22 files changed, 116 insertions(+), 85 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3e136d6..c001c3e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -54,7 +54,7 @@ repos: hooks: - id: actionlint - repo: https://github.com/adrienverge/yamllint - rev: v1.33.0 + rev: v1.34.0 hooks: - id: yamllint - repo: https://github.com/executablebooks/mdformat @@ -68,44 +68,29 @@ repos: - mdformat-toc - mdformat-deflist - mdformat-beautysh - - mdformat-black + - mdformat-ruff + - ruff - mdformat-config + - mdformat-web - repo: https://github.com/DavidAnson/markdownlint-cli2 - rev: v0.10.0 + rev: v0.12.1 hooks: - id: markdownlint-cli2 additional_dependencies: - markdown-it-texmath - repo: https://github.com/scop/pre-commit-shfmt - rev: v3.7.0-4 + rev: v3.8.0-1 hooks: - id: shfmt - - repo: https://github.com/psf/black - rev: 23.11.0 + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.2.1 hooks: - - id: black - - repo: https://github.com/PyCQA/isort - rev: 5.12.0 - hooks: - - id: isort - - repo: https://github.com/pycqa/pydocstyle - rev: 6.3.0 - hooks: - - id: pydocstyle - additional_dependencies: - - tomli + - id: ruff + - id: ruff-format - repo: https://github.com/kumaraditya303/mirrors-pyright - rev: v1.1.335 + rev: v1.1.350 hooks: - id: pyright - - repo: https://github.com/PyCQA/bandit - rev: 1.7.5 - hooks: - - id: bandit - args: - - -cpyproject.toml - additional_dependencies: - - tomli ci: skip: diff --git a/docs/conf.py b/docs/conf.py index 009c886..7d81290 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -2,6 +2,7 @@ https://www.sphinx-doc.org/en/master/usage/configuration.html """ + from termux_language_server import __version__ as version # type: ignore from termux_language_server._metainfo import ( # type: ignore author, diff --git a/pyproject.toml b/pyproject.toml index e2a314b..7bf9fb6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -96,19 +96,33 @@ file = "templates/metainfo.py.j2" [tool.mdformat] number = true -[tool.black] -line-length = 79 +[tool.doq] +template_path = "templates" -[tool.isort] -line_length = 79 -profile = "black" +[tool.ruff] +line-length = 79 -# https://github.com/PyCQA/pydocstyle/issues/418 -[tool.pydocstyle] -add_ignore = "D205, D400" +[tool.ruff.lint] +select = [ + # pycodestyle + "E", + # pyflakes + "F", + # pyupgrade + "UP", + # flake8-bugbear + "B", + # flake8-simplify + "SIM", + # isort + "I", +] +ignore = ["D205", "D400"] +preview = true -[tool.doq] -template_path = "templates" +[tool.ruff.format] +docstring-code-format = true +preview = true [tool.coverage.report] exclude_lines = [ @@ -117,9 +131,6 @@ exclude_lines = [ "\\s*import tomli as tomllib", ] -[tool.bandit.assert_used] -skips = ["*_test.py", "*/test_*.py"] - [tool.cibuildwheel] archs = ["all"] skip = "*37-* *38-* *39-*" diff --git a/src/termux_language_server/__init__.py b/src/termux_language_server/__init__.py index 75a4216..bfd559c 100644 --- a/src/termux_language_server/__init__.py +++ b/src/termux_language_server/__init__.py @@ -1,6 +1,7 @@ r"""Provide ``__version__`` for `importlib.metadata.version() `_. """ + from typing import Literal try: diff --git a/src/termux_language_server/__main__.py b/src/termux_language_server/__main__.py index 063f7fe..65aa79a 100644 --- a/src/termux_language_server/__main__.py +++ b/src/termux_language_server/__main__.py @@ -1,13 +1,13 @@ r"""This module can be called by `python -m `_. """ + from argparse import ArgumentParser, RawDescriptionHelpFormatter from contextlib import suppress from datetime import datetime -from . import FILETYPE +from . import FILETYPE, __version__ from . import __name__ as NAME -from . import __version__ NAME = NAME.replace("_", "-") VERSION = rf"""{NAME} {__version__} diff --git a/src/termux_language_server/finders.py b/src/termux_language_server/finders.py index be35b0c..15c5a27 100644 --- a/src/termux_language_server/finders.py +++ b/src/termux_language_server/finders.py @@ -1,6 +1,7 @@ r"""Finders =========== """ + from copy import deepcopy from dataclasses import dataclass diff --git a/src/termux_language_server/misc/__init__.py b/src/termux_language_server/misc/__init__.py index 6a09d40..6b74d27 100644 --- a/src/termux_language_server/misc/__init__.py +++ b/src/termux_language_server/misc/__init__.py @@ -1,6 +1,7 @@ r"""Misc ======== """ + from typing import Any from .. import FILETYPE diff --git a/src/termux_language_server/misc/color_map.py b/src/termux_language_server/misc/color_map.py index b944992..e98cb42 100644 --- a/src/termux_language_server/misc/color_map.py +++ b/src/termux_language_server/misc/color_map.py @@ -1,6 +1,7 @@ r"""Portage's color.map ======================= """ + from typing import Any from tree_sitter_lsp.misc import get_soup @@ -15,7 +16,10 @@ def init_schema() -> dict[str, dict[str, Any]]: """ filetype = "color.map" schema = { - "$id": f"{SOURCE}/blob/main/src/termux_language_server/assets/json/{filetype}.json", + "$id": ( + f"{SOURCE}/blob/main/" + "src/termux_language_server/assets/json/{filetype}.json" + ), "$schema": "http://json-schema.org/draft-07/schema#", "$comment": ( "Don't edit this file directly! It is generated by " @@ -25,7 +29,7 @@ def init_schema() -> dict[str, dict[str, Any]]: "properties": {}, } dl = get_soup("color.map").find_all("dl")[1] - for dt, dd in zip(dl.find_all("dt"), dl.find_all("dd")): + for dt, dd in zip(dl.find_all("dt"), dl.find_all("dd"), strict=False): name = dt.text.split()[0] description = dd.text.replace("\n", " ").strip() example = dt.text.replace("\n", " ") diff --git a/src/termux_language_server/misc/ebuild.py b/src/termux_language_server/misc/ebuild.py index 7a3870e..6652dcd 100644 --- a/src/termux_language_server/misc/ebuild.py +++ b/src/termux_language_server/misc/ebuild.py @@ -1,6 +1,7 @@ r"""Portage's ebuild ==================== """ + from typing import Any from tree_sitter_lsp.misc import get_soup @@ -16,7 +17,10 @@ def init_schema() -> dict[str, dict[str, Any]]: """ filetype = "ebuild" schema = { - "$id": f"{SOURCE}/blob/main/src/termux_language_server/assets/json/{filetype}.json", + "$id": ( + f"{SOURCE}/blob/main/" + "src/termux_language_server/assets/json/{filetype}.json" + ), "$schema": "http://json-schema.org/draft-07/schema#", "$comment": ( "Don't edit this file directly! It is generated by " @@ -26,7 +30,7 @@ def init_schema() -> dict[str, dict[str, Any]]: "properties": {}, } for dl in get_soup("ebuild").find_all("dl")[20:-2]: - for dt, dd in zip(dl.find_all("dt"), dl.find_all("dd")): + for dt, dd in zip(dl.find_all("dt"), dl.find_all("dd"), strict=False): if dt.strong is None or dt.strong.text.endswith(":"): continue name = dt.strong.text.split()[0] diff --git a/src/termux_language_server/misc/licenses.py b/src/termux_language_server/misc/licenses.py index 8078c78..24f72bd 100644 --- a/src/termux_language_server/misc/licenses.py +++ b/src/termux_language_server/misc/licenses.py @@ -1,6 +1,7 @@ r"""Licenses ============ """ + from license_expression import get_license_index LICENSES = [ diff --git a/src/termux_language_server/misc/make_conf.py b/src/termux_language_server/misc/make_conf.py index dda22fe..41b69ff 100644 --- a/src/termux_language_server/misc/make_conf.py +++ b/src/termux_language_server/misc/make_conf.py @@ -1,6 +1,7 @@ r"""Portage's make.conf ======================= """ + from typing import Any from tree_sitter_lsp.misc import get_soup @@ -15,7 +16,10 @@ def init_schema() -> dict[str, dict[str, Any]]: """ filetype = "make.conf" schema = { - "$id": f"{SOURCE}/blob/main/src/termux_language_server/assets/json/{filetype}.json", + "$id": ( + f"{SOURCE}/blob/main/" + "src/termux_language_server/assets/json/{filetype}.json" + ), "$schema": "http://json-schema.org/draft-07/schema#", "$comment": ( "Don't edit this file directly! It is generated by " @@ -25,7 +29,7 @@ def init_schema() -> dict[str, dict[str, Any]]: "properties": {}, } for dl in get_soup("make.conf").find_all("dl")[:-2]: - for dt, dd in zip(dl.find_all("dt"), dl.find_all("dd")): + for dt, dd in zip(dl.find_all("dt"), dl.find_all("dd"), strict=False): if dt.strong is None: continue name = dt.strong.text.split()[0] diff --git a/src/termux_language_server/misc/makepkg_conf.py b/src/termux_language_server/misc/makepkg_conf.py index 43af06b..245b493 100644 --- a/src/termux_language_server/misc/makepkg_conf.py +++ b/src/termux_language_server/misc/makepkg_conf.py @@ -1,6 +1,7 @@ r"""makepkg.conf ================ """ + from typing import Any from tree_sitter_lsp.misc import get_soup @@ -15,7 +16,10 @@ def init_schema() -> dict[str, dict[str, Any]]: """ filetype = "makepkg.conf" schema = { - "$id": f"{SOURCE}/blob/main/src/termux_language_server/assets/json/{filetype}.json", + "$id": ( + f"{SOURCE}/blob/main/" + "src/termux_language_server/assets/json/{filetype}.json" + ), "$schema": "http://json-schema.org/draft-07/schema#", "$comment": ( "Don't edit this file directly! It is generated by " diff --git a/src/termux_language_server/misc/pkgbuild.py b/src/termux_language_server/misc/pkgbuild.py index 4204394..6d639ff 100644 --- a/src/termux_language_server/misc/pkgbuild.py +++ b/src/termux_language_server/misc/pkgbuild.py @@ -1,6 +1,7 @@ r"""PKGBUILD ============ """ + import os from typing import Any @@ -18,9 +19,9 @@ def get_content(tokens: list[Token]) -> str: :type tokens: list[Token] :rtype: str """ - return "\n".join( - [token.content.replace("\n", " ") for token in tokens if token.content] - ) + return "\n".join([ + token.content.replace("\n", " ") for token in tokens if token.content + ]) def init_schema() -> dict[str, Any]: @@ -31,7 +32,10 @@ def init_schema() -> dict[str, Any]: schemas = {} for filetype in {"PKGBUILD", "install"}: schemas[filetype] = { - "$id": f"{SOURCE}/blob/main/src/termux_language_server/assets/json/{filetype}.json", + "$id": ( + f"{SOURCE}/blob/main/" + "src/termux_language_server/assets/json/{filetype}.json" + ), "$schema": "http://json-schema.org/draft-07/schema#", "$comment": ( "Don't edit this file directly! It is generated by " @@ -63,16 +67,14 @@ def init_schema() -> dict[str, Any]: if token.type == "blockquote_close" ] close_indices = [ - min( - [ - blockquote_close_index - for blockquote_close_index in blockquote_close_indices - if blockquote_close_index > index - ] - ) + min([ + blockquote_close_index + for blockquote_close_index in blockquote_close_indices + if blockquote_close_index > index + ]) for index in indices ] - for index, close_index in zip(indices, close_indices): + for index, close_index in zip(indices, close_indices, strict=False): children = tokens[index].children if children is None: continue @@ -108,9 +110,9 @@ def init_schema() -> dict[str, Any]: schemas[filetype][properties_name][name] = { "description": description } - # makepkg supports building multiple packages from a single PKGBUILD. - # This is achieved by assigning an array of package names to the - # pkgname directive. + # makepkg supports building multiple packages from a single + # PKGBUILD. This is achieved by assigning an array of package names + # to the pkgname directive. if name == "pkgname": schemas[filetype][properties_name][name]["oneOf"] = [ { @@ -129,8 +131,9 @@ def init_schema() -> dict[str, Any]: "uniqueItems": True, } elif kind == "Function": - # Each split package uses a corresponding packaging function with - # name package_foo(), where foo is the name of the split package. + # Each split package uses a corresponding packaging function + # with name package_foo(), where foo is the name of the split + # package. schemas[filetype][properties_name][name]["const"] = 0 # https://archlinux32.org/architecture/ # https://archlinux.org/packages/ diff --git a/src/termux_language_server/misc/termux.py b/src/termux_language_server/misc/termux.py index 54e13ad..dcfd0d0 100644 --- a/src/termux_language_server/misc/termux.py +++ b/src/termux_language_server/misc/termux.py @@ -1,6 +1,7 @@ r"""Termux ========== """ + from typing import Any from tree_sitter_lsp.misc import get_soup @@ -24,7 +25,10 @@ def init_schema() -> dict[str, dict[str, Any]]: schemas = {} for filetype in {"build.sh", "subpackage.sh"}: schemas[filetype] = { - "$id": f"{SOURCE}/blob/main/src/termux_language_server/assets/json/{filetype}.json", + "$id": ( + f"{SOURCE}/blob/main/" + "src/termux_language_server/assets/json/{filetype}.json" + ), "$schema": "http://json-schema.org/draft-07/schema#", "$comment": ( "Don't edit this file directly! It is generated by " @@ -139,27 +143,27 @@ def init_schema() -> dict[str, dict[str, Any]]: schemas["build.sh"]["properties"]["TERMUX_PKG_HOMEPAGE"][ "format" - ] = schemas["build.sh"]["properties"]["TERMUX_PKG_SRCURL"][ - "format" - ] = "uri" - schemas["build.sh"]["properties"]["TERMUX_PKG_MAINTAINER"][ - "default" - ] = "@termux" + ] = schemas["build.sh"]["properties"]["TERMUX_PKG_SRCURL"]["format"] = ( + "uri" + ) + schemas["build.sh"]["properties"]["TERMUX_PKG_MAINTAINER"]["default"] = ( + "@termux" + ) schemas["build.sh"]["properties"]["TERMUX_PKG_UPDATE_METHOD"]["enum"] = [ "github", "gitlab", "repology", ] - schemas["build.sh"]["properties"]["TERMUX_GITLAB_API_HOST"][ - "default" - ] = "gitlab.com" - schemas["build.sh"]["properties"]["TERMUX_GITLAB_API_HOST"][ - "format" - ] = "hostname" + schemas["build.sh"]["properties"]["TERMUX_GITLAB_API_HOST"]["default"] = ( + "gitlab.com" + ) + schemas["build.sh"]["properties"]["TERMUX_GITLAB_API_HOST"]["format"] = ( + "hostname" + ) schemas["build.sh"]["properties"]["TERMUX_PKG_UPDATE_VERSION_REGEXP"][ "format" ] = "regex" - schemas["build.sh"]["properties"]["TERMUX_PKG_LICENSE"][ - "pattern" - ] = rf"{ATOM}(,{ATOM})*" + schemas["build.sh"]["properties"]["TERMUX_PKG_LICENSE"]["pattern"] = ( + rf"{ATOM}(,{ATOM})*" + ) return schemas diff --git a/src/termux_language_server/packages/__init__.py b/src/termux_language_server/packages/__init__.py index bedae70..bdc924f 100644 --- a/src/termux_language_server/packages/__init__.py +++ b/src/termux_language_server/packages/__init__.py @@ -1,6 +1,7 @@ r"""Packages ============ """ + from .. import FILETYPE PACKAGE_VARIABLES = { diff --git a/src/termux_language_server/packages/pkgbuild.py b/src/termux_language_server/packages/pkgbuild.py index b65cacf..3e750af 100644 --- a/src/termux_language_server/packages/pkgbuild.py +++ b/src/termux_language_server/packages/pkgbuild.py @@ -1,6 +1,7 @@ r"""PKGBUILD packages ===================== """ + from pathlib import Path from jinja2 import Template diff --git a/src/termux_language_server/schema.py b/src/termux_language_server/schema.py index 40a79b8..66b92b6 100644 --- a/src/termux_language_server/schema.py +++ b/src/termux_language_server/schema.py @@ -1,6 +1,7 @@ r"""Schema ========== """ + from dataclasses import dataclass from typing import Literal diff --git a/src/termux_language_server/server.py b/src/termux_language_server/server.py index 31d8419..e0f40ec 100644 --- a/src/termux_language_server/server.py +++ b/src/termux_language_server/server.py @@ -1,6 +1,7 @@ r"""Server ========== """ + import re from typing import Any diff --git a/src/termux_language_server/tools/namcap.py b/src/termux_language_server/tools/namcap.py index 47b7ea3..bbe94e1 100644 --- a/src/termux_language_server/tools/namcap.py +++ b/src/termux_language_server/tools/namcap.py @@ -1,6 +1,7 @@ r"""Namcap ========== """ + from lsprotocol.types import Diagnostic, DiagnosticSeverity, Position, Range diff --git a/src/termux_language_server/utils.py b/src/termux_language_server/utils.py index 828c57d..a9f8f93 100644 --- a/src/termux_language_server/utils.py +++ b/src/termux_language_server/utils.py @@ -1,6 +1,7 @@ r"""Utils ========= """ + import json import os from typing import Any, Literal @@ -54,7 +55,7 @@ def get_schema(filetype: FILETYPE) -> dict[str, Any]: ), f"{filetype}.json", ) - with open(file, "r") as f: + with open(file) as f: SCHEMAS[filetype] = json.load(f) return SCHEMAS[filetype] diff --git a/tests/test_schema.py b/tests/test_schema.py index 6267e46..150bef3 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -1,11 +1,11 @@ r"""Test schema.""" -import os -from tree_sitter_languages import get_parser -from tree_sitter_lsp.finders import SchemaFinder +import os from termux_language_server.schema import BashTrie from termux_language_server.utils import get_filetype, get_schema +from tree_sitter_languages import get_parser +from tree_sitter_lsp.finders import SchemaFinder PATH = os.path.dirname(__file__) diff --git a/tests/test_utils.py b/tests/test_utils.py index dbb907f..fd88b8c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,4 +1,5 @@ r"""Test utils.""" + from termux_language_server.utils import get_schema