Movatterモバイル変換


[0]ホーム

URL:


LoginSignup
2

Go to list of users who liked

2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

flake8のプラグインをいろいろ試してみた

Posted at

こんにちは、未来電子テクノロジーでインターンをしている者です。
今回は、flake8で少し遊んでみようと思います。

flake8のインストール

flake8とは、pythonのコードチェッカーツールです。
flake8をインストールする前に、pythonをインストールしておきます。
その後、pipを使ってflake8をインストールします。

command
$pipinstallflake8

flake8をデフォルトで使ってみる

インストールしたら、まずはデフォルトの状態で使ってみたいと思います。
そのためのテストコードを以下のように書きます。

test.py
importsysimportmatha=int(input())b=2defcalc(a,b):returna+bc=calc(a,b)print('a+b='+str(c))

テストコードが書けたら、一度実行してみます。

command
$python test.py4 ←aの値の入力a+b=6

正常に出力されました。
しかし、このコードに対して、flake8を用いると、コードの書き方についてさまざまな警告がなされます。

command
$flake8 test.pytest.py:1:1: F401'sys' imported but unusedtest.py:2:1: F401'math' imported but unusedtest.py:4:2: E225 missing whitespace around operatortest.py:5:4: E222 multiple spaces after operatortest.py:7:1: E302 expected 2 blank lines, found 1test.py:8:13: E225 missing whitespace around operatortest.py:10:1: E305 expected 2 blank lines after class orfunctiondefinition, found 1test.py:10:11: E231 missing whitespace after','test.py:12:13: E225 missing whitespace around operator

警告の内容としては、以下の通りです。
F401:モジュールをインポートしているが、使っていない
E222:スペースが二つ以上連続している
E225:演算子(+、=など)の両側をスペースで空けていない
E231:カンマの後にスペースがない
E302,305:関数やクラスの前後は2行空ける

プラグインを追加する

では、flake8のプラグインを追加していきます。
プラグインの種類は、pipを使って調べられます。

command
$pip search flake8flake8-quotes(2.1.0)                   - Flake8 lintforquotes.flake8-setuptools(0.0.0)               - Flake8 integration with Setuptoolsflake8-expandtab(0.3)                  - flake8fortab junkiesflake8-chart(0.1.5)                    - flake8 stats visualised...

flake8-import-order

まずは、flake8-import-orderを試してみます。
flake8-import-orderは、モジュールがアルファベット順にインポートされているかをチェックします。
では早速インストールして使ってみます。

command
$pipinstallflake8-import-orderCollecting flake8-import-order...Successfully installed flake8-import-order-0.18.1$flake8 test.pytest.py:1:1: F401'sys' imported but unusedtest.py:2:1: I100 Import statements areinthe wrong order.'import math' should be before'import sys'test.py:2:1: F401'math' imported but unused...

すると、I100という警告が追加されました。
これは、mathの方がsysよりもアルファベット順で先だから、mathを先にインポートするべきだという警告です。

flake8-docstrings

次に、flake8-docstringsを試してみます。

command
$pipinstallflake8-docstringsCollecting flake8-docstrings...Successfully installed flake8-docstrings-1.4.0 pydocstyle-4.0.1 snowballstemmer-1.9.1$flake8 test.pytest.py:1:1: D100 Missing docstringinpublic module...test.py:7:1: D103 Missing docstringinpublicfunction...

すると、D100、D103という警告が追加されました。
これらは、それぞれ公開モジュール、関数の説明をdocstrings("""...""")を用いてしなさいという警告です。
試しに、calc関数を説明してみます。

test.py
...defcalc(a,b):"""Calculate a+b."""returna+b...

そして、もう一度flake8を実行してみると、

command
$flake8 test.py...test.py:5:4: E222 multiple spaces after operatortest.py:7:1: E302 expected 2 blank lines, found 1test.py:9:13: E225 missing whitespace around operator...

7行目のD103が消えました。

まとめ

今回は、flake8でいろいろなプラグインを試してみました。
他にもさまざまなプラグインがありますので、また試してみたいと思います。
記事の中に間違いがありましたら、修正しますので、ご指摘のほどよろしくお願いいたします。

2

Go to list of users who liked

2
0

Go to list of comments

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
2

Go to list of users who liked

2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?


[8]ページ先頭

©2009-2025 Movatter.jp