-
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: Docstrings with quote-style = "single"
configuration
#7615
Comments
We also use "single everywhere except docstring". +1 to always use double for docstring (preferably) or make it configurable. |
It looks like Blue also uses double quotes for triple-quoted strings and docstrings, and single quotes everywhere else. |
Some data from Code Search:
So the majority of projects that are using single quotes only apply it to non-docstring, non-multiline strings, but there are some projects that deviate from that pattern. |
@alkatar21 would you expect that What about a single-quoted docstring? Should it use def test():
'Single quoted docstring'
... Using We can
|
I believe the following are all docstrings (used by editors and sphinx for example) and should therefore use class ExampleClass:
"""Class docstring."""
variable1: int
"""Docstring for variable1."""
variable2: int
"""Docstring for variable2."""
def __init__(self, parameter: str) -> None:
self.parameter: str = parameter
"""Docstring for parameter.""" I don't remember ever using non-docstring triple quoted strings and accordingly have no personal opinion about it, but I would suggest to follow PEP8.
I didn't even know that is possible, but off the top of my head I'd say
Then I would suggest to follow PEP8.
I would suggest to follow PEP8 and implement |
@MichaReiser - the other option is to mirror the configuration options that we use for flake8-quotes: a setting for inline quotes, a setting for multiline quotes, and a setting for docstrings (which applies to single- and triple-quoted docstrings). |
I'd prefer only having at most a single quotes setting |
I agree that it would be nice to have a single setting. The data I scraped above demonstrates that we can’t use a single setting and still support all users, but we could make that choice. If we want a single setting, my suggestion would be to enforce double quotes for docstrings and triple-quoted strings, and use the string setting for all others. This would be consistent with Blue and Pyink which always use double quotes for such strings. |
Yeah, I prefer having as few options as possible. Ideally, just one, two if we must. I would try to avoid having all three. The other part that's interesting about this is... Should these options now become global? Or are we fine repeating the configuration twice (I would argue it's fine, because the formatter makes the lint rule obsolete) |
I think having the same settings as in flake8-quotes is not a bad idea. I'm fine with defaults values for multiline and docstrings checking what is most commonly used, but excluding customization on these settings could create the same problem that fueled the need for Black forks (Blue, Pyink, ...) |
Our current plan is to always use double quotes for docstrings and triple-quoted strings (as per PEP 8 and PEP 257). We'll change this an upcoming release, prior to the Beta. If there's still strong demand, we'll then consider adding an additional dedicated setting to use single quotes everywhere (i.e., for docstrings and triple-quoted strings in addition). |
Seems reasonable. Just to add to your data, please keep in mind that there a lot of projects using pre-commit with |
I just tried out
Yes, |
👍 We're going to change this, probably in the upcoming release (or the next one, if it doesn't make it). |
Great to hear @charliermarsh. I'm super excited about being able to switch to I noticed one other formatting deviation from blue related to right-hanging comments, but I'll open a new ticket on that when I get a chance, if it hasn't already been reported. |
…7680) ## Summary At present, `quote-style` is used universally. However, [PEP 8](https://peps.python.org/pep-0008/) and [PEP 257](https://peps.python.org/pep-0257/) suggest that while either single or double quotes are acceptable in general (as long as they're consistent), docstrings and triple-quoted strings should always use double quotes. In our research, the vast majority of Ruff users that enable the `flake8-quotes` rules only enable them for inline strings (i.e., non-triple-quoted strings). Additionally, many Black forks (like Blue and Pyink) use double quotes for docstrings and triple-quoted strings. Our decision for now is to always prefer double quotes for triple-quoted strings (which should include docstrings). Based on feedback, we may consider adding additional options (e.g., a `"preserve"` mode, to avoid changing quotes; or a `"multiline-quote-style"` to override this). Closes #7615. ## Test Plan `cargo test`
Please reconsider this as it's a bit nonsensical as of now since you can do: [tool.ruff.flake8-quotes]
docstring-quotes = 'single'
inline-quotes = 'single'
multiline-quotes = 'single' and then the formatter is just straight up incompatible with the other part of the software. Software definitely shouldn't conflict with itself. You could just split it into 3 variables like above for the most compatible approach. If you need any arguments why use single quotes - they're simply easier to press and that's the advantage. Disadvantages? None for me, I don't mind that let's say 90% of projects go for doubles, no other tool that I'm using has problems with that either. |
Formatter is unrelated to rules, as can be also seen from my comment above. Doing that changed nothing indeed. There's currently no way to disable the quote formatting, pretty sure. EDIT: comment got deleted in case someone is confused. |
FYI, the disadvantage is that you're much more likely to have single quotes inside a string of text than a double quote. Your comment above has one single quote, and mine has seven. Neither of us used a double quote. And easier to press doesn't matter if your formatter converts them to double quotes for you anyway. ;) (That's the reasoning behind Black's choice, by the way) Though I'd also add that single quotes are used by Python's built in repr's. |
I was hoping to covert projects like pypa/black and pyproject-metadata to the Ruff formatter, and wasn't able to due to this - previously we used But anyway, I'd like to adopt ruff-format in these projects without having to ask all our developers about changing styles. Though I could ask, given what PEP says. |
Ahh! Though I notice all the triple quoted strings that were changed were not docstrings. :( |
That's... quite unreasonable I'd say, as it assumes, I don't know why:
You just can't generalize like that, so still no idea why software vendors/authors try to enforce such things TBH, especially when introducing inconsistencies like I mentioned. And the formatters utilize double minimal escaping quoting method anyways, so wrapping with the other when necessary occurs. It really shouldn't matter what's used and what's the user's preference.
True. I could just prefer the more minimalistic look of |
I'm not sure I'd say "most other" - At least Spanish and French both use single quotes, and pretty much don't use double quotes at all! ( Also, many other (computer) languages don't give you the option, and those mostly force double quotes (C, C++, Java, Rust, Matlab, Go, Swift) for strings. Using double quotes in Python is also consistent with those languages, and makes it easier to go back and forth. (note: JavaScript and Fortran are like Python. Ruby and Bash allow both, but double quotes interpolate and single do not. I don't know of a language that forces single quotes.) Anyway, doesn't really matter here, I think here the thing to argue is if you like single quotes, do you like triple quoted single quotes? Due to PEP 257, enforcing double quoted docstrings seems fine, but what about free-standing single triple quotes? That's what we have (hopefully had) in pypa/build. Docstrings used double, everything else (even if triple quoted) was single. I don't like single quotes anyway, so I'm a bad judge. :) |
Yes, exactly. It's not just about the ease of writing single quotes over double quotes (at least for US qwerty keyboards, don't know about others), but about reading code. The extra screen grit of double quotes can reduce the readability of code too. My understanding was that black adopted this style because it was more compatible with JavaScript or JSON or some such, with little ergonomic consideration. |
I don't think any of Asian languges does and that's already the most as there are loads of variations. While French uses them as frequently as English, I don't know where you see them in Spanish - I personally almost never do. They're mostly popular in Germanic languages I'd say.
Why does that matter at all...? Not only does Python allow both, but also the language used by authors is quite irrelevant IMO, while we're talking about a specific tool especially.
This point makes no sense, as I was obviously talking from perspective of someone e.g. writing the librar to parse the JSON.
Why would I want to be consistent with entirely different languages though? Python is normally way way more loose than C, C++ or Java and rather uncomparable. The fact that C doesn't have lambdas doesn't mean I should avoid lambda usage, for instance. The fact that languages differ is an advantage of variety of them, not a disadvantage. You can code a quick bot without type hints and without caring much about formatting with Python and then a business application with 1000 classes with Java that follows very specific standards.
I don't mind them at all ¯_(ツ)_/¯ E.g. for the reason specified by the person above. I think you're way too focused on defending a specific style while not considering the freedom aspect at all. |
I think this discussion shows why Black didn't make it configurable, and just picked a "correct" one. :) Happy to argue about why double quotes are better somewhere else, but it doesn't have anything to do with this issue - Ruff-format already lets you pick single quotes! To summarize: if you set def f():
"""
Docstring.
"""
single = 'hello'
triple = """Hi there""" The docstring is correct, PEP 257 states docstrings should use double quotes. The triple quote string is correct according to PEP 8, which states all triple quoted strings should be made with double quotes. I'm okay with Ruff not providing configurability to contradict a PEP - I'll try to get it updated in pypa/build and pyproject-metadata. The one place where it's left open by formatting PEPs is for simple strings, and Ruff has an option for that, which is nice. Though IMO, even just seeing them above, I'd much rather just make the simple string double quoted too. ;) Having a '"preserve"` option would be fantastic (didn't notice it was missing!), and would help people do whatever they are doing now with Black. Being at least as configurable as Black is a low bar. ;) |
|
I'd also like to be able to configure quote styles separately. flake8-quotes does it very nicely, so why not allow autoformatting to be as flexible:
|
Actually, if you want a single setting: How about a |
We're considering adding the |
Following #1904 (comment) I tried the new configuration
But now docstrings are also formatted with single quotes:
I thought with docstrings there is no real discussion that they always use
"""
and it only refers to normal strings and PEP 8 also says so. Like the following example:There was a short discussion on Discord already where @charliermarsh mentioned the following:
For me it would be important that docstrings always use
"""
regardless ofquote-style
. I am not quite sure if this is to be understood as a feature request or rather as a bug. D300 considers this as a bug as well.The text was updated successfully, but these errors were encountered: