Force string format in pylint
Asked Answered
F

3

17

Python allows to use either single or double quotes for strings. I'd like to enforce only single quotes format in my projects.

Is there any specific rule in pylint or an existing pylint plugin to achieve that?

Faris answered 8/4, 2016 at 1:45 Comment(4)
Out of curiosity, why would you want to do that? Why do you think this is a good idea?Neonate
@ReblochonMasque A lot of style guides have a preferred quotation style. Consistency helps keeps diffs clean, and arguably just looks nicer.Angora
Thank you Chris Martin, I understand and agree with the reasons you explained; I should have been more specific with my question: I am interested to know why the OP would like to "hard wire" a rule that will amputate the language feature that allows to use the double quotes in the event a string contains single ones. I have in fact been thinking along the same lines, and wanted to gather more opinions.Neonate
@ReblochonMasque, as Chris mentioned it's just another internal coding style convention. I think we all agree that mixing quotes styles when no escaping is necessary it looks "bad". So, enforcing a convention is just one decision less for the developer (specially new hires). Of course the linting rule should be intelligent enough to allow edge cases (quotes inside string value). Many projects do the same thing in the javascript world and looks like in python too (eg. docs.saltstack.com/en/latest/topics/development/conventions/…).Faris
I
23

I recently wrote a pylint plugin for this: https://pypi.python.org/pypi/pylint-quotes

You can get it with

pip install pylint-quotes

Then to use it with pylint,

pylint --load-plugins pylint_quotes <module-or-package>

in the .pylintrc file, you can configure which quotes to use:

# Set the linting for string quotes
string-quote=single
triple-quote=double
docstring-quote=double
Israelite answered 14/6, 2017 at 19:20 Comment(0)
Z
3

I don't know if pylint can currently do this, but there is an extension for flake8 called flake8-quotes that does it.

Zirconium answered 8/4, 2016 at 5:53 Comment(0)
H
1

Fast forward to 2023:

Proposed plugin does not work on Pylint >= 3.0.0.

Workaround: www.pylint.org/inconsistent-quotes/

Adds checker to .pylintrc for quote usage consistency.

Meaning: in same file top level quotes should always be of same type. (double <> single)

Hissing answered 14/11, 2023 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.