16

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?

pablomolnar
  • 528
  • 6
  • 14
  • Out of curiosity, why would you want to do that? Why do you think this is a good idea? – Reblochon Masque Apr 08 '16 at 01:47
  • 3
    @ReblochonMasque A lot of style guides have a preferred quotation style. Consistency helps keeps diffs clean, and arguably just looks nicer. – Chris Martin Apr 08 '16 at 01:52
  • 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. – Reblochon Masque Apr 08 '16 at 02:28
  • @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. https://docs.saltstack.com/en/latest/topics/development/conventions/style.html#strings). – pablomolnar Apr 08 '16 at 21:10

2 Answers2

22

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
edaniszewski
  • 221
  • 2
  • 4
3

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

silvioprog
  • 580
  • 1
  • 8
  • 18
The Compiler
  • 11,126
  • 4
  • 40
  • 54