猫でもわかるWebプログラミングと副業

本業エンジニアリングマネージャー。副業Webエンジニア。Web開発のヒントや、副業、日常生活のことを書きます。

Python feedparser で DeprecationWarning To avoid breaking existing software while fixing issue 310, ..

今回起きた事象

feedparser 5 を使っているとこのような DeprecationWarning が出ていた(後に書くが、6でもこの Warning が出る)。

/usr/local/lib/python3.6/site-packages/feedparser.py:334: DeprecationWarning: To avoid breaking existing software while fixing issue 310, a temporary mapping has been created from updated to published if updated doesn't exist. This fallback will be removed in a future version of feedparser. "of feedparser.", DeprecationWarning)

これは feed の updated プロパティを使っていると出る Warning である。

import feedparser

feed_parser_dict = feedparser.parse(feed_url)
for feed in feed_parser_dict.entries:
    print(feed.updated)

この DeprecationWarning の詳細については、feedparser のドキュメントに詳細に書いてありました。

https://pythonhosted.org/feedparser/reference-feed-updated.html

Note: As of version 5.1.1, if this key doesn’t exist but feed.published does, the value of feed.published will be returned.

In the past the RSS pubDate element was stored in updated, but this incorrect behavior was reported in issue 310. However, developers may have come to rely on this incorrect behavior – as was reported in issue 328 – so to help avoid hurting their users’ experience, this mapping from updated to published was temporarily introduced to give developers time to update their software, and to give users time to upgrade. This mapping is temporary and will be removed in a future version of feedparser.

書いている内容としてはこんな感じです。

  • バージョン 5.1.1 において、feed.updated が存在しない場合に、 feed.published が存在しているなら、 feed.published の値が feed.updated として return されます。
  • 以前は、 RSS の pubDate 要素の値が updated にセットされるようになっていましたが、issue 310 でこのような間違った挙動をしていることがわかりました。
  • しかし、issue 328 で上がったように、開発者の中にはこの機能を使っている人もいると思います。そこで、そのような開発者に影響が出ないように、この仕様は一時的に残しておき、開発者が対応する時間や、ソフトウェアの利用者のアップデートのための時間を設けます。
  • この仕様は将来的には削除されます。

issue 310, issue 328 がどれを指しているのかはわかりませんでした。GitHub での issue ではないっぽいです。

要するに、

  • 今までは updated が無い場合には、 published を updated として返していたが、新しいバージョンからは published を updated として返すことはなくなるから、この仕様を利用している人は修正してね

ということ。

feedparser の GitHub リポジトリはこちら

github.com

今回利用していた feedparser のバージョンは 5.2.1 で、問題の実装はここである

not dict.__contains__(self, 'updated') 、つまり、 updated がない場合、かつ、dict.__contains__(self, 'published') な場合は、published を返していることがわかる。

では、この記事執筆時の最新版であるバージョン 6.0.2 ではどうか

まだ直っていない!!!!

めんどくさいことに、 updated の値を参照しただけでこの Warning が出るので、うざすぎる。

feedparser 6 で修正しているから、 feedparser 5 で Warning なら分かるけど、まだ修正されてないのに Warning が出てるのが良くない気がする。

最新の master を見てもこれは直っていなかった。

一応警告を抑制する方法はあるけど、本来知るべきだった警告がでなくなるリスクがあるので、今回はやらないことにした。Warning が気になるなら、別のライブラリを探すのが良さそうだ。

note.nkmk.me

DeprecationWarning とは

DeprecationWarning は、ライブラリの Deprecated な機能を使っている時に出る Warning である。

python コマンド実行時に -Wall オプションをつけると、Warning が表示されるようになる。この時に、 DeprecationWarning も出るので、ライブラリのバージョンアップをする時には便利である。

私の場合は、Django で test を実行する時に、以下のコマンドを実行し、この DeprecationWarning が出ている事に気づいた。

python -Wall manage.py test