-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Conversation
Hmm not sure why pypy3 build failed, it broke during tox setup. |
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. |
e8a6c76
to
7335b76
Compare
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
7335b76
to
d2ae7d3
Compare
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? |
Thanks @akhilramkee! Yes backporting makes sense, on it now. |
The initial implementation (in #7246) introduced the
importlib
mode, which never added the imported module tosys.modules
, so it included a test to ensure callingimport_path
twice would yield different modules.Not adding modules to
sys.modules
proved problematic, so we began to add the imported module tosys.modules
in #7870, but failed to realize that given we are now changingsys.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 insys.modules
.Fix #10811
Fix #10341