LoginSignup
52
40

More than 3 years have passed since last update.

blackとpylintを使った快適なPython開発

Last updated at Posted at 2019-05-31

はじめに

Pythonでチーム開発を快適にする以下のツールをご紹介します。

さらに、上記ツールをPyCharmIntelliJ IDEAで連携する方法をご紹介します。
※ 本記事のキャプチャは執筆環境の都合で IntelliJ IDEA を使っています

本記事では触れませんが、blackpylintの両方に対応しているエディタ/IDEは他にもあります。

  • Emacs
  • Vim
  • Visual Studio Code

できるようになること

ファイルを保存したとき、フォーマッターと静的コード解析が自動でかかるようになります。
2019-05-31_09h53_32.gif

確認した環境

Windows10
:warning: MacやLinuxの場合はパス表記などが異なる可能性があります

ツールやIDEのバージョンは以下の通りです。

バージョン
black 19.3b0
pylint 2.3.1
IntelliJ IDEA 2019.1.3
IntelliJ IDEA の Pythonプラグイン v2019.1.191.7479.19
IntelliJ IDEAFile Watchers プラグイン v.191.6183.20

black

blackとは

Pythonのフォーマッターです。
ソースコードに実行すると、決められたルールに従いフォーマットがかかります。

⚠️ blackは2019-05-30現在β版のため、多少挙動が変わる可能性があります (大きな仕様変更の予定はないとのこと)

メリット

以下のメリットがあると感じています。

  • フォーマットが同じであるため、他人のコードが読みやすくなりレビュー速度が上がる
  • フォーマットを整えるために使うリソースを開発に充てることができる
  • どのエディタを使っても同じフォーマットになる

設定

blackの設定はpyproject.tomlに記載できます。
以下は一例です。

pyproject.toml
[tool.black]
line-length = 100
target-version = ['py36', 'py37']
include = '\.pyi?$'
exclude = '''
/(
    \.eggs
  | \.git
  | \.hg
  | \.mypy_cache
  | \.tox
  | \.venv
  | _build
  | buck-out
  | build
  | dist
)/
'''

設定できる項目

コマンドライン引数で設定できる項目はpyproject.tomlで上書きできます。

コマンドライン引数の一覧はblack --helpで表示させることができます。

$ black --help
Usage: black [OPTIONS] [SRC]...

  The uncompromising code formatter.

Options:
  -l, --line-length INTEGER       How many characters per line to allow.
                                  [default: 88]
  -t, --target-version [py27|py33|py34|py35|py36|py37|py38]
                                  Python versions that should be supported by
                                  Black's output. [default: per-file auto-
                                  detection]
  --py36                          Allow using Python 3.6-only syntax on all
.
(以下略)

pylint

pylintとは

エラーの発見やコード品質を向上させてくれる静的コードチェッカーです。

メリット

以下のメリットがあると感じています。

  • どのエディタを使っても同じ解析ができる
  • 不要な解析は無効化できる (ファイル単位、行単位も可)

設定

設定方法はいくつかあり、http://pylint.pycqa.org/en/latest/user_guide/run.html#command-line-options で紹介されています。

たとえば、プロジェクトで設定ファイルを管理する場合は.pylintcファイルを作成します。
雛形作成用のコマンドを使います。

$ pylint --generate-rcfile > .pylintrc

解析項目の無効化

不要な解析項目は無効にできます。

無効範囲の指定

http://pylint.pycqa.org/en/latest/user_guide/message-control.html に紹介されています。
たとえば、以下のように書くとその行だけimportポジションが間違っている警告を無視できます。

import os

a = "hoge"

import something # pylint: disable=wrong-import-position
無効できる項目の一覧

項目一覧は pylint --list-msgs で確認できます。

$ pylint --list-msgs
:blacklisted-name (C0102): *Black listed name "%s"*
  Used when the name is listed in the black list (unauthorized names).
:invalid-name (C0103): *%s name "%s" doesn't conform to %s*
  Used when the name doesn't conform to naming rules associated to its type
  (constant, variable, class...).
:missing-docstring (C0111): *Missing %s docstring*
  Used when a module, function, class or method has no docstring.Some special
  methods like __init__ doesn't necessary require a docstring.
.
.

全部で1000行くらい表示されます。

blackpylintPyCharmIntelliJ IDEAで使う

blackpylintPyCharmIntelliJ IDEAと連携させる方法をご紹介します。

開発に集中できるよう、ファイル保存時にblackpylintを自動実行できるようにします。

File Watchersプラグインのインストール

ファイル保存時に処理を実行させるためには、公式の提供しているFile Watchersプラグインが必要です。
IntelliJ IDEAにインストールしてください。

blackの設定

Settings > Tools > File Watchers から <Custom> で新規作成します。

image.png

以下の様に設定します。

項目
File type Python
Scope Current File
Program $PyInterpreterDirectory$\black
Arguments $FilePath$

またAdvanced Optionsの以下チェックは外しています。

  • Auto-save edited files to trigger the watcher
  • Trigger the watcher on external changes

IntelliJ IDEAで現在編集中のファイルが保存されたときだけblackを実行したいからです。

全て設定すると以下のようになります。

image.png

Pythonファイルを編集/保存してみましょう。
自動でblackがコードをフォーマットしてくれるはずです :smile:

:book: 【参考】https://black.readthedocs.io/en/stable/editor_integration.html

pylintの設定

blackのときと同様、Settings > Tools > File Watchers から <Custom> で新規作成します。

image.png

以下の様に設定します。

項目
File type Python
Scope Current File
Program $PyInterpreterDirectory$\pylint
Arguments --rcfile $ContentRoot$/.pylintrc $FilePath$

blackと同様、Advanced Optionsの以下チェックは外しています。

  • Auto-save edited files to trigger the watcher
  • Trigger the watcher on external changes

全て設定すると以下のようになります。

image.png

Pythonファイルを編集/保存してみましょう。
自動でpylintが実行され、問題がある場合だけ画面下部にViewが表示されると思います :smile:

:book: 【参考】http://pylint.pycqa.org/en/latest/user_guide/ide-integration.html#pylint-in-pycharm

終わりに

blackpylintを使うことで、エディタやIDEによらずフォーマットやコードチェックできる方法をご紹介しました。

保存時に自動実行するため、実行を忘れる心配もありません。
チームでPython開発を行う場合は、ぜひ試してみていただければと思います :smile:

52
40
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
52
40