classification
Title: unittest.TestCase.assertWarns raises RuntimeEror if sys.modules changes size (Python 3.9)
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, lukasz.langa, pbourke
Priority: normal Keywords:

Created on 2021-11-18 00:47 by pbourke, last changed 2021-11-18 16:10 by lukasz.langa. This issue is now closed.

Messages (3)
msg406516 - (view) Author: Patrick Bourke (pbourke) Date: 2021-11-18 00:47
Hi all,

Forgive me if this is not the correct way to report this, but we have run into the issue from #29620 ( https://bugs.python.org/issue29620 ) on Python 3.9.

The fix appears to be present in the tip of 3.8:

https://github.com/python/cpython/blob/c37a0d9c0ae4aa0d9135fac9a58afc7b34ff71d6/Lib/unittest/case.py#L254

and 3.10:

https://github.com/python/cpython/blob/0ef308a2890571c850c624fb99ac00f8951363c6/Lib/unittest/case.py#L255

but missing from 3.9:

https://github.com/python/cpython/blob/0ef308a2890571c850c624fb99ac00f8951363c6/Lib/unittest/case.py#L255

Here is our test case:

    import unittest
    import graspologic


    class MyTestCase(unittest.TestCase):
        def test_something(self):
            with self.assertWarns(UserWarning):
                pass


    if __name__ == '__main__':
        unittest.main()

fails with:

    ======================================================================
    ERROR: test_something (__main__.MyTestCase)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "C:\Users\pbourke\AppData\Roaming\JetBrains\PyCharm2021.2\scratches\test.py", line 7, in test_something
        with self.assertWarns(UserWarning):
      File "C:\Users\pbourke\.pyenv\pyenv-win\versions\3.9.7\lib\unittest\case.py", line 255, in __enter__
        for v in sys.modules.values():
    RuntimeError: dictionary changed size during iteration


The module triggering the error appears to be IPython.utils.io, which is included somewhere in our dependencies.

My CPython is:
Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)]
msg406517 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python triager) Date: 2021-11-18 01:22
https://github.com/python/cpython/pull/29605 was just opened as a backport
msg406548 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-11-18 16:10
Thanks, Dennis! ✨ 🍰 ✨
History
Date User Action Args
2021-11-18 16:10:57lukasz.langasetstatus: open -> closed

nosy: + lukasz.langa
messages: + msg406548

resolution: fixed
stage: resolved
2021-11-18 01:22:53Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg406517
2021-11-18 00:47:42pbourkecreate