-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Pass importmode to import_path in DoctestModule #10088
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
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This allows doctest to be used with namespace modules
094d3b1
to
85000f0
Compare
asottile
approved these changes
Jun 29, 2022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and just to confirm the failure mode of your test before the fix: $ pytest testing/test_doctest.py
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.2.0.dev179+g85000f037.d20220629, pluggy-1.0.0
rootdir: /tmp/pytest, configfile: pyproject.toml
plugins: hypothesis-6.48.1
collected 134 items
testing/test_doctest.py ....................F........................... [ 35%]
...........................x............................................ [ 89%]
.............. [100%]
=================================== FAILURES ===================================
_________________________ TestDoctests.test_importmode _________________________
self = <test_doctest.TestDoctests object at 0x7f8fe2b40040>
pytester = <Pytester PosixPath('/tmp/pytest-of-asottile/pytest-10/test_importmode0')>
def test_importmode(self, pytester: Pytester):
p = pytester.makepyfile(
**{
"namespacepkg/innerpkg/__init__.py": "",
"namespacepkg/innerpkg/a.py": """
def some_func():
return 42
""",
"namespacepkg/innerpkg/b.py": """
from namespacepkg.innerpkg.a import some_func
def my_func():
'''
>>> my_func()
42
'''
return some_func()
""",
}
)
reprec = pytester.inline_run(p, "--doctest-modules", "--import-mode=importlib")
> reprec.assertoutcome(passed=1)
E AssertionError: ([], [], [<CollectReport 'namespacepkg/innerpkg/b.py' lenresult=0 outcome='failed'>])
E assert {'failed': 1,... 'skipped': 0} == {'failed': 0,... 'skipped': 0}
E Omitting 1 identical items, use -vv to show
E Differing items:
E {'passed': 0} != {'passed': 1}
E {'failed': 1} != {'failed': 0}
E Use -v to get more diff
/tmp/pytest/testing/test_doctest.py:136: AssertionError
----------------------------- Captured stdout call -----------------------------
============================= test session starts ==============================
platform linux -- Python 3.8.10, pytest-7.2.0.dev179+g85000f037.d20220629, pluggy-1.0.0
rootdir: /tmp/pytest-of-asottile/pytest-10/test_importmode0
collected 0 items / 1 error
==================================== ERRORS ====================================
_________________ ERROR collecting namespacepkg/innerpkg/b.py __________________
namespacepkg/innerpkg/b.py:1: in <module>
from namespacepkg.innerpkg.a import some_func
E ModuleNotFoundError: No module named 'namespacepkg'
=========================== short test summary info ============================
ERROR namespacepkg/innerpkg/b.py - ModuleNotFoundError: No module named 'name...
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.09s ===============================
=========================== short test summary info ============================
FAILED testing/test_doctest.py::TestDoctests::test_importmode - AssertionError: ([], [], [<CollectReport 'namespacepkg/innerpkg/b.py' lenre...
=================== 1 failed, 132 passed, 1 xfailed in 6.41s =================== |
4 tasks
4 tasks
nicoddemus
added a commit
to nicoddemus/pytest
that referenced
this pull request
Jun 29, 2023
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. That proved problematic, so we started adding 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
added a commit
to nicoddemus/pytest
that referenced
this pull request
Jun 29, 2023
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
added a commit
to nicoddemus/pytest
that referenced
this pull request
Jul 1, 2023
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
added a commit
to nicoddemus/pytest
that referenced
this pull request
Jul 1, 2023
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
added a commit
that referenced
this pull request
Jul 1, 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 (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
4 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
needs backport
applied to PRs, indicates that it should be ported to the current bug-fix branch
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This allows doctest to be used with namespace modules in tox, as illustrated in the new unit test. See also #3396.