pep8 をバージョンアップしたら test_pep8.py が動かなくなった(ので直した)

今年の3月に入ってから急に更新が激しくなった pep8 パッケージですが、
pep8-1.3 になって内部の実装が大きく変わりました。
Changelog にも

Warning

The internal API is backwards incompatible.

なんてありがたい言葉が書かれています。

さて、API が変わって影響を受けるものといえば、しみずかわ先生作の test_pep8.py です。
test_pep8.py は tests/ フォルダに入れておくと、書かれているコードが pep8 準拠かどうかをチェックしてくれるのですが、
これも API 変更の余波を受けて書き換えが必要になりました。

というわけで、pep8-1.3 に対応した test_pep8.py を作成しました。

# -*- coding: utf-8 -*-import osimport pep8
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))BASE_DIR = os.path.dirname(CURRENT_DIR)


def test_pep8():
    arglist = [['statistics', True],
               ['show-source', True],
               ['repeat', True],
               ['exclude', ['gen-py', 'GAuth.py', 'ttypes.py', 'constants.py']],
               ['paths', [BASE_DIR]]]

    pep8style = pep8.StyleGuide(arglist, parse_argv=False, config_file=True)
    options = pep8style.options
    if options.doctest:
        import doctest
        fail_d, done_d = doctest.testmod(report=False, verbose=options.verbose)
        fail_s, done_s = selftest(options)
        count_failed = fail_s + fail_d
        if not options.quiet:
            count_passed = done_d + done_s - count_failed
            print("%d passed and %d failed." % (count_passed, count_failed))
            print("Test failed." if count_failed else "Test passed.")
        if count_failed:
            sys.exit(1)
    if options.testsuite:
        init_tests(pep8style)
    report = pep8style.check_files()
    if options.statistics:
        report.print_statistics()
    if options.benchmark:
        report.print_benchmark()
    if options.testsuite and not options.quiet:
        report.print_results()
    if report.total_errors:
        if options.count:
            sys.stderr.write(str(report.total_errors) + '\n')
        sys.exit(1)

    # reporting errors (additional summary)
    errors = report.get_count('E')
    warnings = report.get_count('W')
    message = 'pep8: %d errors / %d warnings' % (errors, warnings)
    print message
    assert report.total_errors == 0, message

ちなみに、多くの人が1行79文字の制限にうんざりすることになると思います。
その場合は arglist に ['ignore', 'E501'] というオプションを追加すると幸せになれると思いますよ!