Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

investigate if we can switch from black to ruff for formatting #3241

Closed
mattijn opened this issue Oct 27, 2023 · 1 comment · Fixed by #3243
Closed

investigate if we can switch from black to ruff for formatting #3241

mattijn opened this issue Oct 27, 2023 · 1 comment · Fixed by #3243

Comments

@mattijn
Copy link
Contributor

mattijn commented Oct 27, 2023

With the recent introduction of ruff formatter (docs), we can investigate if we can reduce the number of development dependencies by 1.

All of the black related config within pyproject.toml can be rewritten to ruff style config, but we also use black to format code within tools/update_init_file.py (around here), as such:

# Format file content with black
new_file_content = black.format_str("\n".join(new_lines), mode=black.Mode())

I don't think this usage of black is possible with ruff yet, but please correct me if I'm wrong.

(initial attempt in this branch)

@binste
Copy link
Contributor

binste commented Oct 28, 2023

I'm in favour of moving to ruff! To format strings in the code generation, we can pass the strings through stdin to the ruff format command:

import subprocess

code = """import altair as alt

def some_function():
    d = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10}
    return
    
def another_function():
    c = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
    return
"""
r = subprocess.run(
    # Name of the file does not seem to matter but ruff requires one
    ["ruff", "format", "--stdin-filename", "placeholder.py"],
    input=code.encode(),
    check=True,
    capture_output=True,
)
print(r.stdout.decode())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants