3
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 3 years have passed since last update.

Head First Python 第2版における、「既存コードが PEP 8 に準拠しているかを調べる」という学習を今日(2021年2月3日)実行するには

Posted at

現状

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ページ以降の学習を進めていくのです。

3
2
0

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
3
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?