※当サイトの記事には、広告・プロモーションが含まれます。

VS CodeのPythonによる開発環境で静的解析などはRuffを利用しておけば良いのか

nazology.kusuguru.co.jp

⇧ まぁ、「恋愛」に限らず、「マッチング」はお互いが何を求めているかを確認する手段であるからして、結局のところ、利用者の使い方次第という気がしますかね...

Ruffとは

公式のドキュメントによりますと、

github.com

An extremely fast Python linter and code formatter, written in Rust.

https://github.com/astral-sh/ruff

Ruff aims to be orders of magnitude faster than alternative tools while integrating more functionality behind a single, common interface.

https://github.com/astral-sh/ruff

Ruff can be used to replace Flake8 (plus dozens of plugins), Blackisortpydocstylepyupgradeautoflake, and more, all while executing tens or hundreds of times faster than any individual tool.

https://github.com/astral-sh/ruff

⇧ とありますと。

VS CodeVisual Studio Code)」を利用している場合、

marketplace.visualstudio.com

⇧ 上記の要件を満たしていれば、「拡張機能」をインストールするだけで「Ruff」の機能を利用できるみたい。

今時のシステムのデフォルトのPythonは、3.9とかになってると思うからして、余程、古い「OS(Operation System)」を利用していないなら、要件は満たせるかと。

そして、「VS CodeVisual Studio Code)」の「拡張機能」の「Ruff」を導入する場合は、「pip install ruff」が不要。

VS CodePythonによる開発環境で静的解析などはRuffを利用しておけば良いのか

とりあえず、

zenn.dev

zenn.dev

⇧ 上記サイト様にありますように、

  1. 拡張機能「Ruff」をインストール
  2. プロジェクト直下に、.vscode/settings.json
  3. プロジェクト直下に、pyproject.toml

の3点を整えれば良いみたい。

勿論、

⇧「拡張機能」の「Python」はインストールしているわけですが。

何はともあれ、「VS CodeVisual Studio Code)」を起動し、「拡張機能」で「Ruff」をインストールします。

そしたらば、「[プロジェクトルート]/.vscode/settings.json」に設定を追加します。

■/home/ts0818/work/app/python/app/.vscode/settings.json

{
    "[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.fixAll": "explicit",
          "source.organizeImports": "explicit"
        },
        "editor.defaultFormatter": "charliermarsh.ruff"
    }
    ,"python.testing.unittestEnabled": false
    , "python.testing.pytestEnabled": true
    , "python.testing.pytestArgs": [
        "-c",
        "${workspaceFolder}/src/test/resources/pytest/conf/pytest.ini"
    ]
    , "files.exclude": {
        "**/__pycache__": true
    }
}    

⇧ のような感じ。

最後に、「[プロジェクトルート]/pyproject.toml」を作成し、設定します。

とりあえず、参考サイト様の設定をほぼそのまま流用させていただく。

■/home/ts0818/work/app/python/app/pyproject.toml

[tool.ruff]
line-length = 120

[tool.ruff.format]
docstring-code-format = true

[tool.ruff.lint]
select = ["ALL"]
ignore = [
    "D1",    # undocumented
    "D203",  # one blank line before class
    "D213",  # multi-line summary second line
    "TD001", # invalid todo tag
    "TD002", # missing todo author
    "TD003", # missing todo link
    "PD011", # pandas use of dot values
]
unfixable = [
    "F401", # unused import
    "F841", # unused variable
]

logger-objects = ["src.library.logger.LOGGER"]

[tool.ruff.lint.pylint]
max-args = 6

⇧ で、保存して、「.py」ファイルを確認してみると、警告の雨霰が出るので「Ruff」が機能しているということらしい。

⇧ なるほど、ロジックにのみ集中できるようになれるのはありがたい気がしますな。

ただ、対応が難しそうな問題については、「pyproject.toml」の「[tool.ruff.lint]」の「ignore」に追加していけば良いかと。

カーソルを持っていけば、「Ruff(識別番号)」が表示されるので、「識別番号」を「ignore」に追加していけば良いと。

とりあえず、「Python」の標準ライブラリとかであれば、代替案とかも提案してくれる感じ。

例えば、

■Not Good

import os

os.sep.join(["src", "main"])

■Good

from pathlib import Path

Path("src") / "main"

⇧ のような感じで、基本的には、「pathlib」を推奨してくるなど。

docs.python.org

まぁ、一般的な「Python」のコーディング規約が分からんので、

  • インデント
  • フォーマット整形

など、煩わしい部分はツールに任せてしまって、処理に集中させてもらうことにしますか。

「型」を自動で解決してくれたら言うことないんですけどね...

流石に外部ライブラリとかの「型」については、良しなに面倒を見てくれるわけでは無いと思われる。

欲を言えば、

などのように、インストールした段階で環境構築が済んだ状態になってくれているのが理想なんですが、「VS CodeVisual Studio Code)」は、そのあたりが微妙なんですよね...

環境構築が、秘伝のタレ化してしまう感じ...

毎度モヤモヤ感が半端ない…

今回はこのへんで。