現状
Head First Python 第2版 189ページ~193ページの「回り道」部分は、現在では書籍記載の手順をそのまま実行することができません。
py.test --pep8
を実行する時点で、以下のようなエラーが発生するためです。
C:\NewBook\ch04> py.test --pep8 vsearch.py
c:\python39\lib\site-packages\pep8.py:110: FutureWarning: Possible nested set at position 1
EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
============================================================ test session starts =============================================================
...略...
=================================================================== 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.12s ==============================================================
原因
Pytest 6.0 にて適用された、[Node Construction changed to Node.from_parent](https://docs.pytest.org/en/stable /deprecations.html#node-construction-changed-to-node-from-parent)という変更が原因のようです。
私の環境では、インストールされているpytest
モジュールのバージョンは6.2.2でした。当該変更が適用されているバージョンですね。
import pytest
print(pytest.__version__)
# => 6.2.2
解決方法
O'Reilly MediaのEratta for Head First Pythonには、原著者Paul氏による記述として、以下の解決方法が記載されています。「pycodestyle
モジュールをインストールし、py.test
コマンドを--pycodestyle
オプション付きで実行する」という解決方法ですね。
Here's some notes from another reader on this issue:
"...However, after going through the internet looking for answers I found a solution that produced the same output > as the one in your book! A forum user posted that the pep8 part of pytest is being renamed to pycodestyle, so when I type
sudo -H python3 -m install pytest-pycodestyle
it installs the module to check for pep8 compliance, and then call it by
pytest —pycodestyle *.py
I’m not sure if it is a separate module all together, or it actually is the new name for the pep8 module, but pycodestyle seems to give the same terminal output as pep8 would... "
Note that the published book targets Python 3.5 and that time marches on. 3.9 is just around the corner and pytest 6 recently appeared. Some things change and stuff in the book breaks. This is hard to avoid.
--Paul.
sudo
以下のコマンドはUNIX系環境の場合のコマンドなので、Windows環境においてpycodestyle
モジュールをインストールするコマンドは以下となります。
py -3 -m pip install pytest-pycodestyle
pycodestyle
モジュールのインストールが完了したら、py.test
コマンドを--pycodestyle
オプション付きで実行します。すると、エラーは発生しなくなり、本誌189ページに近い出力が得られます。
C:\NewBook\ch04> py.test --pycodestyle .\vsearch.py
c:\python39\lib\site-packages\pep8.py:110: FutureWarning: Possible nested set at position 1
EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
============================================================ test session starts =============================================================
platform win32 -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: C:\NewBook\ch04
plugins: pep8-1.0.6, pycodestyle-2.2.0
collected 1 item
vsearch.py F [100%]
================================================================== FAILURES ==================================================================
_____________________________________________________________ pycodestyle-check ______________________________________________________________
C:\NewBook\ch04\vsearch.py:1:25: E231 missing whitespace after ':'
C:\NewBook\ch04\vsearch.py:7:26: E231 missing whitespace after ':'
C:\NewBook\ch04\vsearch.py:7:39: E231 missing whitespace after ':'
C:\NewBook\ch04\vsearch.py:7:43: E252 missing whitespace around parameter equals
C:\NewBook\ch04\vsearch.py:7:44: E252 missing whitespace around parameter equals
========================================================== short test summary info ===========================================================
FAILED vsearch.py::PYCODESTYLE
============================================================= 1 failed in 0.07s ==============================================================
あとは、本誌189ページ以降の学習を進めていくのです。