Formatter: Beta feedback #7310
Replies: 45 comments 155 replies
-
Great work you guys are doing here. I would like to try it, but since all the projects I am involved always use black with |
Beta Was this translation helpful? Give feedback.
-
This looks fantastic! It would be great if the Ruff formatter supported an option equivalent to x = [
1,
2,
] will be formatted as:
if |
Beta Was this translation helpful? Give feedback.
-
Is the formatter enabled by default or is there a rule code? |
Beta Was this translation helpful? Give feedback.
-
There is a big problem (imo) in black formatting fluent interfaces right now that could be a great improvement and argument in favor of ruff formatter if it is well impelemented. It is documented in this issue in the black repository and this comment has a good list of examples that could be used as reference. Original Code
Formatted Code
Expected Code
|
Beta Was this translation helpful? Give feedback.
-
Thank you for the formatter effort, it looks fantastic. I ran Our test suite has large SOAP examples written as
and
In fact it seems to me that the only other issue we have is similar, because it relates to chain of function calls like
where
|
Beta Was this translation helpful? Give feedback.
-
Other than a bug (which I've raised) this is looking great! I know that the first steps is to be (mostly) equivalent to Black to help users migrate over. But here's a gripe I have with Black (and prettier in the JS/TS ecosystem), that I'd like to bring forth: Force-unwrapping can lead to less readable code and more git changesSee my comment there #1904 (comment) and this discussion point about fluent interfaces here #7310 (comment) tl;dr: Splitting off strings, ternary conditions, comprehensions and method calls/multiple attribute access into multiple lines increases readability. Unwrapping those decreases readability. And "magic commas" are not usable here. A workaround is to add a bunch of
I ran Ruff on one of my repos currently using autopep8 and split off undesirable formatting into a single commit here to make it easier to preview a bunch of examples: Toufool/AutoSplit@ |
Beta Was this translation helpful? Give feedback.
-
Thanks for inventing your time into this! Is customisability of rufformat's (dummy name i am using until an "official" one appears, "ruff python formatter" is a little verbose haha) one of it's future goals (possibly with built-in presets like black, google, etc.)? I originally switched from black to yapf due to it's great customisability and ability to match the exact styling i prefer, so if you're asking what prevents concretely me from migrating, is lack of configuration options. |
Beta Was this translation helpful? Give feedback.
-
So far no complaints, it's great. I'm really looking forward for ruff's future as a one-stop dev tool for python :) |
Beta Was this translation helpful? Give feedback.
-
Sorry for the tiny-ness and pedestrian-ness of my comment / question here, but my giant gripe with black is the fixed 4-space indent. I’ve recently started using ruff’s vscode extension for linting which seems to work fantastically, and just flipped on the formatter. I’m happy with everything it did to my code except the damn 4-space indents. I noted a merge from last week that deals with tab-size configurability and my questions are:
|
Beta Was this translation helpful? Give feedback.
-
I recently tested Ruff on your project, specifically in a scenario where we use the black formatter with the Is there any way we could align Ruff's quote formatting with the more flexible configuration options provided by flake8-quotes? |
Beta Was this translation helpful? Give feedback.
-
I haven't seen it discussed yet, but have you considered supporting optionally sorting imports within the Many people that use One of the papercuts about this is that often you want to both format your code and sort your imports together (or validate that this is correct). Currently you need to do something like:
And then this needs to be duplicated when you want to
Real world example: What if
which would also sort your imports based on the config in (optionally defaulting to the [tool.ruff.isort] Then users would only ever need to know a single command to change anything about their source code. I think it would also make import sorting more discoverable for new developers. This would also allow IDEs to fully outsource all formatting (sorting and formatting) to Ruff. |
Beta Was this translation helpful? Give feedback.
-
Have you considered (I assume yes, since Ruff is written in Rust 😉) to instead of naming the command:
To use:
To make it shorter? This also matches the previous art in |
Beta Was this translation helpful? Give feedback.
-
Hi, I'd like to open up a discussion thread on the indentation width at function definitions. My feedback/request is: can the Ruff Formatter implement a toggle to turn on/off the 8-space indentation? (Toggle on: 8 spaces; toggle off: 4 spaces) Black's default behavior is: 4-space indentation at function definitions, like this:
This would lead to reduced readability, because the function name and the argument names become harder to distinguish. Adding one more level of indentation greatly improves readability:
PEP8 also has the same suggestion: https://peps.python.org/pep-0008/#indentation There have been a lot of discussion on adding this feature (8-space indentation) in Black's issue board. Black's developers did not implement this. Here are the feature requests that I can find:
I'm also tagging the people who I think are in favor or this feature here: @jni, @flexatone, @Martmists-GH, @stefanoborini, @mintchkin, @nbievre, @veritas9872, @nilsbunger, @LeonarddeR. (This is not an exhaustive list by any means.) |
Beta Was this translation helpful? Give feedback.
-
That's very interesting for me because I am consolidating projects into a monorepo and each has a different set of formatting. Eventually I want consistency across all subpackages, but I want to do that incrementally. I've been trying to think of a way to do that, given that we share a global |
Beta Was this translation helpful? Give feedback.
-
Hi, i've tried out the formatter, and it's great!
Please let me know if the way i'm currently configuring the formatter is incorrect/hacky. Thanks! Apart from these, it works superb! I've been trying to find a good replacement for yapf, since VSCode is deprecating it soon. Thanks for all the awesome work on ruff! |
Beta Was this translation helpful? Give feedback.
-
Re E501 comment: Should the conflicting lint rules section have a call-out regarding E501 like the FAQ entry on Black? For example:
|
Beta Was this translation helpful? Give feedback.
-
Just tried using ruff as a drop-in replacement for black, with which I use the "preview" style, but stopped to post here when I noticed that ruff is inserting unwanted blank lines between the Here is one example of changes that Can ruff add support for opting out of the blank lines between overload variants? Ref: psf/black#1797 |
Beta Was this translation helpful? Give feedback.
-
Does the ruff formatter respect settings in ruff.toml? [format]
line-ending = "lf"
quote-style = "single" This does not work, only in pyproject.toml |
Beta Was this translation helpful? Give feedback.
-
I'd like a Without the config files, had some confusion with the VSCode extension. My ruff linter would only complain when lines are over 120 characters long, but saving would wrap lines down to 80 characters. The only way to stop that from happening seems to be to add a config file. Has this been omitted on purpose? I couldn't find any discussion about this in the issues (besides in a comment in this discussion) |
Beta Was this translation helpful? Give feedback.
-
Just my 2c. I've read that adding configuration will increase complexity (totally makes sense), but it'd be really nice to have. If not a choice of styles, then at least an option to disable certain formatting features (like unwrapping). You can deal with it using a comma, but you can't use it in a statement and the comma is generally an annoyance to me. I'll also say that while being compatible with black is great for black users, I'm one of the users that don't like black for its lack of configuration. The same reason I dislike gofmt, they often make code less clear without a proper and easy way to disable such behaviour. I had less fights with the team for style than I had fights with the formatters, people can be reasonable, machines (unless allowed to be configurable :)) cannot be. |
Beta Was this translation helpful? Give feedback.
-
If I have indent-style set to "tab", when I run ruff format it is still converting indentation within docstring comments to be spaces, which then causes ruff check to fail on rule E101: Indentation contains mixed spaces and tabs Before: After: Is there a format setting I can add to fix this? or is this a bug/intentional? |
Beta Was this translation helpful? Give feedback.
-
I would love the formatter to allow more than one style and it will stay the way the programmer leaves the code. So a "non-opinionated" formatter. Sometimes style A is more readable, some other time style B. For example. Let the programmer select whether the ending parenthesis should go on the same line or not. Could be configured to select allowed styles with a list, e.g.: [tool.ruff.format]
arglist_closing_parenthesis_allowed_position = "same_line, separate_line"
def perform_some_tasks(
data: List[float],
arg2: Union[int, str, bool]
) -> None:
...
def perform_some_tasks(
data: List[float],
arg2: Union[int, str, bool]) -> None:
... |
Beta Was this translation helpful? Give feedback.
-
To me, readability is the only goal of a formatter. "easy to edit" or "small diff" has low value in my book. Therefor I would like an option to let function call arguments be grouped on as few lines as possible. I will use a short line_length just for this examples sake: def generate(
rng: RandomGenerator,
configuration: Config,
style: str,
num_pages: int,
language: str,
include_pictures: bool) -> str:
pass
# this call fits on two lines
generate(rng, config, cool_style, 10
"romanian", include_pictures=True) |
Beta Was this translation helpful? Give feedback.
-
I note that the issue says "we are especially interested in feedback on (1) formatter configuration" but in the context of the formatter's design goals in the documentation which says:
If reading this with emphasis on initial, then a lot of the discussion above about changes to the black style are Personally there are things in the black style I don't like, but accept as I don't want to get bogged down in style discussions - I would rather push for less style configuration options. I interpreted this issue much more narrowly - configuration in the sense of non-style details like including/excluding files (e.g. what extensions to check, should it do automatic recursion into given directories), optionally confirming the AST didn't change, ways to apply the formatter to code blocks embedded in documentation (e.g docstrings, code blocks in RST, or verbatim code blocks in LaTex, as in the tool [Edited as per comment below, on topic to off topic, see italics and strikeout] |
Beta Was this translation helpful? Give feedback.
-
Personally, I think that the real future of code formatting is to make it significantly more intelligent. Think of something like latex that considers a badness for introducing line breaks in various places. The ideal formatter would be something that considered the badness of various breaks in various places according to:
The formatter should also consider vertical formatting as one of the options in my opinion. Vertical formatting uses less vertical space while maintaining separation. Because it uses more indentation, it's not always better; it can induce other unwanted breaks. The line-breaking algorithm would need to be recursive since various break choices can affect other break choices. This can get quite complicated, so you may need to memoize results to maintain performance (dynamic programming). In other words, I'm saying, the kinds of options we need are not just about what the formatter should allow itself to do, but about how it weighs the different options and chooses what to do. |
Beta Was this translation helpful? Give feedback.
-
I just made the switch from Black to Ruff Formatter. This is so much nicer! I haven't been this excited since when I switched from Flake8 to Ruff Linter. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Are there a plans to share more config options related to formatting with defaults set as |
Beta Was this translation helpful? Give feedback.
-
Have been using Ruff's formatter for months now, thanks heaps for this great project. One thing I'm yearning for (yet another user-reckons-just-add-an-option...) is if Ruff could be configured to leave line-breaking alone? Go's legendary formatter largely works like this. It could be an easier way to sidestep the questions above about breaks in comprehensions, intelligent latex-like breaking, etc... basically it means if I want to take up the responsibility of line breaking, then Ruff would let me do so. Theoretical examples: def some_long_function(arg1, arg2,
arg3, ...):
...
# ->
def some_long_function(arg1, arg2,
arg3, ...):
... def some_long_function(
arg1, arg2, arg3):
...
# ->
def some_long_function(
arg1, arg2, arg3
):
... [x for x in some_long_iter if x and x and x and x and x and x and x and x and x....]
# ->
[x for x in some_long_iter if x and x and x and x and x and x and x and x and x....] [x
for x in some_long_iter
if x and x and x and x and x and x and x and x and x....]
# ->
[x
for x in some_long_iter
if x and x and x and x and x and x and x and x and x...] [
x for x in some_long_iter
if x and x and x and x and x and x and x and x...]
# ->
[
x for x in some_long_iter
if x and x and x and x and x and x and x and x...
] I think the cases I'm expecting here are:
|
Beta Was this translation helpful? Give feedback.
-
This discussion aimed to collect feedback on the formatter alpha and beta. Now that the formatter is stable, we ask you to create a new discussion or issue if you have feedback that you want to share.
The Ruff formatter is out now as a Beta release, as of Ruff v0.1.2.
This Discussion exists to collate open-ended feedback on the formatter beta. (Bugs and undocumented deviations should instead be filed in the issue tracker.)
Are there capabilities that preventing you from migrating? Formatting deviations that don't work for your project? Is the formatter the greatest piece of software you've ever used? Let us know -- we'd love to hear from you.
The Ruff formatter is designed as a drop-in replacement for Black, with an excessive focus on performance and direct integration with Ruff. At present, it achieves over > 99.9% compatibility on Black-formatted projects like Django and Zulip.
We consider the Beta production-ready. Between the Beta and Stable release, we'll be focused on:
During the Beta, we are especially interested in feedback on (1) formatter configuration, and (2)
deeper integrations between the formatter and the linter. We'd love to hear from you.
For more on the formatter's design goals, and instructions on using it from the CLI or VS Code, see the documentation.
P.S. This Discussion was previously used for feedback on the Alpha release.
Beta Was this translation helpful? Give feedback.
All reactions