python / python/cpython

`autospec` doesn't work with lazy-imported symbols

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

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

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

説明

Bug report

Bug description:

unittest.mock.patch(target, autospec=True) reads unresolved lazy-import bindings directly from target.__dict__ and autospecs the LazyImportType proxy instead of the imported symbol.

from unittest.mock import patch

lazy from json import dumps

with patch("__main__.dumps", autospec=True) as mock_dumps:
    mock_dumps({})

On CPython main (9d231cbc937f as of now), this raises:

TypeError: 'NonCallableMagicMock' object is not callable

Resolving dumps before patching works. This also affects patch.object, spec=True, and spec_set=True.

A practical example is concurrent.futures.ThreadPoolExecutor, which now uses an explicit lazy import: existing tests such as this one patch it with autospec=True and encounter the same issue.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux, macOS, Windows

Linked PRs
  • gh-153977
  • gh-157768

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

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

はじめの一歩

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

調査の方向性

unittest.mock.patch と patch.object のエントリポイントから始め、json.dumps の遅延インポートを使って例を再現します。autospec=True と spec=True および spec_set=True を比較し、レポートにリンクされている ThreadPoolExecutor のテストケースも含めます。これらのケースが報告された NonCallableMagicMock エラーなしでインポートされたシンボルを処理できれば完了です。

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

評価

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

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

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