Skip to content

Commit

Permalink
Use ruff-format instead of black
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw committed Oct 25, 2023
1 parent 81af044 commit 9ac5b9a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 43 deletions.
10 changes: 2 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 5 additions & 15 deletions polarify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=[],
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand Down
21 changes: 9 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
10 changes: 2 additions & 8 deletions tests/test_parse_body.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,15 @@
@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
transformed_func = polarify(original_func)
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


Expand Down

0 comments on commit 9ac5b9a

Please sign in to comment.