Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking import_module() function causes all subsequent mocks to fail silently #98771

Closed
miguelgrinberg opened this issue Oct 27, 2022 · 2 comments
Labels
topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@miguelgrinberg
Copy link

Bug report

This is a 3.11 regression. Here is the simplest example I could come up with to demonstrate the issue:

from unittest import mock

def f():
    return 'not mocked'

with mock.patch('importlib.import_module') as import_module:
    with mock.patch('__main__.f', return_value='mocked'):
        print(f())

Here is the output under several Python versions:

$ python3.8 mocktest.py
mocked
$ python3.9 mocktest.py
mocked
$ python3.10 mocktest.py
mocked
$ python3.11 mocktest.py
not mocked

In case it helps, I've been looking through the commit list for mock.py and found that ab7fcc8 is the first change that shows the broken behavior.

Your environment

I'm using CPython 3.11.0 on Intel Mac.

$ python3.11
Python 3.11.0 (main, Oct 26 2022, 10:08:00) [Clang 14.0.0 (clang-1400.0.29.102)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
@ronaldoussoren
Copy link
Contributor

I'm not sure if this is a bug, you're mocking importlib.import_module and then do something where a modern implementation would (and as of 3.11 does) use that function to find the module where name resolution should start. The code prints "not mocked" because mock.patch indirectly uses importlib.import_module and hence calls the mock object instead of the real implementation.

This worked in older versions of Python because those used a custom implementation of this functionality instead of relying on a public API for this.

@brettcannon
Copy link
Member

I agree this isn't a bug for the reasons Ronald outlined.

Thanks for at least reporting this, @miguelgrinberg !

@brettcannon brettcannon closed this as not planned Won't fix, can't repro, duplicate, stale Nov 4, 2022
Repository owner moved this from Todo to Done in Unittest issues Nov 4, 2022
maximlt pushed a commit to holoviz-dev/pyct that referenced this issue Jan 30, 2023
* Ensure tox envs that run tests depend on cmd extra

* Fix test_report_gives_package_version on Python 3.11+

Fixes #94. See python/cpython#98771 and
#94 (comment).

* Add missing newline at end of pyct/tests/test_report.py

* Test Python 3.11 and add it to the trove classifiers
noklam added a commit to kedro-org/kedro that referenced this issue Jun 28, 2023
Signed-off-by: Nok <nok.lam.chan@quantumblack.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-importlib type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants