This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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
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 2022-04-11 14:59 by admin. 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 ( ) on Python 3.9.

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

and 3.10:

but missing from 3.9:

Here is our test case:

    import unittest
    import graspologic

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

    if __name__ == '__main__':

fails with:

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

The module triggering the error appears to be, 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 committer) Date: 2021-11-18 01:22 was just opened as a backport
msg406548 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-11-18 16:10
Thanks, Dennis! ✨ 🍰 ✨
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 89994
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