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

に公開
2

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

  1. Press F1 to display the command palette.
  2. Type >Preferences:Open Settings (JSON) in the search bar.
  3. 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.

GitHubで編集を提案

Discussion

manabianmanabian

本記事をきっかけとして black によりシングルクォートを変換しない方法を知ることができ助かったのですが、 skip-numeric-underscore-normalization ではなく、 skip-string-normalization ではないでしょうか?

ドキュメントにて下記のように記載されております。

If you are adopting Black in a large project with pre-existing string conventions (like the popular “single quotes for data, double quotes for human-readable strings”), you can pass --skip-string-normalization on the command line. This is meant as an adoption helper, avoid using this for new projects.

引用元: The Black code style - Black 22.3.0 documentation

ReluRelu

私の環境(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を指定するのが妥当だと思いますので、提案いただきました内容で記事の修正をさせていただきました。

情報提供ありがとうございました。