`self.doCleanups()` cannot be called from a test method of a `unittest.IsolatedAsyncioTestCase` subclass

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

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

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
50/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python
領域
testing

調査の方向性

提供された再現プログラムを unittest.IsolatedAsyncioTestCase で実行し、次に Lib/unittest/async_case.py、特に _callCleanup と _callMaybeAsync を調べます。gh-91150/GH-31837 で特定されたリグレッションを追跡し、テストメソッドから doCleanups() を呼び出すケースのカバレッジを追加します。例と関連する unittest テストが Context already-entered エラーなしでパスすれば完了です。

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

説明

3.13 3.14 3.15 docs stdlib

Example:

import unittest


events = []

class Test(unittest.IsolatedAsyncioTestCase):

    def test(self):
        events.append('started')
        self.addCleanup(events.append, 'cleanup')
        self.doCleanups()
        events.append('end')

unittest.main(exit=False)

assert events == ['started', 'cleanup', 'end'], events

The above passes in 3.9 and 3.10, but fails in 3.11 with

E
======================================================================
ERROR: test (__main__.Test)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/.../cpython/Lib/unittest/async_case.py", line 75, in _callCleanup
    self._callMaybeAsync(function, *args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../cpython/Lib/unittest/async_case.py", line 95, in _callMaybeAsync
    return self._asyncioTestContext.run(func, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: cannot enter context: <_contextvars.Context object at 0x7fb2f93a9900> is already entered

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)
Traceback (most recent call last):
  File "<stdin>", line 16, in <module>
AssertionError: ['started', 'end']

git bisect found the culprit to be gh-91150/GH-31837.

主要言語
Python
スター
77.2k
フォーク
36k
平均マージ
1日 9時間
マージ済み PR(30日)
558

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

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

はじめの一歩

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

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

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

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