From 213f89b240675947a2287280eba9bf148346c52b Mon Sep 17 00:00:00 2001 From: Grieve Date: Tue, 3 Dec 2024 12:14:39 +0800 Subject: [PATCH] chore: add format --- wren-core-py/justfile | 8 +++ wren-core-py/pyproject.toml | 92 ++++++++++++++++++++++++ wren-core-py/tests/__init__.py | 0 wren-core-py/tests/test_modeling_core.py | 10 +-- 4 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 wren-core-py/tests/__init__.py diff --git a/wren-core-py/justfile b/wren-core-py/justfile index 509cc05bd..16bfd0be3 100644 --- a/wren-core-py/justfile +++ b/wren-core-py/justfile @@ -13,3 +13,11 @@ develop: test: develop cargo test --no-default-features poetry run pytest + +alias fmt := format + +format: + cargo fmt + poetry run ruff format . + poetry run ruff check --fix . + taplo fmt diff --git a/wren-core-py/pyproject.toml b/wren-core-py/pyproject.toml index c8efa218b..702a82fc4 100644 --- a/wren-core-py/pyproject.toml +++ b/wren-core-py/pyproject.toml @@ -14,6 +14,7 @@ maturin = "1.7.5" [tool.poetry.group.dev.dependencies] pytest = "8.3.3" +ruff = "0.8.0" [tool.maturin] module-name = "wren_core" @@ -25,3 +26,94 @@ features = ["pyo3/extension-module"] [build-system] requires = ["maturin>=1.0,<2.0"] build-backend = "maturin" + +[tool.ruff] +line-length = 88 +target-version = "py311" +exclude = ["tools/"] + +[tool.ruff.lint] +select = [ + "C4", # comprehensions + "D", # pydocstyle + "E", # pycodestyle + "EXE", # flake8-executable + "F", # pyflakes + "FA", # flake8-future-annotations + "G", # flake8-logging-format + "FLY", # flynt (format string conversion) + "I", # isort + "ICN", # flake8-import-conventions + "INP", # flake8-no-pep420 (implicit namespace packages) + "ISC", # flake8-implicit-str-concat + "PGH", # pygrep-hooks + "PIE", # flake8-pie + "PL", # pylint + "RET", # flake8-return + "RUF", # ruff-specific rules + "SIM", # flake8-simplify + "T10", # flake8-debugger + "T20", # flake8-print + "TID", # flake8-tidy-imports + "UP", # pyupgrade + "YTT", # flake8-2020 +] +ignore = [ + "B008", # do not perform function calls in argument defaults + "B028", # required stacklevel argument to warn + "B904", # raise from e or raise from None in exception handlers + "B905", # zip-without-explicit-strict + "C408", # dict(...) as literal + "C901", # too complex + "D100", # public module + "D101", # public class + "D102", # public method + "D103", # public function + "D104", # public package + "D105", # magic methods + "D106", # nested class + "D107", # init + "D202", # blank lines after function docstring + "D203", # blank line before class docstring + "D213", # Multi-line docstring summary should start at the second line + "D401", # Imperative mood + "D402", # First line should not be the function's signature + "D413", # Blank line required after last section + "E501", # line-too-long, this is automatically enforced by ruff format + "E731", # lambda-assignment + "ISC001", # single line implicit string concat, handled by ruff format + "PGH003", # blanket-type-ignore + "PLC0105", # covariant type parameters should have a _co suffix + "PLR0124", # name compared with self, e.g., a == a + "PLR0911", # too many return statements + "PLR0912", # too many branches + "PLR0913", # too many arguments + "PLR0915", # too many statements + "PLR2004", # forces everything to be a constant + "PLW2901", # overwriting loop variable + "RET504", # unnecessary-assign, these are useful for debugging + "RET505", # superfluous-else-return, stylistic choice + "RET506", # superfluous-else-raise, stylistic choice + "RET507", # superfluous-else-continue, stylistic choice + "RET508", # superfluous-else-break, stylistic choice + "RUF005", # splat instead of concat + "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` + "S101", # ignore "Use of `assert` detected" + "SIM102", # nested ifs + "SIM108", # convert everything to ternary operator + "SIM114", # combine `if` branches using logical `or` operator + "SIM116", # dictionary instead of `if` statements + "SIM117", # nested with statements + "SIM118", # remove .keys() calls from dictionaries + "SIM300", # yoda conditions + "UP007", # Optional[str] -> str | None + "UP038", # non-pep604-isinstance, results in slower code + "W191", # indentation contains tabs +] +# none of these codes will be automatically fixed by ruff +unfixable = [ + "T201", # print statements + "F401", # unused imports + "RUF100", # unused noqa comments + "F841", # unused variables +] diff --git a/wren-core-py/tests/__init__.py b/wren-core-py/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/wren-core-py/tests/test_modeling_core.py b/wren-core-py/tests/test_modeling_core.py index 814a679a8..880a6d290 100644 --- a/wren-core-py/tests/test_modeling_core.py +++ b/wren-core-py/tests/test_modeling_core.py @@ -4,8 +4,8 @@ import pytest from wren_core import ( - SessionContext, Extractor, + SessionContext, to_json_base64, ) @@ -123,9 +123,7 @@ def test_read_function_list(): def test_get_available_functions(): session_context = SessionContext(manifest_str, "tests/functions.csv") functions = session_context.get_available_functions() - add_two = next( - filter(lambda x: x["name"] == "add_two", map(lambda x: x.to_dict(), functions)) - ) + add_two = next(x.to_dict() for x in functions if x["name"] == "add_two") assert add_two["name"] == "add_two" assert add_two["function_type"] == "scalar" assert add_two["description"] == "Adds two numbers together." @@ -133,9 +131,7 @@ def test_get_available_functions(): assert add_two["param_names"] == "f1,f2" assert add_two["param_types"] == "int,int" - max_if = next( - filter(lambda x: x["name"] == "max_if", map(lambda x: x.to_dict(), functions)) - ) + max_if = next(x.to_dict() for x in functions if x["name"] == "max_if") assert max_if["name"] == "max_if" assert max_if["function_type"] == "window" assert max_if["param_names"] is None