From 9ac5b9a9cffaf59546b92ad9a8d94597ebe682b8 Mon Sep 17 00:00:00 2001 From: Pavel Zwerschke Date: Wed, 25 Oct 2023 14:58:33 +0200 Subject: [PATCH] Use ruff-format instead of black --- .pre-commit-config.yaml | 10 ++-------- polarify/main.py | 20 +++++--------------- pyproject.toml | 21 +++++++++------------ tests/test_parse_body.py | 10 ++-------- 4 files changed, 18 insertions(+), 43 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 11160d5..a2f4d4d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,16 +7,10 @@ repos: - id: trailing-whitespace - id: check-toml - repo: https://github.com/quantco/pre-commit-mirrors-ruff - rev: 0.0.291 + rev: 0.1.2 hooks: - id: ruff-conda - - repo: https://github.com/quantco/pre-commit-mirrors-black - rev: 23.9.1 - hooks: - - id: black-conda - args: - - --safe - - --target-version=py39 + - id: ruff-format-conda - repo: https://github.com/quantco/pre-commit-mirrors-mypy rev: 1.5.1 hooks: diff --git a/polarify/main.py b/polarify/main.py index 0a533f4..dad210e 100644 --- a/polarify/main.py +++ b/polarify/main.py @@ -8,13 +8,9 @@ # TODO: match ... case -def build_polars_when_then_otherwise( - test: ast.expr, then: ast.expr, orelse: ast.expr -) -> ast.Call: +def build_polars_when_then_otherwise(test: ast.expr, then: ast.expr, orelse: ast.expr) -> ast.Call: when_node = ast.Call( - func=ast.Attribute( - value=ast.Name(id="pl", ctx=ast.Load()), attr="when", ctx=ast.Load() - ), + func=ast.Attribute(value=ast.Name(id="pl", ctx=ast.Load()), attr="when", ctx=ast.Load()), args=[test], keywords=[], ) @@ -60,9 +56,7 @@ def visit_UnaryOp(self, node: ast.UnaryOp) -> ast.UnaryOp: def visit_Call(self, node: ast.Call) -> ast.Call: node.args = [self.visit(arg) for arg in node.args] - node.keywords = [ - ast.keyword(arg=k.arg, value=self.visit(k.value)) for k in node.keywords - ] + node.keywords = [ast.keyword(arg=k.arg, value=self.visit(k.value)) for k in node.keywords] return node def visit_IfExp(self, node: ast.IfExp) -> ast.Call: @@ -107,9 +101,7 @@ def _handle_assign(stmt: ast.Assign, assignments: dict[str, ast.expr]): ) assert len(t.elts) == len(stmt.value.elts) for sub_t, sub_v in zip(t.elts, stmt.value.elts): - diff = _handle_assign( - ast.Assign(targets=[sub_t], value=sub_v), assignments - ) + diff = _handle_assign(ast.Assign(targets=[sub_t], value=sub_v), assignments) assignments.update(diff) else: raise ValueError( @@ -196,9 +188,7 @@ def is_returning_body(stmts: list[ast.stmt]) -> bool: return False -def parse_body( - full_body: list[ast.stmt], assignments: dict[str, ast.expr] | None = None -) -> State: +def parse_body(full_body: list[ast.stmt], assignments: dict[str, ast.expr] | None = None) -> State: if assignments is None: assignments = {} state = State(UnresolvedState(assignments)) diff --git a/pyproject.toml b/pyproject.toml index d654c07..f924e94 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,18 +31,6 @@ include = [ "/polarify", ] -[tool.black] -target-version = ["py39"] -exclude = ''' -/( - \.eggs - | \.git - | \.venv - | build - | dist -)/ -''' - [tool.ruff] line-length = 100 target-version = "py39" @@ -73,6 +61,15 @@ select = [ # pyupgrade "UP" ] +ignore = [ + # may cause conflicts with ruff formatter + "E501", + "W191" +] + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" [tool.mypy] python_version = "3.9" diff --git a/tests/test_parse_body.py b/tests/test_parse_body.py index 237e924..d5073a2 100644 --- a/tests/test_parse_body.py +++ b/tests/test_parse_body.py @@ -19,10 +19,7 @@ @pytest.fixture( scope="module", params=functions - + [ - pytest.param(f, marks=pytest.mark.xfail(reason="not implemented")) - for f in xfail_functions - ], + + [pytest.param(f, marks=pytest.mark.xfail(reason="not implemented")) for f in xfail_functions], ) def test_funcs(request): original_func = request.param @@ -30,10 +27,7 @@ def test_funcs(request): original_func_unparsed = inspect.getsource(original_func) # build ast from transformed function as format as string transformed_func_unparsed = transform_func_to_new_source(original_func) - print( - f"Original:\n{original_func_unparsed}\n" - f"Transformed:\n{transformed_func_unparsed}" - ) + print(f"Original:\n{original_func_unparsed}\n" f"Transformed:\n{transformed_func_unparsed}") return transformed_func, original_func