![見出し画像](https://assets.st-note.com/production/uploads/images/48451350/rectangle_large_type_2_d13f9034e1c9a4037a2bd00d7825ec3a.jpeg?width=1200)
Photo by
yokoito_illust
PEP8準拠の検査
今日は、『Head First Python』の4章 “コードを再利用する” で学んだことをアウトプットします。
PEP8準拠を調べる準備
プログラムのテストが簡単にできるテストフレームワークの一つにpytestがあります。様々なテストにも使え、プラグインを追加して機能を拡張することもできます。
pytestに追加できるプラグインの1つがpep8です。pep8は、pytestテストフレームワークを使ってコードがPEP8指針に違反していないかを調べます。
以下のコード(vsearch.py)がPEP8指針に準拠しているかを調べることにします。
def search4vowels(phrase:str) -> set:
"""指定された単語内の母音を返す"""
vowels = set('aeiou')
return vowels.intersection(set(phrase))
def search4letters(phrase:str, letters:str='aeiou') -> set:
"""phrase内のletters集合を返す。"""
return set(letters).intersection(set(phrase))
pytestとpep8プラグインのインストール
pipを使ってpytestテストフレームワークとpep8プラグインをインストールします。
$ sudo python3 -m pip install pytest
$ sudo python3 -m pip install pytest-pep8
PEP8にどのくらい準拠しているか?
pytestのインストールでpy.testという新しいプログラムがインストールされています。このプログラムを実行し、vsearch.pyがPEP8に準拠しているかを調べることにします。
vsearch.pyファイルがあるフォルダと同じフォルダにいることを確認し、次のコマンドを入力します。
py.test --pep8 vsearch.py
すると…
あらら、エラーが発生。
toshiya@toshiya-Inspiron-5523:~/mymodules$ py.test --pep8 vsearch.py
/usr/local/lib/python3.9/site-packages/pep8.py:110: FutureWarning: Possible nested set at position 1
EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
============================= test session starts ==============================
platform linux -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/toshiya/mymodules
plugins: pep8-1.0.6
collected 0 items / 1 error
==================================== ERRORS ====================================
________________________ ERROR collecting test session _________________________
Direct construction of Pep8Item has been deprecated, please use Pep8Item.from_parent.
See https://docs.pytest.org/en/stable/deprecations.html#node-construction-changed-to-node-from-parent for more details.
=========================== short test summary info ============================
ERROR
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.24s ===============================
検索してみると、同様のエラーを経験された方が、Quitaにて解決方法を投稿されておられたので、参考にさせて頂きました。
O'Reilly MediaのEratta for Head First Pythonには、原著者Paul氏による記述として、以下の解決方法が記載されています。「pycodestyleモジュールをインストールし、py.testコマンドを--pycodestyleオプション付きで実行する」という解決方法ですね。
pycodestyleモジュールは、以下のようにインストールします。
sudo -H python3 -m install pytest-pycodestyle
そして、py.testコマンドを--pycodestyleオプション付きで実行
$ pytest --pycodestyle vsearch.py
違反メッセージを理解する
==================================== test session starts =====================================
platform linux -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/toshiya/mymodules
plugins: pycodestyle-2.2.0, pep8-1.0.6
collected 1 item
vsearch.py F [100%]
========================================== FAILURES ==========================================
_____________________________________ pycodestyle-check ______________________________________
/home/toshiya/mymodules/vsearch.py:1:25: E231 missing whitespace after ':'
/home/toshiya/mymodules/vsearch.py:7:26: E231 missing whitespace after ':'
/home/toshiya/mymodules/vsearch.py:7:39: E231 missing whitespace after ':'
/home/toshiya/mymodules/vsearch.py:7:43: E252 missing whitespace around parameter equals
/home/toshiya/mymodules/vsearch.py:7:44: E252 missing whitespace around parameter equals
/home/toshiya/mymodules/vsearch.py:10:1: W293 blank line contains whitespace
================================== short test summary info ===================================
FAILED vsearch.py::PYCODESTYLE
===================================== 1 failed in 0.10s ======================================
1〜3番目の問題は、コロンの後の空白がないという指摘です。1番目の指摘は1行目、2,3番目の指摘は7行目の問題を指摘しています。
4,5番目の問題は、イコール(「=」)の前後に空白がないという指摘です。
最後の6番目は、空白行の問題と思われます。
修正が完了したら、コードを再度テストします。
==================================== test session starts =====================================
platform linux -- Python 3.9.2, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/toshiya/mymodules
plugins: pycodestyle-2.2.0, pep8-1.0.6
collected 1 item
vsearch.py . [100%]
===================================== 1 passed in 0.05s ======================================
toshiya@toshiya-Inspiron-5523:~/mymodules$
緑色の表示となり、このコードにPEP8違反はないようですね。
いいなと思ったら応援しよう!
![在野研究者トシヤ](https://assets.st-note.com/production/uploads/images/68199001/profile_1b4bacf8d5918b8f4e27ae33c1707504.jpg?width=600&crop=1:1,smart)