-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Formatter: Add --skip-string-normalization option (parity with Black)? #7525
Comments
Hey Michael! We haven't implemented it yet but we're considering it -- there's some discussion here (#7305). The formatter does support parameterizing on preferred quote style (single vs. double) -- it's not exposed on the CLI or configuration yet (just internally), but will be soon. So the main thing we're trying to figure out is whether the ability to select a preferred quote style obviates the need for |
Thanks for the quick thoughts @charliermarsh! On the Black conversation/issue there are lots of times that people mentioned Another, albeit less common one, that applies to me is RSI issues. I've long had to deal with RSI issues so I'm very careful about how and how much I type (not a huge deal these days but do have to be careful). When typing Those are my two reasons. If you gave us a way to prefer ' over " for normalization, I would not use |
Thanks, that's super helpful. We are planning to expose an option to prefer |
Awesome, I'll keep my eye out for it. :) |
@charliermarsh Should optional features different between Ruff and Black documented? What Ruff has, Black has not, and vice versa. |
I would still be interested in the neutral |
Can you give an example for a short identifier where you prefer using
Ruff doesn't change the quote if it would lead to more escapes. Ruff preserves the double quotes for |
if 'id' not in data:
warnings.warn("Data is missing id field.") |
@mikeckennedy - Just trying to gather data: do you use single quotes for docstrings and multi-line strings? E.g.: def function(x):
'''Some docstring.''' Or only for "inline" strings, like |
Personally I use triple double quotes for docstrings, and single quotes (including triple single quotes) for most other strings except if the string contains single quotes (and no double quotes) then I use double quotes. But I might be fine with a preference for single quotes everywhere (so also for docstrings). |
Our current plan is not to implement this setting as it exists in Black, and first see how far folks can get with our quote-style configuration (see: #7615). If there's still strong demand, we'll of course revisit this flag (and so we'll leave this issue open). Another potential outcome here is that we add something like |
@charliermarsh Exciting to hear, thanks. For me, I think it's always |
+1 for |
Chiming in as i test the new formatter (awesome work you're doing, btw!): I'm testing If you ask me, i prefer to just normalize strings in all my projects and move on... but on the other hand we have cases like this project i'm working on with a team that doesn't want to bother with this. I am a fan of opinionated tools so i'm not really asking that you implement this, but i can see why it's useful for migrating between tools ( |
Thanks @MartinCura -- totally understand, this kind of feedback is really valuable for us as we try to make a decision here :) |
I usually use For triple quoted docstrings, whatever makes the docstring linter happy. For other triple quoted docstrings, I prefer single quotes because it's less noisy and more obvious what I'm looking at, but it's not important to me. EDIT: Oh yeah, and I use |
One consideration with supporting the option is (we could look at black) what the expected behavior is when using the preview formatting that automatically joins and splits strings to make them fit. We then face the situation that we need to join multiple strings, where each string might use different quotes. In which case at least some sort of normalization becomes necessary. |
FWIW those long strings usually fall under the user-facing-English-content category for me, which I associate with double quotes. |
small unnecessary update: normalized strings in my repo when no one was looking 👀 😅 |
As someone converting some codebases to ruff, I ran into this immediately as we are using skip-string-normalization with black. I personally do not see the need to normalize what quote-style is used by folks and would like the option for a |
I inherited some 20y old code, where |
Adding comment for more data points :) . We also ran into this issue when trying to replace Black with ruff formatter in our 10yr old big’ish Django code base with |
Re-reading the comments in this thread, it doesn't seem like there's been a final decision on what the best approach is. I've opened a PR which adds "preserve" as a quote-style option, which is what we would want in order to adopt ruff format for a codebase which uses |
I have based this commit on: - ruff-pre-commit README.md | Using Ruff with pre-commit https://github.com/astral-sh/ruff-pre-commit/blob/main/README.md - The Ruff Formatter | Conflicting lint rules https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules Support for the ruff formatter was added in pytest-ruff by commits from October 2023, released the same day as versions 0.2 and 0.2.1. Hence, it makes sense to require pytest-ruff ≥ 0.2.1 now that we use the ruff formatter. I cannot find in ruff the equivalent for `skip-string-normalization` in black. This is currently an open issue in ruff: astral-sh/ruff#7305 astral-sh/ruff#7525 I have settled on single quotes, for now.
I have based this commit on: - ruff-pre-commit README.md | Using Ruff with pre-commit https://github.com/astral-sh/ruff-pre-commit/blob/main/README.md - The Ruff Formatter | Conflicting lint rules https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules Support for the ruff formatter was added in pytest-ruff by commits from October 2023, released the same day as versions 0.2 and 0.2.1. Hence, it makes sense to require pytest-ruff ≥ 0.2.1 now that we use the ruff formatter. I cannot find in ruff the equivalent for `skip-string-normalization` in black. This is currently an open issue in ruff: astral-sh/ruff#7305 astral-sh/ruff#7525 astral-sh/ruff#8822 I have settled on single quotes, for now.
I have based this commit on: - ruff-pre-commit README.md | Using Ruff with pre-commit https://github.com/astral-sh/ruff-pre-commit/blob/main/README.md - The Ruff Formatter | Conflicting lint rules https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules Support for the ruff formatter was added in pytest-ruff by commits from October 2023, released the same day as versions 0.2 and 0.2.1. Hence, it makes sense to require pytest-ruff ≥ 0.2.1 now that we use the ruff formatter. I cannot find in ruff the equivalent for `skip-string-normalization` in black. This is currently an open issue, in progress: astral-sh/ruff#7305 astral-sh/ruff#7525 astral-sh/ruff#8822 I have settled on single quotes, for now.
FWIW I support adding a |
Could people who want the |
I ran this against our large codebase at $work and the two differences from black that I see are:
This isn't what I want (would prefer for it to just not touch the string quoting ever), but not the end of the world. Hopefully the observations are helpful since the feature is still in preview. |
@mmerickel - Thank you, it's super helpful to get feedback like this. |
Hey folks. I see that Black added --skip-string-normalization ( psf/black#118 (comment) ) but
ruff format --skip-string-normalization
leads toerror: unexpected argument '--skip-string-normalization' found
. Any chance of adding this feature to ruff format?Alternatively, preferably, have ruff potentially prefer
''
rather than""
for strings as an option. That is, still normalize strings, but to''
if specified.I saw two other issues include this text, but neither seemed to be related to this actual functionality. Sorry if it's already addressed.
The text was updated successfully, but these errors were encountered: