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

Keep single-line signatures when body is an Ellipsis (...) in .py files #10026

Closed
etienneschalk opened this issue Feb 18, 2024 · 5 comments
Closed

Comments

@etienneschalk
Copy link

A minimal code snippet that reproduces the bug.

File name: format_ruff.py

Before ruff:

class MyClass:
    def __add__(self, other: int) -> int: ...

After ruff:

class MyClass:
    def __add__(self, other: int) -> int:
        ...

The command you invoked

ruff format format_ruff.py 

Note: in real-life scenario, one might have installed the Ruff extension on VSCode, opened a file with many stubs like in the minimal code snippet, and see them all formatted, introducing unwanted code change, which can be annoying for version control.

This might also slow down ruff adoption in existing large code bases where the case mentioned above happens a lot.

The current Ruff settings

No overriden settings. I ran a pip install ruff then ran the command above

The current Ruff version

ruff --version
ruff 0.2.2

What I would like

A way to opt-out of de-inlining the Ellipsis (...) in stubs. I have searched in documentation without finding such an option. I browsed the following issues (in Links section).

An option could be:

[tool.ruff.format]
keep-single-line-ellipsis = true

Links

Related Issues:

Related PRs:

Thanks!

@MichaReiser
Copy link
Member

Thanks for the excellent writeup! We've implemented your desired behavior but it's currently only applied when you format your code with --preview mode enabled as you can see here https://play.ruff.rs/6cc50dba-b5ae-4dea-9f58-14b179932e65.

We hope to promote the new formatting to stable in this month, but you can already start using it if you enable ruff.format.preview = true or call ruff format --preview

Let me know if that works for you

@etienneschalk
Copy link
Author

Hello, thanks for you answer!

I ran a pre-commit run --all after enabling the preview flag in pyproject.toml:

[tool.ruff.format]
preview = true

It puts back the Ellipses back on their single line 👍

However, it also brings new changes, mainly compressing multiple braces in a javascript formatting style.

Excerpt from the xarray repo:

-        self.ds = Dataset(
-            {
-                "a": (("x", "y"), np.ones((300, 400))),
-                "b": (("x", "y"), np.ones((300, 400))),
-            }
-        )
+        self.ds = Dataset({
+            "a": (("x", "y"), np.ones((300, 400))),
+            "b": (("x", "y"), np.ones((300, 400))),
+        })

Even though I personally like this new formatting (it looks like javascript formatting, is more concise, and saves a level of indentation), I would still like to get more granularity inside of the preview flag, to choose which features to opt-in (orthogonal configuration), reducing code diff at the same time in a migration context.

According to the documentation of Preview we can select rules for the linter with explicit-preview-rules = true, however I cannot find an equivalent for the formatter. Is there a "formatting rule" concept, equivalent to the "linting rules", that would enable me to opt-in only the Ellipsis formatting rule?

Thanks!

@MichaReiser
Copy link
Member

That makes sense. There's no such granular opt-in to preview formatting. It's all or nothing, similar to the formatter itself which only provides very few configuration nobs.

For preview, this is especially important because it ensures that users give feedback to all preview styles and don't just disable the styles they don't like. It prevents situations where all users disabled a controversial style change but we decide releasing it because we never received any negative feedback on the style.

@etienneschalk
Copy link
Author

Okay, I fully understand the point of having a single preview style!

Thanks a lot for the help on that topic!

@MichaReiser
Copy link
Member

I'll close this issue. You can follow #8678 to get notified when we promote the new preview styles (The style you're calling out won't be promoted)

@MichaReiser MichaReiser closed this as not planned Won't fix, can't repro, duplicate, stale Feb 18, 2024
jdegenstein added a commit to jdegenstein/build123d that referenced this issue Feb 21, 2024
thanks to astral-sh/ruff#10026 for suggestion on keeping single line Ellipsis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants