iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😈
How to prevent Black from automatically replacing single quotes with double quotes in VS Code
Many of you writing Python probably use black. I have automatic formatting enabled on file save in VS Code, but during this process, black's rules automatically replace single quotes with double quotes. For those who prefer single quotes, this is a bit troublesome. It results in source code where double quotes and single quotes are mixed until the next time the file is saved during editing, which feels a bit off. So, this is about configuring it so they aren't replaced automatically.
Setup Steps
- Press
F1to display the command palette. - Type
>Preferences:Open Settings (JSON)in the search bar. - Add the following to settings.json and save:
"python.formatting.blackArgs": ["skip-string-normalization"]
I understand the sentiment that strings should be double quotes, as they are easier to type. However, when in Rome, do as the Romans do.
Discussion
本記事をきっかけとして black によりシングルクォートを変換しない方法を知ることができ助かったのですが、
skip-numeric-underscore-normalizationではなく、skip-string-normalizationではないでしょうか?ドキュメントにて下記のように記載されております。
引用元: The Black code style - Black 22.3.0 documentation
私の環境(Python 3.10.4, black 22.3.0)では
skip-numeric-underscore-normalizationでもダブルクォートへの変換が行われなかったので少し調べてみたのですが、Add --single-quote option #594によると、ダブルクォートへの変換が行われないような仕様になっているオプションとして、line-length,skip-string-normalization,skip-numeric-underscore-normalizationが存在しているようで、それぞれのオプションを指定することでダブルクォートへの変換が行われないことも確認できました。ただ、本記事の「シングルクォートを変換しない」ということを目的に考えると、引用していただいた内容から
skip-string-normalizationを指定するのが妥当だと思いますので、提案いただきました内容で記事の修正をさせていただきました。情報提供ありがとうございました。