python / python/cpython

unittest.mock.patch('path') neither forwards or backwards compatible starting from Python >= v3.11

オープン
#117,860 コメント 13 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

topic-importlib type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:

Issue

It appears that Python v3.11 and v3.12 are not respecting project functions and classes relative to the module root (module.__init__) when running tests and leveraging unittest.mock.

Proof of Failure

👉 I created a project that can be found here which demonstrates the entire situation.

You can review the import failures across the multiple versions of Python here

Expected Behavior

That the following would work in Python >= 3.11 as it does with all previous versions:

import sys
from unittest import mock


# This actually retrieves apprise.Apprise.Apprise().notify when using
# earlier versions of py.test (hence apprise.__init__.__all__ is respected)
@mock.patch("apprise.Apprise.notify")
def test_py11_and_higher_fails(mock_notify):
    """
    Only works with Python v3.10

    Python v3.11+ produces (where ?? is swapped with 11, or 12 respectively to whichever version of Python is being tested):
    AttributeError: <module 'apprise.Apprise' from '/usr/local/lib/python3.??/site-packages/apprise/Apprise.py'> does not have the attribute 'notify
    """
    pass

For it to work in newer versions of Python, the modules internal object placements are ignored. The following does however work (but incompatible with Python v3.10 and less):

# ...
@mock.patch("apprise.Apprise.Apprise.notify")
def test_py10_and_lower_fails(mock_notify):
     ...

Apprise Background

I'm not sure if this information is relavant; but perhaps the issue is centered around the module itself I'm testing against that makes use of the __all__ setting. Just incase; here are details that surround it:

The __init__ residing in the Apprise Module identifies:

# Get our class Apprise() from within the Apprise.py file
from .Apprise import Apprise

__all__ = [
 'Apprise',  #...
]

Regular Usage Works

Despite testing failing, all versions of the Python continue to respect import and correctly source the class/function refernced within the modules (inconsisent with unittest). The following works great:

# pip install apprise

# All versions of Python correctly respect module definitions here.
from apprise import Apprise

# Do our magic with it. 
obj = Apprise()

After Thoughts

At first I thought there was an issue with py.test but since Pytest v8.1.1 works in Python v3.10 but not in Python v3.11 or v3.12, I decided to open the ticket here. But the issue seems to be related to unittest.mock.patch and centers around Python v3.11 and higher.

Personally, I feel I've exhausted all avenues with my own research, so hopefully you can help me share what could have been introduced. Any tips/help would be much appreciated. 🙏

CPython versions tested on:

3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12

  • bolded are impacted by issue.
Operating systems tested on:

Linux

Linked PRs
  • gh-118176
  • gh-118432

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Apprise の例で unittest.mock.patch を使用して動作を再現し、リンク先のプロジェクトと GitHub Actions の実行結果に示されている Python 3.10、3.11、3.12 の結果を比較します。apprise/init.py と patch 対象解決のエントリーポイントを読み、その後、リンクされている PR gh-118176 と gh-118432 を確認します。報告された Python バージョン全体で意図した patch 対象の動作が確立され、回帰テストでカバーされていれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
testing-qa
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
30/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。