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

Fix duplicated imports with importlib mode and doctest-modules #11148

Merged
merged 1 commit into from
Jul 1, 2023

Conversation

nicoddemus
Copy link
Member

@nicoddemus nicoddemus commented Jun 29, 2023

The initial implementation (in #7246) introduced the importlib mode, which never added the imported module to sys.modules, so it included a test to ensure calling import_path twice would yield different modules.

Not adding modules to sys.modules proved problematic, so we began to add the imported module to sys.modules in #7870, but failed to realize that given we are now changing sys.modules, we might as well avoid importing it more than once.

Then #10088 came along, passing importlib also when importing application modules to collect docstrings (as opposed to only test modules before), which caused problems due to imports having side-effects and the expectation being that they are imported only once.

With this PR, import_path returns the module immediately if already in sys.modules.

Fix #10811
Fix #10341

@nicoddemus
Copy link
Member Author

Hmm not sure why pypy3 build failed, it broke during tox setup.

jaraco added a commit to pmxbot/pmxbot that referenced this pull request Jun 29, 2023
@jaraco
Copy link
Contributor

jaraco commented Jun 29, 2023

Good news is this does work to fix most of the test failures in pmxbot, so it's a good fix. Bad news is there's still two import-related failures that may have a different root cause. I'll investigate those, put together another reproducer and possibly file a new issue (if distinct). Regardless, this fix here seems solid, so feel free to merge.

Update: the other issue appears to be a manifestation of #10337.

The initial implementation (in pytest-dev#7246) introduced the `importlib` mode, which
never added the imported module to `sys.modules`, so it included a test
to ensure calling `import_path` twice would yield different modules.

Not adding modules to `sys.modules` proved problematic, so we began to add the imported module to `sys.modules`
in pytest-dev#7870, but failed to realize that given we are now changing `sys.modules`, we might
as well avoid importing it more than once.

Then pytest-dev#10088 came along, passing `importlib` also when importing application modules
(as opposed to only test modules before), which caused problems due to imports
having side-effects and the expectation being that they are imported only once.

With this PR, `import_path` returns the module immediately if already in
`sys.modules`.

Fix pytest-dev#10811, pytest-dev#10341
@nicoddemus nicoddemus enabled auto-merge (squash) July 1, 2023 15:14
@nicoddemus nicoddemus merged commit b77d0de into pytest-dev:main Jul 1, 2023
25 checks passed
@nicoddemus nicoddemus deleted the import-mode-10811 branch July 1, 2023 16:01
@akhilramkee
Copy link
Contributor

akhilramkee commented Jul 3, 2023

Thanks for the bug fix!

Is there a plan to backport this to 7.4? If not, would this be part of a minor release or a major release?

@nicoddemus
Copy link
Member Author

Thanks @akhilramkee!

Yes backporting makes sense, on it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants