-
Notifications
You must be signed in to change notification settings - Fork 886
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
Settings for forcing/prefering single/double quotes for strings #399
Comments
As a useful reference, I use this tool for exactly this: https://github.com/myint/unify |
I'm always preferring single quote while writing JavaScript, and eslint works well with it. |
YAPF doesn't seem to replace triple single quotes with triple double quotes, even though PEP 8 specifies triple double quotes: https://www.python.org/dev/peps/pep-0008/#string-quotes |
Is this feature on the roadmap at all? This really seems like a huge source of inconsistency. Even google internally forces quote symbol rules. |
Would be really useful. Any progress on this? |
Hello! I would like to communicate that this is also relevant to my interests! Please implement this! |
Thanks for opening this thread. I'm also looking forward to this being implemented. |
Bump |
This is one aspect in which black is superior to yapf. From the relevant portion of the docs:
It goes further:
|
Yapf has a philosophy of not changing the source at all, except for whitespace. You could always format your code with PS: |
Well, to be fair that's not at all a bad idea. |
@kamahen How are these two statements in concord? Does the README need to be changed? Anyway, I normally use |
From my experiences, black is superior. But I really really love 2-space indentation and black for-the-life-of-me refuse to include that option. |
@fanninpm "How are these two statements in concord? Does the README need to be changed?" No. (I would love it if yapf had some options for doing these things; but I don't have time to do any of them, and Emacs will reformat comments for me.) Black is fine as long as you don't want any options. I've used pretty-printers like that in the past (Plus), and there are worse things (the Plus pretty-printer was great because my input was on punched cards, so minor edits to the code were much easier if I didn't have to worry about reformatting). I think (but can't remember for sure) that someone wrote a reformatter for BNR Protel; I do remember that we all agreed on a standard format and henceforth there was no religious arguing over code layout standards. |
We don't allow changes to the token stream. So we don't add, remove, or modify the tokens. This is why we don't allow adding a \-newline or rearranging imports, etc.
I've considered adding a feature where people could run their own transforms over the code. Similar to how lib2to3 does it with "fixes". (In fact, it would be exactly like that, because that's what we'll use.) That will allow them to use these transforms, but the onus is on the programmer to ensure it's safe to perform the transformations. |
lib2to3 is going away; don't know if any of the potential successors will have a transformation library. All I've seen so far is that there are a couple of potential successors for parsing. See https://bugs.python.org/issue40360: lib2to3 is now deprecated and may not be able to fully parse Python 3.10+. CPython encourages using https://libcst.readthedocs.io/ or https://parso.readthedocs.io/ https://github.com/pyga/awpa LibCST builds on parso. Comparing stars on GitHub: https://github.com/PyCQA/redbaron : 457 https://bugs.python.org/issue40360#msg367726 and https://bugs.python.org/issue40360#msg367730 |
See if 521d617 helps you with the quotes issue. |
When will this be merged into master and released? |
Yeah, what's the state of this? This commit doesn't seem to be in
It is a bad idea: Editors can usually call black or yapf for formatting, but not both... |
I beg to differ. I believe it's not impossible to set up a sequence of formatters with specific orders -- I know for a fact Emacs can do this in a breeze. |
This is a well-intentioned philosophy, but YAPF would be a far more useful tool if the philosophy was for this to merely be the default behaviour. I've seen hundreds of hours wasted by devs not being able to find code because of these things, and the resulting formatting arguments. The more that can be enforced by code, saving devs from having to worry about it, the better, IMHO. |
There just needs to be a simple script that follows these rules:
def func():
"""
I want my docstrings to look like this
"""
...
import ubelt
text = ubelt.codeblock(
'''
This text is meant to be used problematically, so distinguish it from documentation
''')
import ubelt
text = ubelt.codeblock(
"""
I know what I'm doing here. Please don't escape my single quotes. It would make this ugly!
""")
def foo(): Anyway, if anyone writes that tool let me know. I'll just run it after yapf. I really don't care if it is a part of it. I just want a configurable quote normalizing script. If someone writes it I can put it on pypi if they need help with that part. (I might write it if nobody else does, I imagine you can at least get 90% of the way there with a Concrete Syntax Tree, or a tool like redbaron, or even ast and some hueristics) /rant EDIT: I did write it myself: https://gist.github.com/Erotemic/75618ea970a2af49cd43db1bf61a8ac6 It's not the exact spec I wroteup, but its close, and it got the job done. |
Instead of Anyway, you can run |
@kamahen because I don't want to have to write |
@kamahen: Does your comment have anything to do with quotation marks? |
@mscheper - My comment has to do with transformation libraries - and changing between single and double quotes would be an example of a transformation. As noted elsewhere, yapf only changes whitespace and leaves everything else alone. Other tools, such as Any tool that depends on lib2to3 (both yapf and black currently depend on it) is subject to breakage with future changes to Python syntax. (Already we've seen problems with the "walrus" operator, for example) |
Please, just implement this, and make the default adhere to your philosophy. This way we can have amazing Yapf, but with double quotes. The way I see it:
So can we ever have a formatter do exactly what we want? |
It seems to me that this is a fair bit of work (which nobody seems to be volunteering to do) and there's a simple work-around: use |
I prefer to use single quotes, and you can use Eslint to configure whether to use single or double quotes by default during front-end development. But there doesn't seem to be a python formatter that can do that yet. |
Just a setting so quoting style can be unified across a codebase.
e.g.
for the three strings
abc
,a'c
,a"c
prefer_single_quotes
->'abc'
,"a'c"
,'a"c'
prefer_double_quotes
->"abc"
,"a'c"
,'a"c'
force_single_quotes
->'abc'
,'a\'c'
,'a"c'
force_double_quotes
->"abc"
,"a'c"
,"a\"c"
The text was updated successfully, but these errors were encountered: